gpt4 book ai didi

javascript - Chrome 扩展程序的内容脚本中出现 XSS "Blocked a frame with origin from accessing a cross-origin frame"错误

转载 作者:行者123 更新时间:2023-12-02 23:52:20 27 4
gpt4 key购买 nike

我在 Google Chrome 商店中安装此扩展程序已有一段时间了。进行维护更新后,我注意到 content.js (内容脚本)中出现以下行:

//Get top document URL (that is the same for all IFRAMEs)
var strTopURL = window.top.document.URL;

现在,当加载的页面中有 IFRAME 时,会引发以下异常:

Blocked a frame with origin "https://www.youtube.com" from accessing a cross-origin frame.

就像我说的,它曾经是获取扩展程序的顶级文档 URL 的方法(从内容脚本)。那么现在公认的方法是什么?

PS。再说一次,我谈论的是 Google Chrome 扩展(而不仅仅是页面上的常规 JS。)

编辑:此脚本在 manifest.json 中的 content_scripts 下运行,定义如下:

"content_scripts": [
{
"run_at": "document_end",
"all_frames" : true,
"match_about_blank": true,
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}
],

最佳答案

内容脚本应要求您的后台脚本通过消息传递来完成此操作:

chrome.runtime.sendMessage('getTopUrl', url => {
// use the URL here inside the callback or store in a global variable
// to use in another event callback that will be triggered in the future
console.log(url);
});
// can't use it right here - because the callback runs asynchronously

background script应该在manifest.json中声明:

"background": {
"scripts": ["background.js"],
"persistent": false
},

您还需要在manifest.json中需要特定的URL权限或允许所有URL:

"permissions": ["<all_urls>"]

以及后台脚本中的监听器:

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg === 'getTopUrl') {
chrome.tabs.get(sender.tab.id, tab => sendResponse(tab.url));
// keep the message channel open for the asynchronous callback above
return true;
}
});

关于javascript - Chrome 扩展程序的内容脚本中出现 XSS "Blocked a frame with origin from accessing a cross-origin frame"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com