Firefox 有一个扩展,它使注入的 JS 代码进入页面。
XUL 代码:
window.addEventListener("load", function load(event){
document.addEventListener("TestEvnt", function (e) {
var info = e.detail;
if(info.cmd=='account_list') {
consoleLog('... event account_list');
}
}, false, true);
document.addEventListener("DOMContentLoaded", function (e) {
var window = e.originalTarget.defaultView.wrappedJSObject;
var inject = window.document.createElement('script');
inject.type = 'text/javascript';
inject.setAttribute('src','resource://apptab-notify/injscr.js');
window.document.getElementsByTagName('head')[0].appendChild(inject);
}, false);
},false);
代码 injscr.js:
var additionalInfo = {
"cmd": 'account_list',
"url": document.location.href
};
var evt = document.createEvent('CustomEvent');
evt.initCustomEvent('TestEvnt', true, false, additionalInfo);
document.dispatchEvent(evt);
如何提供双向通信,从扩展监听代码注入事件?不使用 Addon-SDK!