Skip to content

Commit

Permalink
Extension fetches data via Service Worker and message passing
Browse files Browse the repository at this point in the history
  • Loading branch information
rarous committed Feb 6, 2025
1 parent 416bd91 commit 5b377fb
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 86 deletions.
36 changes: 0 additions & 36 deletions extension/background.js

This file was deleted.

45 changes: 20 additions & 25 deletions extension/content.mjs

Large diffs are not rendered by default.

32 changes: 23 additions & 9 deletions extension/helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import { cleanPriceText, cleanUnitPriceText } from "@hlidac-shopu/lib/parse.mjs";
import { shopName, shops as shops_lib } from "@hlidac-shopu/lib/shops.mjs";

/**
*
* @param {Element|string} s DOM element or CSS selector of the element
* @returns {string|null}
*/
export function cleanPrice(s) {
const el = typeof s === "string" ? document.querySelector(s) : s;
if (!el) return null;
let priceText = el.textContent;
return cleanPriceText(priceText);
}
//Check if price is per unit or per weight

/**
* Check if the price is per unit or per weight
* @param {Element|string} s DOM element or CSS selector of the element
* @returns {boolean|null}
*/
export function isUnitPrice(s) {
const el = typeof s === "string" ? document.querySelector(s) : s;
if (!el) return null;
return el.textContent.includes("/kg");
}
//Get price for product weight from price per 1 Kg

/**
* Get price for product weight from price per 1 Kg
* @param {Element|string} s DOM element or CSS selector of the element
* @param {number} quantity
* @returns {number|null}
*/
export function cleanUnitPrice(s, quantity) {
const el = typeof s === "string" ? document.querySelector(s) : s;
if (!el) return null;
Expand Down Expand Up @@ -45,18 +61,16 @@ export function getItemIdFromUrl(url) {
* @returns {boolean}
*/
export function isElementVisible(el) {
if (el == document) return true;
if (el === document) return true;

if (!el) return false;
if (!el.parentNode) return false;
if (el.style) {
if (el.style.display == "none") return false;
if (el.style.visibility == "hidden") return false;
}
if (el.style?.display === "none") return false;
if (el.style?.visibility === "hidden") return false;

const style = window.getComputedStyle(el, "");
if (style.display == "none") return false;
if (style.visibility == "hidden") return false;
if (style.display === "none") return false;
if (style.visibility === "hidden") return false;

return isElementVisible(el.parentNode);
}
Expand Down
14 changes: 4 additions & 10 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"author": "TopMonks s.r.o., Apify Technologies s.r.o., Keboola Czech s.r.o. & contributors",
"homepage_url": "https://www.hlidacshopu.cz/",
"background": {
"service_worker": "background.js"
"scripts": ["sw.js"],
"service_worker": "sw.js"
},
"icons": {
"128": "icons/favicon-128x128.png",
Expand Down Expand Up @@ -40,9 +41,7 @@
}
]
},
"host_permissions": [
"https://api.hlidacshopu.cz/*"
],
"host_permissions": ["https://api.hlidacshopu.cz/*"],
"content_scripts": [
{
"matches": [
Expand Down Expand Up @@ -94,15 +93,10 @@
"https://www.tetadrogerie.cz/*",
"https://www.tsbohemia.cz/*"
],
"js": [
"content.js"
],
"js": ["content.js"],
"run_at": "document_idle"
}
],
"content_security_policy": {
"extension_pages": "default-src 'self'"
},
"browser_specific_settings": {
"gecko": {
"id": "{d6f0f975-91a3-4d78-96f7-5f1859ad18b6}",
Expand Down
31 changes: 31 additions & 0 deletions extension/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

const { runtime } = chrome || browser;

function getVersion() {
return runtime.getManifest().version;
}

function fetchData(url, info) {
const searchString = new URLSearchParams(Object.entries(info).filter(([, val]) => Boolean(val)));
searchString.append("url", url);
searchString.append("ext", getVersion());
console.log(searchString.toString());
return fetch(`https://api.hlidacshopu.cz/v2/detail?${searchString}`).then(resp => {
if (resp.status === 404) {
return resp.json();
}
if (!resp.ok) {
throw new Error("HTTP error, status = " + resp.status);
}
return resp.json();
});
}

runtime.onMessage.addListener(({ name, options }, sender, sendResponse) => {
if (name === "hs-detail") {
fetchData(options.url, options.info).then(sendResponse);
return true;
}
return false;
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@
"lint:firefox": "web-ext lint --config web-ext-config.firefox.mjs",
"build:extension": "node ./scripts/build-extension.mjs",
"watch:extension": "node ./scripts/watch-extension.mjs",
"build:firefox:prepare": "cp -R ./extension/ ./extension-dist/; cat ./extension/manifest.json | jq 'del(.background)' > ./extension-dist/manifest.json",
"build:firefox:prepare": "cp -R ./extension/ ./extension-dist/",
"build:firefox:build": "web-ext build --config web-ext-config.firefox.mjs --overwrite-dest",
"build:firefox:rename": "node scripts/rename.mjs dist/*.zip 'hl_da_shop_(.*)' 'firefox-extension%1'",
"build:firefox": "run-s build:firefox:prepare build:firefox:build build:firefox:rename",
"start:firefox": "web-ext run --config web-ext-config.firefox.mjs --browser-console",
"start:firefox": "web-ext run --config web-ext-config.firefox.mjs --browser-console --devtools",
"publish:firefox": "web-ext-submit --channel=listed --config web-ext-config.firefox.mjs",
"build:chrome:build": "web-ext build --overwrite-dest",
"build:chrome:rename": "node scripts/rename.mjs dist/*.zip 'hl_da_shop_(.*)' 'chrome-extension%1'",
"build:chrome": "run-s build:chrome:build build:chrome:rename",
"start:chrome": "web-ext run --target=chromium",
"start:chrome": "web-ext run --target=chromium --devtools",
"publish:chrome": "chrome-webstore-upload upload --auto-publish --extension-id=plmlonggbfebcjelncogcnclagkmkikk --source=dist/chrome-extension-$(PRINT=manifest ./version.sh).zip",
"start:safari": "open 'apple/App.xcodeproj'",
"start:www.hlidacshopu.cz": "BLENDID_CONFIG_PATH=./www.hlidacshopu.cz/config/ dotenv blendid",
Expand Down
3 changes: 2 additions & 1 deletion scripts/build-extension.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ esbuild.buildSync({
target: ["es2017", "firefox57", "safari12"],
charset: "utf8",
bundle: true,
outfile: output
outfile: output,
sourcemap: "linked"
});
2 changes: 1 addition & 1 deletion web-ext-config.firefox.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
sourceDir: "./extension-dist",
ignoreFiles: ["README.md", "background.js", "**/*.mjs"]
ignoreFiles: ["README.md", "**/*.mjs"]
};
2 changes: 1 addition & 1 deletion web-ext-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export default {
artifactsDir: "./dist",
ignoreFiles: ["README.md", "**/*.mjs"],
run: {
startUrl: ["https://www.alza.cz/trhakdne"]
startUrl: ["https://www.alza.cz/"]
}
};

0 comments on commit 5b377fb

Please sign in to comment.