我正在研究一個簡單的擴展,它基於瀏覽器中的某些用戶操作將消息發送到一個彈出腳本,然後該腳本又調用後臺腳本中的函數。我是新開發的Chrome擴展程序,請耐心等待。異常郵件檢測
目前,我有一個設置,檢測用戶在內容腳本瀏覽器中的操作,發送消息到彈出腳本,並調用檢測到的背景頁面中的功能(或者我相信,我沒有通過background.js獲取警報或日誌以顯示任何地方)。
我的問題是:爲什麼在從後臺腳本發送消息時沒有檢測到消息,並且是在我的後臺腳本中被調用的函數呢?
的manifest.json
{
...
"browser_action": {
"default_icon": "gamify.png",
"default_popup": "user_stats.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["jquery.min.js", "contentscript.js"],
"run_at": "document_end"
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"storage"
]
}
contentscript.js
$(document).ready(function() {
$("#page-container").click(function() {
chrome.runtime.sendMessage({
action: "Load"
});
});
});
//彈出腳本
$(document).ready(function() {
var bg = chrome.extension.getBackgroundPage();
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
var action = request.action;
if (action == "Load") {
bg.initialize();
}
});
});
background.js
function initialize() {
chrome.runtime.sendMessage({
action: "Start"
});
chrome.storage.sync.get("initialized", function(data) {
alert("BS: Get initialized: " data);
//Do stuff here
});
}
你能解釋一下把這個「代理」代碼放在彈出窗口背後的邏輯嗎? – Xan 2014-10-11 22:22:44
更進一步,我打算讓彈出式js處理內容和後臺腳本之間的消息/數據傳遞,同時使用該數據修改顯示彈出框中的元素。 – Chris 2014-10-11 22:25:41
壞主意。我會解釋爲什麼。 – Xan 2014-10-11 22:33:30