Browse Source

fix: ignore errors for browser.runtime.sendMessage

Gerald 7 years ago
parent
commit
103f56f1a2

+ 4 - 4
src/background/app.js

@@ -8,7 +8,7 @@ import {
   newScript, parseMeta,
   setClipboard, checkUpdate,
   getOption, setOption, hookOptions, getAllOptions,
-  initialize,
+  initialize, sendMessageOrIgnore,
 } from './utils';
 import { tabOpen, tabClose } from './utils/tabs';
 import createNotification from './utils/notifications';
@@ -27,7 +27,7 @@ hookOptions(changes => {
   if ('isApplied' in changes) setIcon(changes.isApplied);
   if ('autoUpdate' in changes) autoUpdate();
   if ('showBadge' in changes) updateBadges();
-  browser.runtime.sendMessage({
+  sendMessageOrIgnore({
     cmd: 'UpdateOptions',
     data: changes,
   });
@@ -99,7 +99,7 @@ const commands = {
     })
     .then(([script]) => {
       sync.sync();
-      browser.runtime.sendMessage({
+      sendMessageOrIgnore({
         cmd: 'UpdateScript',
         data: {
           where: { id: script.props.id },
@@ -137,7 +137,7 @@ const commands = {
   Vacuum: vacuum,
   ParseScript(data) {
     return parseScript(data).then(res => {
-      browser.runtime.sendMessage(res);
+      sendMessageOrIgnore(res);
       sync.sync();
       return res.data;
     });

+ 3 - 3
src/background/sync/base.js

@@ -1,6 +1,6 @@
 import { debounce, normalizeKeys, request, noop } from 'src/common';
 import { objectGet, objectSet, objectPick, objectPurify } from 'src/common/object';
-import { getEventEmitter, getOption, setOption, hookOptions } from '../utils';
+import { getEventEmitter, getOption, setOption, hookOptions, sendMessageOrIgnore } from '../utils';
 import {
   getScripts,
   getScriptCode,
@@ -173,7 +173,7 @@ function extendService(options) {
 }
 
 const onStateChange = debounce(() => {
-  browser.runtime.sendMessage({
+  sendMessageOrIgnore({
     cmd: 'UpdateSync',
     data: getStates(),
   });
@@ -435,7 +435,7 @@ export const BaseService = serviceFactory({
               delete data.config.enabled;
             }
             return parseScript(data)
-            .then(res => { browser.runtime.sendMessage(res); });
+            .then(res => { sendMessageOrIgnore(res); });
           });
         }),
         ...putRemote.map(({ local, remote }) => {

+ 3 - 2
src/background/utils/db.js

@@ -5,6 +5,7 @@ import { testScript, testBlacklist } from './tester';
 import { register } from './init';
 import patchDB from './patch-db';
 import { setOption } from './options';
+import { sendMessageOrIgnore } from '.';
 
 function cacheOrFetch(handle) {
   const requests = {};
@@ -210,7 +211,7 @@ export function sortScripts() {
   });
   return normalizePosition()
   .then(changed => {
-    browser.runtime.sendMessage({ cmd: 'ScriptsUpdated' });
+    sendMessageOrIgnore({ cmd: 'ScriptsUpdated' });
     return changed;
   });
 }
@@ -356,7 +357,7 @@ export function removeScript(id) {
     storage.code.remove(id);
     storage.value.remove(id);
   }
-  browser.runtime.sendMessage({
+  sendMessageOrIgnore({
     cmd: 'RemoveScript',
     data: id,
   });

+ 4 - 0
src/background/utils/index.js

@@ -29,3 +29,7 @@ export function broadcast(data) {
     });
   });
 }
+
+export function sendMessageOrIgnore(...args) {
+  return browser.runtime.sendMessage(...args).catch(noop);
+}

+ 8 - 8
src/background/utils/update.js

@@ -2,7 +2,7 @@ import { i18n, request } from 'src/common';
 import { parseScript } from './db';
 import { parseMeta, compareVersion } from './script';
 import { getOption } from './options';
-import { notify } from '.';
+import { notify, sendMessageOrIgnore } from '.';
 
 const processes = {};
 
@@ -30,34 +30,34 @@ function doCheckUpdate(script) {
     if (compareVersion(script.meta.version, meta.version) < 0) return Promise.resolve();
     update.checking = false;
     update.message = i18n('msgNoUpdate');
-    browser.runtime.sendMessage(res);
+    sendMessageOrIgnore(res);
     return Promise.reject();
   };
   const errHandler = () => {
     update.checking = false;
     update.message = i18n('msgErrorFetchingUpdateInfo');
-    browser.runtime.sendMessage(res);
+    sendMessageOrIgnore(res);
     return Promise.reject();
   };
   const doUpdate = () => {
     if (!downloadURL) {
       update.message = `<span class="new">${i18n('msgNewVersion')}</span>`;
-      browser.runtime.sendMessage(res);
+      sendMessageOrIgnore(res);
       return Promise.reject();
     }
     update.message = i18n('msgUpdating');
-    browser.runtime.sendMessage(res);
+    sendMessageOrIgnore(res);
     return request(downloadURL)
     .then(({ data }) => data, () => {
       update.checking = false;
       update.message = i18n('msgErrorFetchingScript');
-      browser.runtime.sendMessage(res);
+      sendMessageOrIgnore(res);
       return Promise.reject();
     });
   };
   if (!updateURL) return Promise.reject();
   update.message = i18n('msgCheckingForUpdate');
-  browser.runtime.sendMessage(res);
+  sendMessageOrIgnore(res);
   return request(updateURL, {
     headers: {
       Accept: 'text/x-userscript-meta',
@@ -80,7 +80,7 @@ export default function checkUpdate(script) {
     .then(res => {
       const { data: { update } } = res;
       update.checking = false;
-      browser.runtime.sendMessage(res);
+      sendMessageOrIgnore(res);
       updated = true;
       if (getOption('notifyUpdates')) {
         notify({