我有一個Chrome擴展,其中彈出消息的內容腳本 - 非常簡單。什麼是Chrome擴展中消息傳遞的一些高級故障排除方法?
aSearch.js(彈出)
function sendMessage(comment){
window.close(); //Close popup.
console.log("In send message: " + comment);
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {method: "insertComment", comment: comment}, function(response) {
// console.log(response.farewell);
});
});
}
然後content.js:始終在Mac OS(Chrome的61.0.3164.100),但不能在Windows 10(鉻61.0.3163.100)以下的作品
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log('In content script after receiving request to insertcomment...');
if (request.method == 'insertComment'){
etc...
我可以提供更多代碼......但即使background.js顯示相關活動,「console.log」也不會觸發。
aSearch.js是一個html彈出窗口,用於替換最初的一個...登錄後(不確定這是否重要)。這是在background.js完成的:
chrome.browserAction.setPopup({ //Sets popup to last visited
popup: 'aSearch.html' // Open this html file within the popup.
});
什麼可以使消息傳遞中斷的任何指導?我是否應該採用不同的方法來處理跨越多個標籤的常用(頻繁)消息傳遞?
爲什麼Chrome for Windows的行爲會有所不同?
附註:我實際上更新了Chrome for Windows,因爲我在寫這個問題之後......在更新突然消息傳遞之後,有一點點工作,然後又回到休眠狀態。
UPDATING問題: 我添加更多的代碼到頂部的彈出aSearch.js ...
我想我關閉該消息之前彈出可以發送...它的工作有時(和我的MacOS開發機器),因爲電腦速度很快。在Windows上,這是我的Mac上的虛擬機,速度不是那麼快...
明天,我將移動消息響應中的關閉......等待消息回覆。
這是否合理?
確保內容腳本實際上已加載。例如,在文件的開頭添加console.log,或者在devtools - Sources - Content腳本中進行調試。 – wOxxOm
這是 - 因爲我有一個焦點監聽器,它有一個控制檯味精。 Thx尋找! – 11teenth
我沒有回答 - 我評論它,以澄清我知道我的content.js腳本加載在頁面上。 – 11teenth