为什么我的Edge扩展中的内容脚本块不起作用?

时间:2017-06-18 09:03:27

标签: javascript json microsoft-edge microsoft-edge-extension

我有一个适用于Chrome和Firefox的工作扩展程序,我也试图让它在Microsoft Edge中运行。由于某种原因,内容脚本停留在“runtime.sendMessage”函数上。我使用的是Edge 40.15063。

Edge控制台说:

“API调用:'runtime.sendMessage'”

“script block(9)(214,17)。”

manifest.json:

{

  "manifest_version": 2,
  "name": "injecter",
  "version": "0.1.5.9",
  "author": "kovler",

  "description": "check the page url and then inject a code",
  "icons": {
    "48": "icons/icon32.png",
    "96": "icons/icon96.png"
  },
  "applications": {
    "gecko": {
      "id": "test@test.com",
      "strict_min_version": "45.0"
    }
  },
  "content_scripts": [{
    "matches": ["http://*/*", "https://*/*"],
    "js": ["edge.js", "content.js"]
  }],
  "background": {
    "scripts": ["edge.js", "constants.js", "promises.js", "main.js"],
    "persistent": true
  },
  "permissions": [
    "http://*/",
    "https://*/",
    "storage",
    "webNavigation",
    "tabs",
    "activeTab",
    "cookies",
    "webRequest",
    "webRequestBlocking"
  ],
  "-ms-preload": {
    "backgroundScript": "backgroundScriptsAPIBridge.js",
    "contentScript": "contentScriptsAPIBridge.js"
  }
}

edge.js:

if (typeof chrome == "undefined" || typeof chrome.runtime == "undefined")
    chrome = browser;

content.js:

(function(){

  function injectLocalScript (content, node) {
    var th = document.getElementsByTagName(node)[0];
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.innerHTML = content;
    th.appendChild(s);
  }

  function getAnswer(theMessageEvent) {
    if (theMessageEvent) {
      if (theMessageEvent.name === "Inject") {
        injectLocalScript(theMessageEvent.codeLine, 'body'); 
        console.log("injected");
      }      
    }
  }

  chrome.runtime.sendMessage({ name: "injectionCompleted" }, getAnswer );

}());

0 个答案:

没有答案