From b8ee6e4f9a55062fd72d694ef93d8bcf6488ce80 Mon Sep 17 00:00:00 2001 From: Christopher White Date: Wed, 31 Jul 2019 09:36:35 -0400 Subject: [PATCH] Fixes: module "process"; chrome.runtime.lastError - Added a 'require' to pull process into the background page. May fix the issue identified at https://github.com/cxw42/TabFern/issues/100#issuecomment-513267574 . - More sophisticated stringification of chrome.runtime.lastError. Somewhere around Chrome 75, chrome.runtime.lastError lost toString(), as far as I can tell. --- app/bg/background.js | 9 ++++++++- app/win/main_tl.js | 12 ++++++------ package.json | 3 ++- vendor/common.js | 23 ++++++++++++++++++++++- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/bg/background.js b/app/bg/background.js index d784b42f..f60a333d 100755 --- a/app/bg/background.js +++ b/app/bg/background.js @@ -5,6 +5,13 @@ console.log('TabFern: running ' + __filename); if(false) { // Vendor files - listed here only so they'll be bundled require('vendor/validation'); require('vendor/common'); + + // The following require() seems to fix the 'cannot find module "process" + // from "/"' error mentioned at + // https://github.com/cxw42/TabFern/issues/100#issuecomment-450058252 + // and discussed further at + // https://github.com/cxw42/TabFern/issues/100#issuecomment-513267574 . + require('process/browser'); } const S = require('common/setting-definitions'); // in app/ @@ -144,7 +151,7 @@ function editNoteOnClick(info, tab) function(resp){ if(isLastError()) { console.log('Couldn\'t send edit-note to ' + tab.id + ': ' + - chrome.runtime.lastError); + lastBrowserErrorMessageString()); } else { console.log({[`response to edit-note for ${tab.id}`]: resp}); } diff --git a/app/win/main_tl.js b/app/win/main_tl.js index fcbbe775..d74fcc9a 100755 --- a/app/win/main_tl.js +++ b/app/win/main_tl.js @@ -442,7 +442,7 @@ function saveTree(save_ephemeral_windows = true, cbk = undefined) return; // Saved OK } //else there was an error - let msg = _T('errCouldNotSave', chrome.runtime.lastError.toString()); + let msg = _T('errCouldNotSave', lastBrowserErrorMessageString()); log.error(msg); window.alert(msg); // The user needs to know if(typeof cbk === 'function') cbk(new Error(msg)); @@ -561,7 +561,7 @@ function flagOnlyCurrentTabCC(cwin) if(!isLastError()) { flagOnlyCurrentTab(cwin); } else { - log.info({"Couldn't flag": chrome.runtime.lastError}); + log.info({"Couldn't flag": lastBrowserErrorMessageString()}); } } //flagOnlyCurrentTabCC @@ -1710,7 +1710,7 @@ function loadSavedWindowsIntoTree(next_action) { READIT: if(isLastError()) { //Chrome couldn't load the data - log.error("Chrome couldn't load save data: " + chrome.runtime.lastError + + log.error("Chrome couldn't load save data: " + lastBrowserErrorMessageString() + "\nHowever, if you didn't have any save data, this isn't " + "a problem!"); @@ -1745,7 +1745,7 @@ function DBG_printSaveData() { chrome.storage.local.get(K.STORAGE_KEY, function(items) { if(isLastError()) { - console.log(chrome.runtime.lastError); + console.log(lastBrowserErrorMessageString()); } else { let parsed = items[K.STORAGE_KEY]; console.log('Save data:'); @@ -2005,7 +2005,7 @@ function treeOnSelect(evt_unused, evt_data, options={}) if(isLastError()) { window.alert(_T('errCouldNotOpenWindow', - chrome.runtime.lastError)); + lastBrowserErrorMessageString())); return; // with the state in the tree unchanged } @@ -4322,7 +4322,7 @@ function moveWinToLastPositionIfAny_catch(done, items_or_err) chrome.windows.update(my_winid, size_data, (win)=>{ if(isLastError()) { - log.error(`Could not move window: ${chrome.runtime.lastError}`); + log.error(`Could not move window: ${lastBrowserErrorMessageString()}`); } else { log.info({"Updated window size":$.extend({},win)}); } diff --git a/package.json b/package.json index b78b8c27..47b7ab2f 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tabfern", - "version": "0.2.1-pre.1", + "version": "0.2.1-pre.2", "description": "Google Chrome extension for displaying, saving, and managing tabs", "main": "src/view/main.js", "directories": { @@ -46,6 +46,7 @@ "keypress.js": "^2.1.5", "loglevel": "^1.6.1", "path-browserify": "^1.0.0", + "process": "*", "requirejs": "^2.3.5", "rmodal": "^1.0.33", "signals": "^1.0.0", diff --git a/vendor/common.js b/vendor/common.js index f7d7c984..ebcd773b 100755 --- a/vendor/common.js +++ b/vendor/common.js @@ -17,7 +17,7 @@ const MSG_GET_VIEW_WIN_ID = 'getViewWindowID'; const MSG_EDIT_TAB_NOTE = 'editTabNote'; ////////////////////////////////////////////////////////////////////////// }}}1 -// Test for Firefox // {{{1 +// Cross-browser error handling, and browser tests // {{{1 // Not sure if I need this, but I'm playing it safe for now. Firefox returns // null rather than undefined in chrome.runtime.lastError when there is // no error. This is to test for null in Firefox without changing my @@ -54,6 +54,27 @@ BROWSER_TYPE=null; // unknown } })(window); +/// Return a string representation of chrome.runtime.lastError. +/// +/// Somewhere around Chrome 75, chrome.runtime.lastError lost toString(), +/// as far as I can tell. +/// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error +/// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/lastError +/// https://developer.chrome.com/extensions/runtime#property-lastError +function lastBrowserErrorMessageString() { + if(!chrome.runtime.lastError) { + return ""; + } + + if(typeof chrome.runtime.lastError !== 'object') { + return `${chrome.runtime.lastError}`; + } + + return chrome.runtime.lastError.message || + chrome.runtime.lastError.description || + chrome.runtime.lastError.toString(); +} + ////////////////////////////////////////////////////////////////////////// }}}1 // DOM-related functions // {{{1