0

test.js

doSomething(){
   console.log('Call the function of embadded js file function');
}

I have one test.js file that I have injected into the webpage like

var scriptDom = document.createElement('script');
const url = chrome.runtime.getURL('test.js');
scriptDom['src'] = url;
document.getElementsByTagName("BODY")[0].appendChild(scriptDom); 

Now I want to call doSomething() method from content script is possible to call the injected javascript to call from content script

Muhammad chhota
  • 1,837
  • 2
  • 16
  • 27
  • No, content scripts run in isolated world. You can add another script element that runs a function exposed by the first one or use DOM messaging via CustomEvent. See [Insert code into the page context using a content script](//stackoverflow.com/q/9515704) – wOxxOm Mar 09 '19 at 11:06

1 Answers1

0

An answer given by Row W help me to solve the problem

Added event listener in the test.js (webpage) file

window.addEventListener("event_name", function(e){ 
     console.log('event recived in embadded js file')
});

and from content script I trigger the event to

 window.dispatchEvent(new CustomEvent('event_name', {
            'detail': {
                'message': 'some data',
            }
}));
Muhammad chhota
  • 1,837
  • 2
  • 16
  • 27