I come from Chrome extensions, so I'm used to defining when a file should be injected by setting run_at, e.g., to document_start for injection before DOM construction. Is there an equivalent for Firefox addons?
Yes, the equivalent would be the content-document-global-created notification. An extension can add an observer for that notification and then do something with the window - like injecting a content script. See How to override JS function from a firefox extension? for one example of using this notification.
If you use the Add-on SDK it will do this job for you. The page-mod package supports a contentScriptWhen parameter - you can use "start" as its value and the content script will be injected before any page scripts get a chance to run.
Related
I want to write a browser for some purpose using electron and I choose the <webview> tag to emulate the browser page.
It has a toolbar which contains back forward refresh buttons.
I bind the new window event, in the handler I just create a new <webview> tag for that url.
However, I found that there are some limits.
1. window.opener in new <webview> is null
2. <form> with method="POST" can't be handled correctly, because there are no solutions to get postData from new-window event.(But there is probably a PR for this: https://github.com/electron/electron/pull/19703)
Perhaps I'm wrong from the start. I shouldn't use electron for this purpose? But I don't know much about c#/c++...
I do not recommend to use <webview> at the moment as its usage is strongly discouraged by the Electron team. The recommendation is to use a BrowserView instead (see documentation).
Using Electron to build a web browser should work fine, there is actually a similar project called Wexond. You could check it out as a starting point for your own browser.
I'm working on a Chrome extension that needs contentscripts inside IFrames inside the backgroundpage.
Also I don't want to use the <all_urls> permission. I want to request only the needed host-permissions at runtime.
I can't find a way to do both.
Here are the two ways to declare contentscripts.
The problem with the manifest file is: I can't specify a match-pattern, because I don't know the hosts, and I don't want to use the <all_urls> match-pattern. Is there a way to change this setting at runtime?
The problem with the programmatic injection is: The backgroundpage doesn't have a tabId. Without it I can't use chrome.tabs.executeScript. Is there another way to inject a contentscript into an IFrame inside the backgroundpage?
It's not possible yet. Here is the feature request I made. They say:
it's probably not something we can get to immediately
I'm writing a devtools Chrome extension with a dev panel. The devtools script can use chrome.devtools.inspectedWindow.reload to reload the page and run a script before other scripts run (in every frame in the inspected window). However, the injected script is no longer run for subsequent page reloads and newly created frames.
How can I inject a script into the inspected window that is run at the start of every subsequent page load in the inspected window?
I know I can use a content script that runs at document_start, but that would run the script at the start of each page load regardless of whether the dev panel is open - and the script is intensive, so I'd like to avoid running the script when it's not needed.
Could the devtools script somehow listen for the beginning of page loads in the inspected window and respond by running a script in the page's context?
One option that you can use to avoid running the script when it's not needed, as you have said, is programmatic injection. As discussed:
Inserting code into a page programmatically is useful when your JavaScript or CSS code shouldn't be injected into every single page that matches the pattern — for example, if you want a script to run only when the user clicks a browser action's icon.
To insert code into a page, you must have the following:
your extension must have cross-origin permissions for the page.
It also must be able to use the chrome.tabs module.
You can get both kinds of permission using the manifest file's permissions field.
Once you have permissions set up, you can inject JavaScript into a page by calling tabs.executeScript.
As also discussed in chrome.devtools.inspectedWindow in executing code in the inspected window, use the tabs.executeScript method unless you need the specific functionality that the eval method provides.
However in Communicating Between Extension Components, please note that:
The DevTools page can't call tabs.executeScript directly. To inject a content script from the DevTools page, you must retrieve the ID of the inspected window's tab using the inspectedWindow.tabId property and send a message to the background page. From the background page, call tabs.executeScript to inject the script.
You may go through the given documentations for more information and examples.
I am working on a Chrome extension that needs to call a native application.
As per Google documentation, we are using the chrome.runtime.connectNative method.
However in our extension, it seems that the chrome.runtime object has no method 'connectNative'. When we display the list of methods of the chrome.runtime object, we get the following list (printed by console.log("" + Object.getOwnPropertyNames(chrome.runtime));
getManifest,getURL,reload,requestUpdateCheck,connect,sendMessage,onConnect,onMessage,id
We are using Chrome 31.0.1650.63 on MacOS X 10.8.5 . We have also tried with Chrome Canary (version 34.0.1767.0 canary), we have the same error, with a slightly different list of methods for chrome.runtime:
getManifest,getURL,setUninstallUrl,reload,requestUpdateCheck,connect,sendMessage,onConnect,onMessage,id
So, in both cases (regular Chrome and Chrome Canary), we don't have the 'connectNative' method.
This does not seem to be a permissions problem, our extension manifest does have "nativeMessaging" in the permissions attribute. When we click on the permissions link in the Chrome extension settings, we can see that the extension can "communicate with cooperating native applications".
(sorry I couldn't post screenshots or the full manifest, StackOverflow won't let me paste things that even remotely look like I'm posting an image since I don't have enough reputation....)
Are we missing something ?
The list of properties of chrome.runtime you are getting indicates that your code is running as a content script. Most chrome.* APIs are not available to content scripts. They can only be used from background or event pages, popups, or other extension views you define. So you can use regular extension messaging from your content script to a background or event page, which in its turn can call your native app.
I am trying to use chrome.windows.onCreated but I got Uncaught TypeError: Cannot read property 'onCreated' of undefined. I noticed in the samples, all calls to chrome.* API's seem to originate from background scripts? Can I not use them in content scripts?
I want to trigger clicks in my browser. Then get some information (scrape) in popup window. For that I think I will need to know when a window is opened. So I need windows.onCreated? Can I use that in content scripts? Or how will I combine background and content scripts?
From the documentation for Content scripts:
However, content scripts have some limitations. They cannot:
Use chrome.* APIs (except for parts of chrome.extension)
Use variables or functions defined by their extension's pages
Use variables or functions defined by web pages or by other content scripts
These limitations can indirectly be avoided, mainly by sending messages within the extension. The documentation offers several examples which involves message passing. Prior Chrome 20, the message API methods was called onRequest and sendRequest. Since version 20, they're called onMessage and sendMessage.
Here's an answer which mentions the steps how to pass a message from a content script to a popup:
https://stackoverflow.com/a/11617742