Incorporate feedback from Osmose.
Created a 'utils.js' file that is imported into the content script 'product_info.js'. These are now bundled by webpack into a single script.
This commit is contained in:
Родитель
bb1c30c7ad
Коммит
a4fe7941bd
|
@ -1 +1,2 @@
|
|||
build
|
||||
src/product_info.js
|
||||
|
|
|
@ -23,7 +23,7 @@
|
|||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["product_info.js"]
|
||||
"js": ["product_info.bundle.js"]
|
||||
}
|
||||
],
|
||||
"sidebar_action": {
|
||||
|
|
|
@ -1,66 +1,55 @@
|
|||
import utils from './utils';
|
||||
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
const OpenGraphPropertyValues = {
|
||||
title: 'og:title',
|
||||
image: 'og:image',
|
||||
price: 'og:price:amount',
|
||||
};
|
||||
(async function main() {
|
||||
const OpenGraphPropertyValues = {
|
||||
title: 'og:title',
|
||||
image: 'og:image',
|
||||
price: 'og:price:amount',
|
||||
};
|
||||
|
||||
function openBackgroundPort() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const port = browser.runtime.connect();
|
||||
port.onMessage.addListener((message) => {
|
||||
if (message.type === 'background-ready') {
|
||||
resolve(port);
|
||||
function openBackgroundPort() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const port = browser.runtime.connect();
|
||||
port.onMessage.addListener((message) => {
|
||||
if (message.type === 'background-ready') {
|
||||
resolve(port);
|
||||
}
|
||||
});
|
||||
port.onDisconnect.addListener(() => {
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getProductData(port) {
|
||||
const data = {};
|
||||
for (const [key, value] of Object.entries(OpenGraphPropertyValues)) {
|
||||
const metaEle = document.querySelector(`meta[property="${value}"]`);
|
||||
if (metaEle) {
|
||||
data[key] = metaEle.getAttribute('content');
|
||||
}
|
||||
});
|
||||
port.onDisconnect.addListener(() => {
|
||||
reject();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function getProductData(port) {
|
||||
const data = {};
|
||||
for (const [key, value] of Object.entries(OpenGraphPropertyValues)) {
|
||||
const metaEle = document.querySelector(`meta[property="${value}"]`);
|
||||
if (metaEle) {
|
||||
data[key] = metaEle.getAttribute('content');
|
||||
}
|
||||
}
|
||||
port.postMessage({
|
||||
type: 'product-data',
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
function portSuccess(port) {
|
||||
// Make sure page has finished loading, as JS could alter the DOM.
|
||||
if (document.readyState === 'complete') {
|
||||
getProductData(port);
|
||||
} else {
|
||||
window.addEventListener('load', () => {
|
||||
getProductData(port);
|
||||
port.postMessage({
|
||||
type: 'product-data',
|
||||
data,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let delay = 2; // seconds
|
||||
const DELAY_MULTIPLIER = 2;
|
||||
const NUM_ATTEMPTS = 5;
|
||||
const DELAY_MAX = delay ** NUM_ATTEMPTS;
|
||||
// Connect to the background script until it's available. See Issue #17 and bug 1474727.
|
||||
function portFail() {
|
||||
if (delay > DELAY_MAX) {
|
||||
console.error(`Could not establish a connection after ${NUM_ATTEMPTS} attempts.`);
|
||||
return;
|
||||
try {
|
||||
const port = await utils.retry(openBackgroundPort);
|
||||
// Make sure page has finished loading, as JS could alter the DOM.
|
||||
if (document.readyState === 'complete') {
|
||||
getProductData(port);
|
||||
} else {
|
||||
window.addEventListener('load', () => {
|
||||
getProductData(port);
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Could not establish connection to background script.');
|
||||
}
|
||||
setTimeout(() => {
|
||||
openBackgroundPort().then(portSuccess, portFail);
|
||||
delay *= DELAY_MULTIPLIER;
|
||||
}, delay * 1000); // ms
|
||||
}
|
||||
|
||||
openBackgroundPort().then(portSuccess, portFail);
|
||||
}());
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
const utils = {
|
||||
/* eslint-disable arrow-body-style */
|
||||
wait: async (delay) => {
|
||||
return new Promise((resolve) => {
|
||||
window.setTimeout(resolve, delay);
|
||||
});
|
||||
},
|
||||
|
||||
retry: async (callback, maxRetries = 5, delayFactor = 2, initialDelay = 2) => {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
let delay = initialDelay;
|
||||
let lastError = null;
|
||||
for (let k = 0; k < maxRetries; k++) {
|
||||
try {
|
||||
// If we don't await here, the callback's promise will be returned
|
||||
// immediately instead of potentially failing.
|
||||
return await callback();
|
||||
} catch (err) {
|
||||
lastError = err;
|
||||
await utils.wait(delay);
|
||||
delay *= delayFactor;
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
},
|
||||
};
|
||||
|
||||
export default utils;
|
|
@ -13,6 +13,7 @@ module.exports = {
|
|||
entry: {
|
||||
background: './src/background.js',
|
||||
sidebar: './src/sidebar.jsx',
|
||||
product_info: './src/product_info.js',
|
||||
},
|
||||
output: {
|
||||
path: BUILD_DIR,
|
||||
|
@ -31,8 +32,6 @@ module.exports = {
|
|||
},
|
||||
plugins: [
|
||||
new CopyWebpackPlugin([
|
||||
{from: 'product_info.js'},
|
||||
|
||||
// Static files
|
||||
{from: '**/*.svg'},
|
||||
{from: '**/*.html'},
|
||||
|
|
Загрузка…
Ссылка в новой задаче