0

可能重复:
从 chrome 的扩展内容脚本访问 iframe 内容

我正在尝试通过 Chrome 扩展来操作网页上 iframe 的一部分。

每次我这样做时,我都会收到“不安全的 JavaScript 尝试使用 URL 访问框架”错误。 阅读 Chrome 扩展文档,看来我应该能够在 manifest.json 中请求执行此操作的权限。但我所做的一切似乎都不允许我这样做。

我的清单.json:

{
  "name": "Manipulate an iframe",
  "description": "Test to manipulate an iframe",
  "version": "1.0",
  "background": { "scripts": ["background.js"] },

  "permissions": [
    "tabs", 
    "http://*/",
    "https://*/",
    "<all_urls>"
  ],
  "browser_action": {
    "name": "Modify iframe",
    "icons": ["icon.png"]
  },
  "manifest_version": 2
}

我的背景.js:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(null, { allFrames:true, runAt:"document_end", file: "jquery-latest.min.js" }, function() {
    chrome.tabs.executeScript(null, { allFrames:true, runAt:"document_end", file: "content-script.js" });
  });
});

我的内容-script.js:

$('.container').css('width','98%');
$('#myPage iframe').contents().find('#code').unwrap()

当我单击扩展按钮时,容器的大小已正确调整,但我无法触摸 iframe。在我看来,我已经授予了所有权限,那么我还需要在其他地方指定权限吗?

编辑 :

我发现这个答案有助于理解 Chrome 扩展程序中不同类型的脚本:https ://stackoverflow.com/a/9916089/1698152

标记的重复答案:从 chrome 的扩展内容脚本访问 iframe 内容足以回答我的问题。

我没有尝试通过 tabs.executeScript 将内容脚本注入所有帧,而是通过在清单中定义它们将内容脚本注入所有帧。然后,我可以通过消息传递从后台页面脚本调用这些脚本中的函数。

但是,我仍然不明白为什么这与我已经在做的有任何不同。

4

0 回答 0