如何从chrome.runtime.onMessageExternal.addListener访问扩展html元素?

时间:2019-06-03 09:40:40

标签: html google-chrome-extension listener

我已经连接了带有chrome扩展名的网页。在background.js中收集响应。以下是我的代码:

manifest.json

{
  "manifest_version": 2,

  "name": "CIC Wallet",
  "description": "The CIC Wallet in your browser",
  "version": "1.0",

  "browser_action": {
   "default_icon": "Image/CI_logo-01.png",
   "default_popup": "popup.html"
  },
  "icons": { "16": "Image/CI_logo-01.png",
    "48": "Image/CI_logo-01.png",
   "128": "Image/CI_logo-01.png" 
  },
  "background":{
    "scripts": ["background.js"]
  },
  "externally_connectable": {
    "matches": ["http://127.0.0.1:5500/main.html"]
  },
  "web_accessible_resources": [
    "popup.js", "background.js"
    ],
  "permissions": [
   "activeTab"
   ]
}

background.js

chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
    console.log('from webpage');
    window.open("popup.html", "CIC Notification", "width=357,height=600,status=no,scrollbars=yes,resizable=no");
    document.getElementById("sendtrform").style.display="block";
});

main.js

window.addEventListener('load', function load(event){

  var createButton = document.getElementById('btn_buy_trust');
  createButton.addEventListener('click', function() { 
    var laserExtensionId = "myextID";
    chrome.runtime.sendMessage(laserExtensionId, 'Hi from Webpage',
    function(response) {

    });
  });
});

main.js是我网页上的js文件,单击按钮时,我正在发送消息。在background.js中,我正在收集响应并在单独的窗口中打开扩展HTML文件。在这里的background.js中,我无法从popup.html文件访问HTML元素。 我在此行出现错误:

document.getElementById("sendtrform").style.display="block";

有人可以帮助我,如何从background.js访问HTML元素?

0 个答案:

没有答案