Skip to content

Commit

Permalink
fix: ensure background page responds to message
Browse files Browse the repository at this point in the history
Fixes #184.
  • Loading branch information
dessant committed Apr 5, 2022
1 parent 1cbcdb1 commit 5352e2a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,11 @@ async function processMessage(request, sender) {

browser.tabs.sendMessage(...params);
} else if (request.id === 'sendNativeMessage') {
return browser.runtime.sendNativeMessage('application.id', request.message);
const response = await browser.runtime.sendNativeMessage(
'application.id',
request.message
);
return Promise.resolve({response});
} else if (request.id === 'getPlatform') {
return getPlatform({fallback: false});
} else if (request.id === 'storageRequest') {
Expand Down
34 changes: 30 additions & 4 deletions src/share/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import browser from 'webextension-polyfill';
import {validate as uuidValidate} from 'uuid';

async function processIncomingShare(shareId) {
const response = await browser.runtime.sendMessage({
id: 'sendNativeMessage',
message: {id: 'getShareId'}
function getShareId() {
// Safari: message may be sent before the background page
// event listener has been initialized on browser start
return new Promise((resolve, reject) => {
let stop;

const sendMessage = async function () {
const data = await browser.runtime.sendMessage({
id: 'sendNativeMessage',
message: {id: 'getShareId'}
});
if (data) {
window.clearTimeout(timeoutId);
resolve(data.response);
} else if (stop) {
reject(new Error('Background page is not ready'));
} else {
window.setTimeout(sendMessage, 30);
}
};

const timeoutId = window.setTimeout(function () {
stop = true;
}, 60000); // 1 minute

sendMessage();
});
}

async function processIncomingShare(shareId) {
const response = await getShareId();

if (response && response.shareId === shareId) {
const tabUrl = `${browser.runtime.getURL(
Expand Down

0 comments on commit 5352e2a

Please sign in to comment.