2017-02-09 69 views
1

我試圖運行tabs.sendMessage() MDN page上給出的示例代碼。所以我的代碼是:browser.tabs.sendMessage():錯誤:接收結束不存在

manifest.json的

{ 
    "manifest_version": 2, 
    "name": "test1", 
    "version": "1.0", 
    "description": "test", 
    "icons": { 
     "48": "icons/Ruler48.png" 
    }, 
    "permissions": [ 
     "notifications", 
     "tabs", 
     "activeTab" 
    ], 
    "browser_action": { 
     "default_icon": "icons/Ruler48.png", 
     "default_title": "test" 
    }, 
    "content_scripts": [{ 
     "matches": ["*://*/"], 
     "js": ["content-script.js"] 
    }], 
    "background": { 
     "scripts": ["bgS.js"] 
    } 
} 

bgS.js:

function onError(error) { 
    console.error(`Error: ${error}`); 
} 

function sendMessageToTabs(tabs) { 
    for (let tab of tabs) { 
    console.log(tab.id); 
    browser.tabs.sendMessage(
     tab.id, 
     {greeting: "Hi from background script"} 
    ).then(response => { 
     console.log("Message from the content script:"); 
     console.log(response.response); 
    }).catch(onError); 
    } 
} 

browser.browserAction.onClicked.addListener(() => { 
    browser.tabs.query({ 
    currentWindow: true, 
    active: true 
    }).then(sendMessageToTabs).catch(onError); 
}); 

內容的script.js

browser.runtime.onMessage.addListener(request => { 
    console.log("Message from the background script:"); 
    console.log(request.greeting); 
    return Promise.resolve({response: "Hi from content script"}); 
}); 

我與網絡的內線運行運行它,點擊瀏覽器的操作按鈕,在控制檯後,我得到這個消息:Error: Error: Could not establish connection. Receiving end does not exist.

輸出從web-ext run --verbose

web-ext run --verbose 
[program.js][debug] Getting the version from package.json 
[program.js][info] Version: 1.8.0 
[program.js][debug] Getting the version from package.json 
[cmd/run.js][info] Running web extension from C:\test 
[util/manifest.js][debug] Validating manifest at C:\test\manifest.json 
[cmd/run.js][debug] Creating new Firefox profile 
[cmd/run.js][debug] Deferring extension installation until after connecting to the remote debugger  
[firefox/index.js][debug] Checking if remote Firefox port 6005 is available 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[firefox/index.js][debug] Executing Firefox binary: C:\Program Files\Mozilla Firefox\firefox.exe  
[firefox/index.js][info] Use --verbose or open Tools > Web Developer > Browser Console to see logging 
[cmd/run.js][debug] Connecting to the remote Firefox debugger 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[cmd/run.js][debug] Retrying Firefox (0); connection error: Error: connect ECONNREFUSED 127.0.0.1:6005 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[cmd/run.js][debug] Retrying Firefox (1); connection error: Error: connect ECONNREFUSED 127.0.0.1:6005 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[cmd/run.js][debug] Retrying Firefox (2); connection error: Error: connect ECONNREFUSED 127.0.0.1:6005 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[firefox/index.js][debug] Firefox stdout: Started debugger server on 6005 
[cmd/run.js][debug] Retrying Firefox (3); connection error: Error: connect ECONNREFUSED 127.0.0.1:6005 
[firefox/remote.js][debug] Connecting to Firefox on port 6005 
[firefox/remote.js][debug] Connected to the remote Firefox debugger 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/security/socket.js, line 778: ReferenceError: reference to undefined property this._handshakeTimeout 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: chrome://browser/content/browser.js, line 6573: SyntaxError: applying the 'delete' operator to an unqualified name is deprecated 
[firefox/remote.js][debug] installTemporaryAddon: {"addon":{"id":"[email protected]","actor":false},"from":"server1.conn0.addonsActor3"} 
[firefox/remote.js][info] Installed C:\test as a temporary add-on 
[cmd/run.js][info] The extension will reload if any source file changes 
[util/file-filter.js][debug] Resolved path **/*.xpi with sourceDir C:\test to C:\test\**\*.xpi 
[util/file-filter.js][debug] Resolved path **/*.zip with sourceDir C:\test to C:\test\**\*.zip 
[util/file-filter.js][debug] Resolved path **/.* with sourceDir C:\test to C:\test\**\.* 
[util/file-filter.js][debug] Resolved path **/.*/**/* with sourceDir C:\test to C:\test\**\.*\**\* 
[util/file-filter.js][debug] Resolved path **/node_modules with sourceDir C:\test to C:\test\**\node_modules 
[util/file-filter.js][debug] Resolved path **/node_modules/**/* with sourceDir C:\test to C:\test\**\node_modules\**\* 
[util/file-filter.js][debug] Resolved path C:\test\web-ext-artifacts with sourceDir C:\test to C:\test\web-ext-artifacts 
[util/file-filter.js][debug] Resolved path C:\test\web-ext-artifacts\**\* with sourceDir C:\test to C:\test\web-ext-artifacts\**\* 
[watcher.js][debug] Watching for file changes in C:\test 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: chrome://browser/content/tabbrowser.xml, line 640: ReferenceError: reference to undefined property this.mBrowser.initialPageLoadedFromURLBar 
[firefox/index.js][debug] Firefox stdout: 1486682957594 [email protected]porary-addon WARN Please specify whether you want browser_style or not in your browser_action options. 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: chrome://global/content/bindings/browser.xml, line 385: ReferenceError: reference to undefined property tabBrowser.localName 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/node-properties/node-properties.js, line 134: ReferenceError: reference to undefined property control.resume 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/node-properties/node-properties.js, line 124: ReferenceError: reference to undefined property options._comments[c] 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/commonjs/toolkit/loader.js -> resource://devtools/shared/node-properties/node-properties.js, line 130: ReferenceError: reference to undefined property options._separators[c] 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/system/unload.js, line 31: ReferenceError: reference to undefined property observers[i].value 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/ProfileAge.jsm, line 202: ReferenceError: reference to undefined property times.reset 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: resource://gre/modules/LoginStore.jsm, line 275: ReferenceError: reference to undefined property this.data.version 
[firefox/index.js][debug] Firefox stderr: JavaScript strict warning: chrome://global/content/bindings/browser.xml, line 385: ReferenceError: reference to undefined property tabBrowser.localName 

