Skip to content

Commit

Permalink
Fix mozilla#237, fix mozilla#242: Re-add toolbar icon reset workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Kelly committed Nov 5, 2018
1 parent d07b508 commit 4f26c96
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ import {registerEvents, handleWidgetRemoved} from 'commerce/telemetry/extension'
['blocking', 'responseHeaders'],
);

// Workaround for bug 1493470: Resend product info to the background
// script in case subframe loads clear the toolbar icon.
// TODO(osmose): Remove once Firefox 64 hits the release channel.
browser.webRequest.onCompleted.addListener(
(details) => {
if (details.tabId) {
browser.tabs.sendMessage(details.tabId, {type: 'resend-product'});
}
},
{urls: ['https://*/*', 'http://*/*'], types: ['sub_frame']},
);

// Set up listener to trigger re-extraction when a page changes the URL via
// the history API.
browser.webNavigation.onHistoryStateUpdated.addListener(
Expand Down
16 changes: 8 additions & 8 deletions src/extraction/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function extractProduct() {
}

async function sendProductToBackground(extractedProduct) {
if (!extractedProduct) {
return null;
}

return browser.runtime.sendMessage({
type: 'extracted-product',
extractedProduct: {
Expand Down Expand Up @@ -109,18 +113,14 @@ async function attemptExtraction() {
extractedProduct = await attemptExtraction();
});

// Messy workaround for bug 1493470: Resend product info to the background
// script twice in case subframe loads clear the toolbar icon.
// TODO(osmose): Remove once Firefox 64 hits the release channel.
const resend = () => sendProductToBackground(extractedProduct);
setTimeout(resend, 5000);
setTimeout(resend, 10000);

browser.runtime.onMessage.addListener(async (message) => {
// Re-extract the product if requested
if (message.type === 'reextract-product') {
// Re-extract the product if requested
extractedProduct = await attemptExtraction();
await sendProductToBackground(extractedProduct);
} else if (message.type === 'resend-product') {
// Re-send the already-extracted product if requested
await sendProductToBackground(extractedProduct);
}
});
}());

0 comments on commit 4f26c96

Please sign in to comment.