Skip to content

Commit

Permalink
fix: update Reddit Repost Sleuth
Browse files Browse the repository at this point in the history
Closes #272.
  • Loading branch information
dessant committed Oct 9, 2023
1 parent 3a9ac9e commit ff9fe18
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/engines/repostSleuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand Down
19 changes: 13 additions & 6 deletions src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ff9fe18

Please sign in to comment.