這,什麼瀏覽器控制檯中,第一條消息出現在我安裝它之前。我的意思是它在安裝之前就已經存在了,所以它會默默安裝。點擊按鈕後,我得到了最後的兩個消息,第一個從該行給出tab.idconsole.log(tab.id)然後錯誤:

1486683758937 [email protected]porary-addon WARN Please specify whether you want browser_style or not in your browser_action options. 
10 bgS.js:8:3 
Error: Error: Could not establish connection. Receiving end does not exist. bgS.js:3:3 
+0

「瀏覽器控制檯」(https://developer.mozilla.org/en-US/docs/Tools/Browser_Console)(Ctrl-Shift-J或Cmd-Shift- J在OSX上)當您嘗試安裝和使用擴展? – Makyen

+0

單擊瀏覽器動作按鈕時,活動選項卡是否包含以「http://」或「https://」開頭的URL的頁面?你是否正在使用類似'about:addons'的URL來查看標籤,'about:debugging'等?你是否試圖在[AMO](https://addons.mozilla.org/)的頁面上使用它? – Makyen

+0

不知道你要求什麼,但是當我運行它與網絡分機運行--verbose我得到這個。我更新了問題。 – someNameJustToAnswer

回答

1

您所看到的結果時,有一個內容腳本爲您的擴展定義沒有runtime.onMessage監聽器錯誤在tabs.sendMessage()的調用中指定的選項卡中。這可能是因爲沒有內容腳本,或者因爲已注入的內容腳本沒有爲runtime.onMessage添加偵聽器。

您應該記住,您不能將內容腳本插入到具有像about:newtababout:debugging等網址的頁面中。另外,Firefox不允許您將腳本注入addons.mozilla.org的頁面。

由於您的可能並非您所期望的,因此您的問題已複雜化。你可能是爲了你的matches是:

"matches": ["*://*/*"], 

注意在匹配模式結束後*。您目前使用的模式"*://*/"只會匹配使用httphttps方案加載的任何域上的首頁。例如,它不會匹配https://www.google.com/?gws_rd=ssl,但會匹配http://www.example.com/

鑑於您的代碼直接在用戶點擊瀏覽器或頁面操作按鈕時執行,您需要以某種合理的方式處理此錯誤,以便向用戶提供他們所要求的無法完成的反饋,因爲用戶點擊頁面上的按鈕,如about:newtab,其中導致此錯誤。你如何處理這個錯誤,將取決於你的代碼在做什麼。與其設置一種情況,用戶點擊他們希望工作的啓用按鈕,更好的選擇是在不能執行代碼的頁面上禁用您的操作按鈕(對於瀏覽器操作,使用browserAction.disable())。