Answers for "chrome extension dynamically add javascript"

1

chrome extension dynamically add javascript

// content script
chrome.runtime.sendMessage({
    injectScript : true,
  	filename:'/scripts/myfile.js'
  }, function(response) {
  if(response.done) {
  	//handle something when script is done
  }
});

// background script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if(message.injectScript){
    chrome.tabs.executeScript(sender.tab.id, {file: message.filename}, function() {
      sendResponse({ done: true });
    });
    return true; // Required for async sendResponse()
  }
});
Posted by: Guest on March-04-2020
0

chrome extension dynamically add javascript

//inject another js file from content script w/ callback
function injectScript(file, node,callback) {
    var th = document.getElementsByTagName(node)[0];
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('src', file);
    s.onload = function() {
        callback();
    };
    th.appendChild(s);
}

injectScript(chrome.extension.getURL('myscript.js'), 'body',function(){
//mysript.js is done loading
});
Posted by: Guest on March-03-2020

Code answers related to "chrome extension dynamically add javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language