Firefox / Chrome Web扩展 - 尝试通过内容脚本注入IFrame时出现安全性错误

时间:2018-04-15 22:15:10

标签: javascript iframe google-chrome-extension firefox-webextensions content-script

我在内容脚本中有以下非常简单的代码,只要我的" popup.html"按下文件:

" inject.js"

内的代码部分
browser.runtime.onMessage.addListener((message) => {
    console.log("Trying to inject iFrame");
    var iframe = document.createElement("iframe");
    iframe.src = browser.extension.getURL("inject.html");
    document.body.appendChild(iframe);
});

" inject.html"的内容是:

<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <p>
        Hello. This is a Test.
    </p>
</body>

</html>

但是,当此代码运行时,我在控制台中获得以下输出(使用&#34; example.com:作为示例URL):

  

尝试注入iFrame
  安全错误:http://example.com/的内容可能无法加载或链接到moz-extension://218287b3-46eb-4cf6-a27f-45b9369c0cd9/inject.html。

这是我的&#34; manifest.json&#34;

{
"manifest_version": 2,
"name": "Summarizer",
"version": "1.0",

"description": "Summarizes webpages",

"permissions": [
    "activeTab",
    "tabs",
    "storage",
    "downloads",
    "*://*.smmry.com/*"
],

"icons":
{
    "48": "icons/border-48.png"
},

"browser_action":
{
    "browser_style": true,
    "default_popup": "popup/choose_length_page.html",
    "default_icon":
    {
        "16": "icons/summarizer-icon-16.png",
        "32": "icons/summarizer-icon-32.png"
    }
}

"web_accessible_resources": [
    "inject.html"
]
}

最后,这是我的扩展程序的顶级文件结构:

Extension_File_Structure

如何修复此安全错误?

这与此不重复:Security error in Firefox WebExtension when trying getUrl image因为我在manifest.json

中提供了完整路径

1 个答案:

答案 0 :(得分:0)

我错过了一个逗号。这是manifest.json的相关部分,其中包含逗号:

"browser_action":
{
    "browser_style": true,
    "default_popup": "popup/choose_length_page.html",
    "default_icon":
    {
        "16": "icons/summarizer-icon-16.png",
        "32": "icons/summarizer-icon-32.png"
    }
},

"web_accessible_resources": [
    "inject.html"
]