📜 ⬆️ ⬇️

Fast food recipe for popular browsers

0147 (1)

There was a task to write a simple extension for all (if possible) popular browsers. Expansion activity is the implementation of javascript ʻa in the body of the document.

The doctor prescribed javascript injections


It would seem that everything is simple, we execute the code in the browser line:

  1. javascript : var s = document. createElement ( 'script' ) ; s. type = 'text/javascript' ; document. body . appendChild ( s ) ; s. src = 'script.js' ; void ( 0 ) ;
  2. javascript : var s = document. createElement ( 'script' ) ; s. type = 'text/javascript' ; document. body . appendChild ( s ) ; s. src = 'script.js' ; void ( 0 ) ;
  3. javascript : var s = document. createElement ( 'script' ) ; s. type = 'text/javascript' ; document. body . appendChild ( s ) ; s. src = 'script.js' ; void ( 0 ) ;
  4. javascript : var s = document. createElement ( 'script' ) ; s. type = 'text/javascript' ; document. body . appendChild ( s ) ; s. src = 'script.js' ; void ( 0 ) ;
  5. 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 has to 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. Naive searches for a simple solution were not successful:
Writing extensions for a lot of browsers scared me the most, but what to do began to recall the most popular ones:
“There’s no problem with Firefox and Chrome,” I thought.
')

Google chrome


In an hour, an extension for Chrome was written - a lot of documentation on the official site, a lot of examples. In general, a paradise for the developer.

Examples and Guidelines

fragment from background.html

  1. function onRequest ( tabId , changeInfo , tab ) {
  2. if ( changeInfo. status == 'complete' ) {
  3. chrome. tabs . executeScript ( tabId , { code : "code without javascript:" } ) ;
  4. } ;
  5. } ;
  6. chrome. tabs . onUpdated . addListener ( onRequest ) ;

Everything turned out simple and tasteful. Directly insert into the body of the document

Source: https://habr.com/ru/post/107953/


All Articles