How to access the main HTML document from a pyscript browser extension? - google-chrome-extension

I am trying to create a browser extension following this tutorial and I want to create an extension which can access the actual document and ideally all the network (requests etc.).
But when I print out the documentURL from document
print(document.documentURI)
inside index.html I get the URI of the extension itself (like chrome-extension://enlpckinaieecgmjgamlcbicmjiahkkf/index.html) and not for the webpage it is running on.
How to "access" the actual webpage?

Related

Inject script into iframe inside Chrome extension

I have a Chrome extension that contains a page, chrome-extension://foo/index.html
That page contains an iframe, which loads pages I do not own (not on the same domain).
I want to inject a script into that iframe from the Chrome extension - however, URLs starting with chrome-extension:// are not a valid match pattern for either the "permissions" or "content_scripts" field of the manifest (so I cannot inject it programmatically or declaratively). I am able to inject the script if I host my index.html file elsewhere (not as part of the Chrome extension), but it's annoying to have to have part of my Chrome extension hosted elsewhere. Is there any way to give my extension permissions to inject a script in an iframe inside a file that it owns?

How can I access the url a document is loaded from in chrome.webRequest.onBeforeRequest?

I am building a chrome extension that redirects the tab to an html file if a url on a blacklist is loaded. This html file includes both continue and back buttons. The only problem is I am using chrome.webRequest.onBeforeRequest and I am unable to find a way to store the url that the request originates from. According to the MDN web docs there is a FrameAncestor property that allows you to access this url, but this is not supported by chrome. Are there any equivalent functions or work arounds?

Substitute Chrome tab content without connecting to server

I am writing a Chrome extension and for certain tabs I need:
Prevent a tab from connecting to its URL
Show custom text within the tab
(UPDATE) Tab's URL to stay the same preferably
Please suggest a direction how to achieve both the goals.
What I have tried... I can prevent original content from loading:
chrome.webRequest.onBeforeRequest.addListener(...
return {cancel: true};
A technical page is shown then:
Requests to the server have been blocked by an extension.
Try disabling your extensions.
ERR_BLOCKED_BY_CLIENT
(Maybe it's possible to have a custom technical page?)
But this also prevents my content script from loading and thus I lose ability to alter tab's DOM.
Without onBeforeRequest I can show custom text in the tab via a content script, but Chrome still gonna request the original URL.
Instead of cancelling the request by returning {cancel: true}, redirect the request to a URL which contains your custom content by returning {redirectUrl: "..."}.
The easiest way of doing this will be to include a "request blocked" HTML document within your extension as a web-accessible resource. You can pass parameters to the document using query string arguments -- they are ignored when fetching web-accessible resources, but can be accessed from content scripts using window.location.search.
A notable example of an extension that uses this approach is uBlock Origin, which uses a web-accessible HTML page document-blocked.html as a substitute for blocked web pages.

Google Chrome Extension get DOM from another page of the site

I am dveloping my first extension and I am currently getting data from the page but the page is for example: "example.com/my-data" and I need to display this data from "example.com/" to not need go to that page. How can I get the data from the home page?

Can I access the content of an iframe in JavaScript that is part of a Google Chrome extension?

I have an iframe within the main window named "test_iframe". I want to access the content of "test_iframe" within a Google Chrome extension. I understand that I can do this if I have requisite permissions given in manifest.json.
Can I access the content of this iframe from a background HTML page? Or can I access it in a content script which is part of that extension? (In the latter case I suppose I have to pass the iframe content from the content script to a background page as part of a message for further processing(?))
Let's say this test_iframe comes from http://frame.example.com. I would just inject a content script directly to frame.example.com (instead of the parent page) and do everything there.

Resources