試試這個:
Main.js [內容腳本現在
var workers = [];
var widget = widgets.Widget({
id: "my-link",
label: "my-page",
contentURL: self.data.url('mypage.ico')//,
//emit the click event
contentScript: 'self.port.emit('icon-click', 'icon clicked')'
});
function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker);
if(index != -1) {
workerArray.splice(index, 1);
}
}
var pageMod = require('sdk/page-mod').PageMod({
include: ['https://my-page.com'],
//pass the click event to page Script
contentScript: 'self.port.on("icon-click", function()
{document.defaultView.postMessage({ action:"icon-clicked", msg: "my message", "https://my-page.com"})
})',
onAttach: function(worker) {
workers.push(worker);
worker.on('detach', function() {
detachWorker(this, workers);
});
}
});
//pass event to worker's content script
widget.port.on('icon-click', function(){
//use for each on workers
worker[0].port.emit("icon-click", 'icon clicked');
});
,在您的網頁腳本監聽的postMessage
window.addEventListener('message', function(event) {
//include all the dialog libs/resources/css in page
var data = event.data;
if(data.action === 'icon-clicked'){
showDialog(data.msg);
}
}, false);
你可能誤會我question.My點是關於最後一個**頁面可以顯示但沒有CSS和JS(因爲主html中的樣式和js鏈接無效)。**。無法導入通常的本地html文件。 – penkzhou
還是謝謝你的回答。 – penkzhou