diff --git a/.eslintrc.json b/.eslintrc.json index b933648..caab1f9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -6,7 +6,6 @@ "rules": { "max-statements-per-line": [2, {"max": 1}], "no-use-before-define": [2, "nofunc"], - "semi": [2, "always"], "no-console": ["error", {"allow": ["warn", "error"]}] }, "extends": [ diff --git a/.gitignore b/.gitignore index d8992fd..da3fff1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules web-ext-artifacts build +chrome *.zip *.xpi diff --git a/package.json b/package.json index 0196157..9c183f7 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "lint": "eslint --ext .js,.mjs . --cache", "test": "npm run lint && npm run build && web-ext lint", "start": "web-ext run", + "start-chrome": "web-ext run --target chromium", "build": "sync-version src/static/manifest.json && shx rm -rf build/* && rollup -c", "build-locales": "tx pull -a --use-git-timestamps && webext-tx-fix -s src/static", "build-artifact": "shx rm -rf web-ext-artifacts/* && web-ext build --ignore-files **/*.js.map", @@ -19,7 +20,8 @@ "changelog": "shx cat README.md | mkcat | mkql \"[content=Changelog] + ul > :first-child > *\" | mkout" }, "eslintIgnore": [ - "build" + "build", + "chrome" ], "devDependencies": { "@eight04/idb-storage": "^0.4.2", diff --git a/src/background.js b/src/background.js index 16e6a55..35317b3 100644 --- a/src/background.js +++ b/src/background.js @@ -255,59 +255,53 @@ function createDynamicIcon({file, enabled, onupdate}) { return {update}; } -function pickImages(tabId, frameId = 0) { +async function pickImages(tabId, frameId = 0) { const result = { tabId, frames: [], env: null }; - return Promise.all([ + await Promise.all([ getImages(), getEnv(), pref.get("collectFromFrames") && getImagesFromChildFrames() - ]).then(() => { - // make sure the main frame is the first frame - const index = result.frames.findIndex(f => f.frameId === frameId); - if (index !== 0) { - const mainFrame = result.frames[index]; - result.frames[index] = result.frames[0]; - result.frames[0] = mainFrame; - } - return result; - }); + ]); + // make sure the main frame is the first frame + const index = result.frames.findIndex(f => f.frameId === frameId); + if (index !== 0) { + const mainFrame = result.frames[index]; + result.frames[index] = result.frames[0]; + result.frames[0] = mainFrame; + } + return result; - function getImages() { - return browser.tabs.sendMessage(tabId, {method: "getImages"}, {frameId}) - .then(images => { - result.frames.push({ - frameId, - images - }); - }); + async function getImages() { + const images = await browser.tabs.sendMessage(tabId, {method: "getImages"}, {frameId}) + result.frames.push({ + frameId, + images + }); } - function getEnv() { - return browser.tabs.sendMessage(tabId, {method: "getEnv"}, {frameId}) - .then(env => { - result.env = env; - }); + async function getEnv() { + const env = await browser.tabs.sendMessage(tabId, {method: "getEnv"}, {frameId}) + result.env = env; } - function getImagesFromChildFrames() { - return getChildFrames() - .then(frameIds => - Promise.all(frameIds.map(frameId => - browser.tabs.sendMessage(tabId, {method: "getImages"}, {frameId}) - .then(images => { - result.frames.push({ - frameId, - images - }); - }) - // https://github.com/eight04/image-picka/issues/100 - .catch(console.warn) - )) - ); + async function getImagesFromChildFrames() { + const frameIds = await getChildFrames(); + await Promise.all(frameIds.map(frameId => async () => { + try { + const images = await browser.tabs.sendMessage(tabId, {method: "getImages"}, {frameId}); + result.frames.push({ + frameId, + images + }); + } catch (err) { + // https://github.com/eight04/image-picka/issues/100 + console.warn(err); + } + })); } function getChildFrames() { diff --git a/src/lib/image-util.js b/src/lib/image-util.js index 9a21059..b8e1bfa 100644 --- a/src/lib/image-util.js +++ b/src/lib/image-util.js @@ -19,6 +19,9 @@ function update() { function getSrcFromSrcset(set) { const rules = parseSrcset(set); + if (!rules.length) { + return null; + } let maxRule; for (const rule of rules) { // FIXME: what if the rules have both density and width? @@ -39,7 +42,11 @@ export function getSrc(img) { // prefer srcset first // https://www.harakis.com/hara-elite/large-2br-apartment-gallery/ if (img.srcset) { - return (getSrcFromSrcset(img.srcset)); + try { + return getSrcFromSrcset(img.srcset); + } catch (err) { + console.warn("Error parsing srcset", img.srcset); + } } if (img.src) { return img.src; @@ -97,11 +104,24 @@ function getSrcFromPicture(el) { source = s; break; } - if (source) return getSrcFromSrcset(source.srcset); + if (source && source.srcset) { + try { + return getSrcFromSrcset(source.srcset); + } catch (err) { + console.warn("Error parsing srcset", source.srcset); + } + } const img = el.querySelector("img"); if (img) return getSrc(img); if (allSources.length) { - return getSrcFromSrcset(allSources[allSources.length - 1].srcset); + const srcset = allSources[allSources.length - 1].srcset + if (srcset) { + try { + return getSrcFromSrcset(srcset); + } catch (err) { + console.warn("Error parsing srcset", srcset); + } + } } }