showing results for - "chrome extension dynamically add javascript"
Matteo
23 Feb 2020
1// content script
2chrome.runtime.sendMessage({
3    injectScript : true,
4  	filename:'/scripts/myfile.js'
5  }, function(response) {
6  if(response.done) {
7  	//handle something when script is done
8  }
9});
10
11// background script
12chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
13  if(message.injectScript){
14    chrome.tabs.executeScript(sender.tab.id, {file: message.filename}, function() {
15      sendResponse({ done: true });
16    });
17    return true; // Required for async sendResponse()
18  }
19});
Liam
29 Jan 2020
1//inject another js file from content script w/ callback
2function injectScript(file, node,callback) {
3    var th = document.getElementsByTagName(node)[0];
4    var s = document.createElement('script');
5    s.setAttribute('type', 'text/javascript');
6    s.setAttribute('src', file);
7    s.onload = function() {
8        callback();
9    };
10    th.appendChild(s);
11}
12
13injectScript(chrome.extension.getURL('myscript.js'), 'body',function(){
14//mysript.js is done loading
15});