📜 ⬆️ ⬇️

Implementing Javascript code into a page via the Chrome Extension

Once I faced the task of writing an extension for a single browser-based toy under Google Chrome.
For the extension it was necessary to use the javascript environment of the page itself. The problem turned out to be that access to this environment is impossible from Content-extension scripts.

As a result of the search work, it was found that there are no successful solutions to this problem, therefore, after studying the existing material, a simple system was created that allows you to embed any code directly into the village, without additional suffering in the development process. Next, I will present a simple example of an extension that solves this problem.

The idea is based on copying the contents of the script file directly into the tag. . , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
  .     ,     ,       . 

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
    . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  • . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
    . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  • . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
    . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  • . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
    . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  • . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
    . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
. , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
. , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
  .     ,     ,       . 

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
. , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
  .     ,     ,       . 

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
. , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .
  1. . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  2. . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
  3. . , , .

    :
    manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

    :

    manifest.json

    { "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

    manifest- .
    , injected.js web_accessible_resources, content-.

    injected.js

    function injected_main() { alert('Injected!'); }

    , . javascript , .

    background.js

    $.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

    Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

    . ( ), - . - injected.js .
. , , .

:
manifest.json - background.js - content script, injected.js - jquery.js - ajax. jquery.com

:

manifest.json

{ "name": "JS Code Injection", "version": "1.0", "manifest_version": 2, "content_scripts": [ { "matches": [ "http://extension.target.url" ], "js": [ "jquery.js", "background.js" ], "run_at": "document_end" } ], "web_accessible_resources": [ "/injected.js" ] }

manifest- .
, injected.js web_accessible_resources, content-.

injected.js

function injected_main() { alert('Injected!'); }

, . javascript , .

background.js

$.get(chrome.extension.getURL('/injected.js'), function(data) { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.innerHTML = data; document.getElementsByTagName("head")[0].appendChild(script); document.getElementsByTagName("body")[0].setAttribute("onLoad", "injected_main();"); } );

Chrome Extension API chrome.extension.getURL() , , . ajax ( Shared DOM ) , ( injected_main() injected.js)

. ( ), - . - injected.js .

')

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


All Articles