Chrome插件文字转发

时间:2017-01-03 14:21:51

标签: javascript html css google-chrome

我正在使用Chrome插件,我需要帮助将数据从popup.html转发到background.js

我想将文本粘贴到弹出窗口的输入文本框中,并能够在后台脚本中使用它,目前,它应该只附加文本,但我打算使用文本操作来自动化“数据库”输入工作。 从excel通过我的插件到某些形式。

的manifest.json

{
  "manifest_version": 2,

      "name": "LMT",
      "description": "This extension will save your life",
      "version": "1.0",

  "browser_action": {
       "default_icon": "icon.png",
       "default_popup": "popup.html",
       "default_title": "LMT"
  },
  "background": {
      "scripts": ["background.js"]

  },
    "content_scripts":[{
        "matches": ["http://*/*","https://*/*"],
        "js":["contentscript.js"]

}],
      "permissions": [
       "activeTab",
       "background" ,
       "tabs",
       "http://*/*",
       "https://*/*"
   ]
}

Popup.html

<!DOCTYPE html>
<html>
<head>
    <script src="contentscript.js"></script>
    <style>body{height: 100px;width: 200px;}</style>
</head>
<body>

    <p>Welcome to Upload tool</p>
    <input type="text" id="inputxt">
    <button id="start">Start</button>

</body>
</html>

Content.js

(function() {

    document.addEventListener("DOMContentLoaded", function(event) { 
        var element = document.getElementById('start');

        var txtimport = document.getElementById('inputxt');
        element.addEventListener('click', function () {


                chrome.tabs.executeScript(null,{file:"background.js"});

                //alert("BOF");

        });
    });


})();

background.js

var x = document.createElement("div");
var t = document.createTextNode("This is a paragraph.");
x.appendChild(t);
x.style.width = "80%";
x.style.marginLeft = "50px";
x.style.height = "300px";
x.style.background = "#499";
x.style.color = "white";
document.body.appendChild(x);

谢谢

1 个答案:

答案 0 :(得分:0)

如果您尝试传递数据,或者只是在background.js和Content.js之间进行通信,那么您应该查看Message Passing

本文有一些很好的例子。