javascript - Message callback returns a value infrequently - Chrome Extension -
javascript - Message callback returns a value infrequently - Chrome Extension -
i'm building chrome extension communicates nodejs server through websockets. point of track browsing history content. seems work, (30% of time) callback in function passed onmessage.addlistener doesn't fire correctly. allow me show code:
background.js
var socket = io('http://localhost:3000/'); var tabload = function (tab) { socket.emit('page load', tab); }; var tabupdate = function (tabid, changeinfo, tab) { var url = tab.url; if (url !== undefined && changeinfo.status == "complete") { tab.user_agent = navigator.useragent; tab.description = ''; tab.content = ''; socket.emit('insert', tab); } }; socket.on('inserted', function(page){ socket.emit('event', 'requesting page content\n'); //page = {tab: page, id: docs._id}; chrome.tabs.sendmessage(page.tab_id, {requested: "content", page: page}, function(data) { socket.emit('content', data); }); }); seek { chrome.tabs.oncreated.addlistener(tabload); chrome.tabs.onupdated.addlistener(tabupdate); } catch(e) { alert('error in background.js: ' + e.message); }
content script - public.js
var messagehandler = function(request, sender, sendcontent) { if (request.requested == "content") { var html = document.getelementsbytagname('html')[0].innerhtml; var info = { content: html, page: request.page }; sendcontent(data); homecoming true; } }; chrome.extension.onmessage.addlistener(messagehandler);
the problem info in sendcontent undefined, while alright. ideas how debug or i'm doing wrong?
i've tried replacing document.getelementsbytagname('html')[0].innerhtml
hardcoded 'test' string, didn't help.
pages youtube/wikipedia seem never work, while facebook/google works.
edit: sendcontent callback fire 100% of time it's info passed undefined.
edit: here's manifest file
{ "manifest_version": 2, "name": "socket test", "description": "sockets cool", "version": "1.0", "permissions": [ "http://st-api.localhost/", "http://localhost:3000/", "tabs", "background", "history", "idle", "notifications" ], "content_scripts": [{ "matches": ["*://*/"], "js": ["public/public.js"] //"run_at": "document_start" }], //"browser_action": { // "default_icon": "logo.png", // "default_popup": "index.html" //}, "background": { //"page" : "background.html", "scripts": ["socket-io.js", "background.js"], "persistent": true } }
first off, understanding sendcontent
executed 100% of time wrong.
as established in comments, sendmessage
callback gets executed when there error; , error is, in case, "receiving end not exist"
the error lies in manifest declaration of content script. match pattern "*://*/"
only match top-level pages on http
, https
uris. i.e. http://example.com/
match, while http://example.com/test
not.
the easiest prepare "*://*/*"
, recommend universal match pattern "<all_urls>"
.
with fixed, there still couple of improvements code.
replacechrome.extension.onmessage
(which deprecated) , utilize chrome.runtime.onmessage
modify sendmessage
part more resilient, checking chrome.runtime.lasterror
. despite wide permission, chrome still won't inject content scripts pages (e.g. chrome://
pages, chrome web store) make sure utilize "run_at" : "document_start"
in content script, create sure onupdated
"complete"
not fired before script ready. javascript node.js google-chrome-extension websocket
Comments
Post a Comment