2016-12-07 21 views
-2

因此,当用户单击popup.html中的提交时,我需要将文本框值(在popup.html中)传递给我的内容脚本。我阅读了很多教程,但我无法真正理解他们做了什么。将popup.html中的文本框值传递给Chrome扩展中的我的内容脚本

popup.html:

<!DOCTYPE html> 
<html> 

<body> 

<form> 
    <input type="text" placeholder="MCM Username" id="username"><br> 
    <input type="checkbox" name="vehicle" value="Bike" checked> ON?<br> 
    <input type="submit" placeholder="MCM Username" id="sbBtn"> 


</body> 
</html> 

manifest.json的:

{ 
    "name": "MCMRainbowName", 
    "manifest_version": 2, 
    "version": "1.0.0", 
    "description": "Turn your name into a rainbow color on MC-Market.", 
    "browser_action": 
    { 
     "default_icon": "icon.png", 
     "default_popup": "onoff.html" 
    }, 

    "content_scripts":[ 
     { 
      "matches":["http://mc-market.org/*", "https://mc-market.org/*"], 
      "js": ["rainbow.js"] 
     } 
    ] 
} 

rainbow.js(我的内容脚本):

var username = "RaghavJhavar"; 

setInterval(function() { 
    var elements = document.getElementsByTagName("span"); 

    for(var i = 0; i < elements.length; i++){ 
     if(elements[i].innerText == username /*|| elements[i].innerText == "RaghavJhavar"*/){ 
      document.getElementsByTagName("span")[i].setAttribute("class", "style21"); 
     } 
    } 
}, 1000); 

当用户点击 “提交” 上popup.html,如何将textbox的值id =“username”发送到rainbow.js?我希望rainbow.js中的变量用户名与popup.html中id为“用户名”的文本框值相等。

你能解释一下怎么做吗?

非常感谢。

+0

可能的复制[Chrome扩展 - 消息从Popup传递到内容脚本](http://stackoverflow.com/questions/6108906/chrome-extension-message-passing-from-popup-to-content-script) – Makyen

回答

相关问题