Skip to content

Commit

Permalink
Fixes: module "process"; chrome.runtime.lastError
Browse files Browse the repository at this point in the history
- Added a 'require' to pull process into the background page.  May fix
  the issue identified at
  #100 (comment) .
- More sophisticated stringification of chrome.runtime.lastError.
  Somewhere around Chrome 75, chrome.runtime.lastError lost toString(),
  as far as I can tell.
  • Loading branch information
Christopher White committed Jul 31, 2019
1 parent 78204fd commit b8ee6e4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
9 changes: 8 additions & 1 deletion app/bg/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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});
}
Expand Down
12 changes: 6 additions & 6 deletions app/win/main_tl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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!");

Expand Down Expand Up @@ -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:');
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)});
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 22 additions & 1 deletion vendor/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "<no error>";
}

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

Expand Down

0 comments on commit b8ee6e4

Please sign in to comment.