它是一個沙盒的東西我猜,你不允許從頁面執行腳本,所以我猜插件數到。
,已知它只是玩沙箱外部的事....
的script.js
var customEvent = document.createEvent('Event');
customEvent.initEvent('JPlayerNotify', true, true);
function notify() {
document.getElementById('communicationDIV').innerText='notify';
document.getElementById('communicationDIV').dispatchEvent(customEvent);
}
// Utitlity function to append some js into the page, so it runs in the context of the page
function appendScript(file) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.setAttribute("src", chrome.extension.getURL(file));
document.head.appendChild(script);
}
appendScript("JPlayer.js");
// had to wait for a bit for the page to be ready (dialup and all), you wont need to do the setTimeout
setTimeout("notify()",3500);
JPlayer.js
var notify_node = document.createElement('div');
notify_node.id = 'communicationDIV';
document.documentElement.appendChild(notify_node);
notify_node.addEventListener('JPlayerNotify', function() {
var eventData = notify_node.innerText;
if (eventData=='notify'){
$("#jplayer").jPlayer('play', 0);
}
});
的manifest.json
{
"name": "JPlayerNotify",
"version": "0.5.0",
"description": "JPlayerNotify",
"content_scripts" : [
{
"matches": ["http://chat.stackoverflow.com/rooms/*"],
"js" : ["script.js"],
"run_at" : "document_idle",
"all_frames" : false
}
],
"permissions": [
"http://stackoverflow.com/*",
"https://stackoverflow.com/*",
"http://*.stackoverflow.com/*",
"https://*.stackoverflow.com/*"
]
}
你可以看到這裏的頁面通信的一些東西... http://code.google.com/chrome/extensions/content_scripts.html
在哪些文件你把這個代碼?我猜測代碼沒有在頁面上運行,而是擴展的背景頁面。 – mowwwalker 2012-03-07 00:16:46
@Walkerneo我在其中一個「content_scripts」JS文件中運行這個。 – PeeHaa 2012-03-07 00:19:19
@Walkerneo我添加了一個鏈接到項目,所以你可以驗證。 – PeeHaa 2012-03-08 22:07:14