内容脚本未运行

时间:2017-06-17 22:15:55

标签: firefox firefox-addon firefox-webextensions

我需要运行附加到发生错误时出现的特定HTML页面的内容脚本。 webextension目前是嵌入式扩展。文件结构如下:

- SDK extension [directory]
   - webextension [directory]
     - main.js
     - data [directory]
       - error.html
       - error-script.js

一切都运行良好,但是当我点击应触发内容脚本的按钮时,没有任何反应。我在manifest.js "js": ["webextension/data/error-script.js"]中为内容脚本文件路径尝试了多种格式,但没有任何更改。

修改 发生错误时error.html的路径包含一些自动生成的前缀,该前缀不反映实际路径:

moz-extension://7c92a8c2-219d-4233-a973-c51032e2a375/data/error.html

这是一个简单的示例代码:

1)manifest.json

{
  "manifest_version": 2,
  "name": "example",
  "version": "1.0",
  "description": "No description.",
  "background": {
    "scripts": ["main.js","error.js"]
  },

  "content_scripts": [
    {
      "matches": ["*://*/error.html"],
      "js": ["webextension/data/error-script.js"]
    }
  ],

  "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest"
],

"browser_action": {
  "default_icon": {
    "64": "icons/myicon.png"
  },
  "default_title": "example"
},

"hasEmbeddedWebExtension": true
}

2)HTML页面error.html

<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
Test.
<input type="button" class="error-button" id="error-button" value="error">
</body>
</html>

3)内容脚本error-script.js

var button = document.getElementById("error-button");
button.addEventListener("click",showAlert);

function showAlert()
{
  alert("Error");
}//end function.

4)后台脚本:error.js

var target = "<all_urls>";

function logError(responseDetails) {
      errorTab=responseDetails.tabId;
      console.log("response tab: "+errorTab);
      browser.tabs.update(errorTab,{url: "data/error.html"});
}//end function

browser.webRequest.onErrorOccurred.addListener(
  logError,
  {urls: [target],
  types: ["main_frame"]}
);

0 个答案:

没有答案