javascript - Unable to check runtime.lastError during browserAction.setBadgeText -
chrome.browseraction.setbadgetext(object details) used set badge text chrome extension. however, if tabid doesn't exist, chrome produces following error using console.error():
unchecked runtime.lasterror while running browseraction.setbadgetext: no tab id: ####.
this becomes problem when badge text set during page load. if tab exists closed user, setbadgetext ends being called using non-existent tabid.
normally, can prevented checking chrome.runtime.lasterror in callback argument of problematic function as answered in question. however, since browseraction.setbadgetext() has no callback parameter, there seems no way prevent error.
when tabid integer not representing tab, following code still produces error though attempts to...
- catch error
try...catch(which doesn't work sinceconsole.error()not thrown). - check
lasterrorafter callingsetbadgetext(which doesn't work since function asynchronous). - use various
settimeoutintervals checklasterrorafter callingsetbadgetext(which doesn't work , wouldn't reliable). - add callback argument
setbadgetext(which has been commented out since chrome produces error when using since there no second argument).
var tabid = 5000; function clearerror () { console.log(chrome.runtime.lasterror); } try { chrome.browseraction.setbadgetext({ text: 'text', tabid: tabid }/*, clearerror*/); clearerror(); settimeout(clearerror, 0); settimeout(clearerror, 50); settimeout(clearerror, 500); } catch (e) { console.log('caught', e); clearerror(); } would there way check chrome.runtime.lasterror prevent error occurring?
an option call chrome.tabs.get first , if no error called assume tab exist next few milliseconds.
var tabid = 5000; function callback(tab) { if (chrome.runtime.lasterror) { console.log(chrome.runtime.lasterror.message); } else { chrome.browseraction.setbadgetext({ text: 'text', tabid: tabid }); } } chrome.tabs.get(tabid, callback); there of course chance tab closed between tabs.get finishing , setbadgetext getting called it's unlikely.
Comments
Post a Comment