Chrome Extension: Accessing a cross-origin iframe within background.html

烈酒焚心 提交于 2019-12-12 07:37:39

问题


I'm trying to write an extension that scrapes a site that needs to be loaded as a webpage, due to the complexities of the pages (XHR isn't really an option). The best solution I've come up with is to do the scraping via an iframe placed in the background.html. The extension will provide the user with notifications when certain conditions on the target page are met.

There was a very similar question posted here a few years ago:

Chrome extension: loading a hidden page (without iframe)

I was able to load an iframe with the target page in the background page by removing the x-frame-options header, as suggested in the response to the question above. I can scrape the data via a content script when the page is loaded in a browser tab/window. However, I want to be able to do this from the background script, and of course it cannot access the iframe contentDocument directly due to cross-origin security.

Unfortunately, although the iframe loads the content from the target page just fine, it does not load the content script. It doesn't seem the background.html loads any content scripts at all. This makes sense, because things could get crazy when extensions start loading each other's content scripts.

Is there a manifest option that will allow a specific content script to run in the iframe on the background page?

If not, is there any way to relax cross-origin security in the extension to access the iframe contentDocument?

Please note, using the --disable-web-security chrome command line switch isn't an option. I want non-technical people to be able to use the extension, and I don't want to require users to disable web security to use it.

If there isn't any way to run a content script in a background page or relax security, what other possibilities are there for scraping a page DOM with a chrome extension without using XHR? Surely there must be a way.

来源:https://stackoverflow.com/questions/40248467/chrome-extension-accessing-a-cross-origin-iframe-within-background-html

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