调用。
test.js
doSomething(){
console.log('Call the function of embadded js file function');
}
我有一个 test.js 我已经注入网页的文件,例如
var scriptDom = document.createElement('script');
const url = chrome.runtime.getURL('test.js');
scriptDom['src'] = url;
document.getElementsByTagName("BODY")[0].appendChild(scriptDom);
现在,我想从内容脚本调用doSomething()
方法,可以调用注入的JavaScript从内容脚本
rost w给出的答案帮助我解决问题
在 test.js (网页)文件中添加了事件侦听器
window.addEventListener("event_name", function(e){
console.log('event recived in embadded js file')
});
,从内容脚本我将事件触发到
window.dispatchEvent(new CustomEvent('event_name', {
'detail': {
'message': 'some data',
}
}));