Bläddra i källkod

fix: retry bg connection on startup in options.html (#653)

tophf 6 år sedan
förälder
incheckning
028d0655e5
4 ändrade filer med 26 tillägg och 18 borttagningar
  1. 21 1
      src/common/index.js
  2. 1 1
      src/common/options.js
  3. 3 15
      src/injected/content/index.js
  4. 1 1
      src/options/index.js

+ 21 - 1
src/common/index.js

@@ -1,3 +1,5 @@
+import { noop } from './util';
+
 export * from './util';
 
 export function i18n(name, args) {
@@ -31,7 +33,8 @@ export function initHooks() {
   return { hook, fire };
 }
 
-export function sendMessage(payload) {
+export function sendMessage(payload, { retry } = {}) {
+  if (retry) return sendMessageRetry(payload);
   const promise = browser.runtime.sendMessage(payload)
   .then((res) => {
     const { data, error } = res || {};
@@ -44,6 +47,23 @@ export function sendMessage(payload) {
   return promise;
 }
 
+/**
+ * The active tab page and its [content] scripts load before the extension's
+ * persistent background script when Chrome starts with a URL via command line
+ * or when configured to restore the session, https://crbug.com/314686
+ */
+export async function sendMessageRetry(payload, retries = 10) {
+  const makePause = ms => new Promise(resolve => setTimeout(resolve, ms));
+  let pauseDuration = 10;
+  for (; retries > 0; retries -= 1) {
+    const data = await sendMessage(payload).catch(noop);
+    if (data) return data;
+    await makePause(pauseDuration);
+    pauseDuration *= 2;
+  }
+  throw new Error('Violentmonkey cannot connect to the background page.');
+}
+
 export function leftpad(input, length, pad = '0') {
   let num = input.toString();
   while (num.length < length) num = `${pad}${num}`;

+ 1 - 1
src/common/options.js

@@ -3,7 +3,7 @@ import { objectGet, objectSet } from './object';
 
 let options = {};
 const hooks = initHooks();
-const ready = sendMessage({ cmd: 'GetAllOptions' })
+const ready = sendMessage({ cmd: 'GetAllOptions' }, { retry: true })
 .then((data) => {
   options = data;
   if (data) hooks.fire(data);

+ 3 - 15
src/injected/content/index.js

@@ -2,7 +2,7 @@ import { getUniqId } from '#/common';
 import { INJECT_PAGE, INJECT_CONTENT } from '#/common/consts';
 import { bindEvents, sendMessage } from '../utils';
 import {
-  setJsonDump, objectKeys, filter, forEach, includes, append, createElement, setAttribute, noop,
+  setJsonDump, objectKeys, filter, forEach, includes, append, createElement, setAttribute,
 } from '../utils/helpers';
 import bridge from './bridge';
 import './clipboard';
@@ -22,25 +22,13 @@ export default async function initialize(contentId, webId) {
   bridge.post = bindEvents(contentId, webId, bridge.onHandle);
   bridge.destId = webId;
   setJsonDump({ native: true });
-  // The active web page tab and its content scripts load before the background script
-  // when Chrome starts with a URL (via command line or when configured to restore the session).
-  const makePause = ms => new Promise(resolve => setTimeout(resolve, ms));
-  let pauseDuration = 10;
-  let data;
-  const msg = {
+  const data = await sendMessage({
     cmd: 'GetInjected',
     data: {
       url: window.location.href,
       reset: IS_TOP,
     },
-  };
-  for (let retries = 10; retries > 0; retries -= 1) {
-    data = await sendMessage(msg).catch(noop);
-    if (data) break;
-    await makePause(pauseDuration);
-    pauseDuration *= 2;
-  }
-  if (!data) throw new Error('Violentmonkey cannot connect to the background page.');
+  }, { retry: true });
   const scriptLists = triageScripts(data);
   getPopup();
   setBadge();

+ 1 - 1
src/options/index.js

@@ -50,7 +50,7 @@ function initScript(script) {
 }
 
 function loadData() {
-  sendMessage({ cmd: 'GetData' })
+  sendMessage({ cmd: 'GetData' }, { retry: true })
   .then((data) => {
     const oldCache = store.cache || {};
     store.cache = data.cache;