Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google chrome 在后台页面加载远程网页:Chrome扩展_Google Chrome_Google Chrome Extension - Fatal编程技术网

Google chrome 在后台页面加载远程网页:Chrome扩展

Google chrome 在后台页面加载远程网页:Chrome扩展,google-chrome,google-chrome-extension,Google Chrome,Google Chrome Extension,是否可以使用chrome扩展将远程网页加载到后台页面 "background": { "page": "local.html" } 有效,但是 "background": { "page": "http://...." } 失败,出现以下错误: Could not load background page http://.... 不,那是不可能的。这是可能的因为Chrome 22-请参见答案的底部 您可以创建文件,以便可以手动构造背景脚本。在网络关闭的情况下,请确保

是否可以使用chrome扩展将远程网页加载到后台页面

"background": {
    "page": "local.html"
  }
有效,但是

"background": {
    "page": "http://...."
  }
失败,出现以下错误:

Could not load background page http://....
不,那是不可能的。这是可能的因为Chrome 22-请参见答案的底部

您可以创建文件,以便可以手动构造背景脚本。在网络关闭的情况下,请确保在扩展中包含回退资源:

<!-- ... doctype etc ... (background.html) -->
<script src="https://..../external_bg.js"></script>
<script src="bg.js"></script>
如果要动态构建页面,请避免使用类似
eval
的方法,因为CSP也禁止使用这些方法。您可以编写模板,并请求外部值来填充模板<代码>本地存储可用于缓存变量。有关缓存外部资源的示例,请参阅。这个答案指的是内容脚本,因此不能使用确切的方法来启用缓存脚本(因为您需要使用
eval
来加载脚本)。但是,缓存技术仍然可以使用


我也尝试过使用以下方法,但它不起作用(包括在这个答案中,这样你就不必亲自尝试):
从AJAX响应创建一个URL,然后使用创建一个临时URL来加载资源

// Modification of https://stackoverflow.com/a/10371025
// Instead of `chrome.tabs.executeScript`, use 
// x.responseText  or  x.response (eg when using x.responseType='arraybuffer')
var blob = new Blob([x.responseText], {type: 'application/javascript'});
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var s = document.createElement('script');
s.src = url;
document.head.appendChild(s);
前面的代码产生以下错误:

Could not load background page http://....
拒绝加载脚本“blob:chrome扩展%3A//damgmpplfpicjkeogacmlgiceidmilllf/96356d24-3680-4188-812e-5661d23e81df”,因为它违反了以下内容安全策略指令:“脚本src‘self’chrome扩展资源:”

在后台页面中加载外部资源 自Chrome 22以来,在技术上可以(使用
unsafe eval
CSP策略)在后台页面加载非https资源。这显然是不推荐的,因为存在安全问题(例如,它容易受到攻击)

下面是一个加载任意资源并在后台脚本上下文中运行它的示例

function loadScript(url) {
    var x = new XMLHttpRequest();
    x.onload = function() {
        eval(x.responseText); // <---- !!!
    };
    x.open('GET', url);
    x.send();
}
// Usage:
loadScript('http://badpractic.es/insecure.js');

谢谢你非常详细的回答。我尝试的另一种方法是在
local.html
中嵌入外部网站的iframe。它已加载,但由于同源策略,我无法在iframe中调用javascript函数。我会给你的解决方案一个旋转。@DarkX现象如果你能控制外部页面,那么实现(
reference\u to_frame.contentWindow.postMessage
)与框架通信。不幸的是,我无法控制外部页面。@Cthulhu技术已经发展,我已经更新了我的答案。非常酷。我最终重新设计了我的解决方案。但我认为这不应该是首选。
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": ["http://badpractic.es/insecure.js"],
"background": {"scripts": ["background.js"] }