Load remote webpage in background page: Chrome Extension

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

Is it possible to load a remote webpage into a background page using a chrome extension?

"background": {     "page": "local.html"   } 

works, but

"background": {     "page": "http://...."   } 

fails with the following error:

Could not load background page http://.... 

回答1:

No, that's not possible. It is possible since Chrome 22 - see the bottom of the answer.

You can whitelist a https: resource in the manifest file file, so that your background script can manually be constructed. Make sure that you include a fallback resource in your extension, in the case that the network is down:

 

Because of the Content security policy (CSP), you cannot run inline JavaScript, so you have to use external JS files. bg.js may look like:

if (!window.namespace_of_external_bg) {     // Fallback, by defining fallback methods or injecting a new script:     document.write(''); } 

If you want to dynamically construct a page, avoid use of eval-like methods, because these are also forbidden by the CSP. You can write a template, and request external values to populate your template. localStorage can be used to cache variables. For an example on caching external resources, see Chrome extension adding external javascript to current page's html. This answer referred to Content scripts, so the exact method cannot be used to enable caching scripts (because you would need to use eval to load the script). However, the caching technique can still be used.


I have also tried to use the following method, which does not work (included in this answer, so that you don't have to try it yourself):
Create a Blob from the AJAX response, then use webkitURL.createObjectURL to create a temporary URL to load the resource.

// 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); 

The previous code yields the following error:

Refused to load the script 'blob:chrome-extension%3A//damgmplfpicjkeogacmlgiceidmilllf/96356d24-3680-4188-812e-5661d23e81df' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".

Loading external resources in the background page

Since Chrome 22, it is technically possible (using the unsafe-eval CSP policy) to load non-https resources in the background page. This obviously not recommended because of security concerns (because it's susceptible to the MITM attack, for instance).

Here's an example to load an arbitrary resource and run it in the context of the background script.

function loadScript(url) {     var x = new XMLHttpRequest();     x.onload = function() {         eval(x.responseText); // 
  • The unsafe-eval CSP policy must be specified.
  • As usual, to make cross-origin requests, the URL must be whitelisted in the manifest at the permissions section, or the server must enable CORS.

So, the manifest should at least contain:

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", "permissions": ["http://badpractic.es/insecure.js"], "background": {"scripts": ["background.js"] } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!