A recipe for instant extensions for popular browsers
The problem arose of writing a simple extension for all (if possible) popular browsers. The activity of the extension is to inject javascript into the body of the document.
Doctor prescribed javascript injections
It would seem simple, we execute the code in the browser line:
- javascript: var s = document.createElement('script');
- s.type='text/javascript';
- document.body.appendChild(s);
- s.src='script.js';
- void(0);
But what to do if your script should process all pages of another site. Do not force the user to constantly click on the tab with the injection code after each transition to a new page. The naive search for a simple solution was unsuccessful:- It was the interception of all links on the page and the addition of its “javascript:” code, of course the code was executed as part of an open page and then moved to a new “clean” page through (location.href).
- The option with setTimeout was generally in the order of nonsense ... execution fell off after the start of loading a new page.
- IE 7+ (for 6-ku decided to just score, a lot of costs for the sake of a small audience of users - excuse me)
- Firefox 1.5+
- Chrome 4+
- Opera 9+ (as a result, extension turned out to be written only under Opera 11)
- Safari 3+
Google chrome
An extension for Chrome was written in an hour - a lot of documentation on the official site, a lot of examples. In general, a paradise for the developer.
Examples and Guide
snippet from background.html
- function onRequest(tabId, changeInfo, tab) {
- if (changeInfo.status == 'complete'){
- chrome.tabs.executeScript(tabId, { code:"код без javascript:" });
- };
- };
- chrome.tabs.onUpdated.addListener(onRequest);
Everything turned out simple and tasteful. Insert directly into the document body