diff --git a/src/engines/repostSleuth.js b/src/engines/repostSleuth.js index 7bf2030..5677006 100644 --- a/src/engines/repostSleuth.js +++ b/src/engines/repostSleuth.js @@ -14,7 +14,10 @@ async function search({session, search, image, storageIds}) { input.dispatchEvent(new Event('change')); } else { - const input = await findNode('input[placeholder="https://redd.it/xyz"]'); + const input = await findNode( + '//input[preceding-sibling::label[contains(., "Image URL")]]', + {selectorType: 'xpath'} + ); input.value = image.imageUrl; @@ -23,7 +26,11 @@ async function search({session, search, image, storageIds}) { input.dispatchEvent(new Event('input')); } - (await findNode('main button.primary')).click(); + ( + await findNode('.v-main button.primary:not(.v-btn--disabled)', { + observerOptions: {attributes: true, attributeFilter: ['class']} + }) + ).click(); } function init() { diff --git a/src/utils/common.js b/src/utils/common.js index 83f9049..d03096e 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -248,7 +248,9 @@ function filenameToFileExt(name) { return (/(?:\.([^.]+))?$/.exec(name)[1] || '').toLowerCase(); } -function querySelectorXpath(rootNode, selector) { +function querySelectorXpath(selector, {rootNode = null} = {}) { + rootNode = rootNode || document; + return document.evaluate( selector, rootNode, @@ -258,10 +260,15 @@ function querySelectorXpath(rootNode, selector) { ).singleNodeValue; } -function nodeQuerySelector(rootNode, selector, {selectorType = 'css'} = {}) { +function nodeQuerySelector( + selector, + {rootNode = null, selectorType = 'css'} = {} +) { + rootNode = rootNode || document; + return selectorType === 'css' ? rootNode.querySelector(selector) - : querySelectorXpath(rootNode, selector); + : querySelectorXpath(selector, {rootNode}); } function findNode( @@ -277,14 +284,14 @@ function findNode( return new Promise((resolve, reject) => { rootNode = rootNode || document; - const el = nodeQuerySelector(rootNode, selector, {selectorType}); + const el = nodeQuerySelector(selector, {rootNode, selectorType}); if (el) { resolve(el); return; } const observer = new MutationObserver(function (mutations, obs) { - const el = nodeQuerySelector(rootNode, selector, {selectorType}); + const el = nodeQuerySelector(selector, {rootNode, selectorType}); if (el) { obs.disconnect(); window.clearTimeout(timeoutId); @@ -338,7 +345,7 @@ async function processNode( if (reprocess) { const observer = new MutationObserver(function (mutations, obs) { - const el = nodeQuerySelector(rootNode, selector, {selectorType}); + const el = nodeQuerySelector(selector, {rootNode, selectorType}); if (el && !el.isSameNode(node)) { node = el; actionFn(node);