按钮单击后,将数据从弹出窗口发送到内容脚本

时间:2017-02-08 13:15:11

标签: google-chrome-extension content-script

我正在制作Chrome扩展程序,我想在弹出窗口中输入用户输入并填充页面的文本框。我有以下代码,但弹出窗口中的数据未到达内容脚本 Manifest.json

{
  "manifest_version": 2,
  "name": "Copy from popup to current tab",
  "version": "1.0",
  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab",
   "storage"
   ],
    "content_scripts": [{
    "js": ["content.js"],
    "matches": ["*://*/*"],
    "run_at": "document_end"
  }]
}  

Popup.html

<!DOCTYPE html>
<html>
  <head>
    <title>Copy from popup to webpage</title>
    <script src="popup.js"></script>
  </head>
  <body>
  <input type= "text" id = "inp" height='50' width = '200'></input>
    <button id="send">Click Me</button>
  </body>
</html>  

Popup.js

window.onload = function(){
    document.getElementById('send').onclick=LogIn;
  }
function LogIn(){
var data = document.getElementById('inp').value;
chrome.storage.local.set({
    data : data
},function(){
    chrome.tabs.executeScript({
        file: "content.js"
    });
}
);
}  

Content.js

chrome.storage.local.get('data',function(items){
var gotdata = items.data;
chrome.storage.local.remove('data');
});

0 个答案:

没有答案