Skip to content

Commit

Permalink
chore: codefactor search-engine
Browse files Browse the repository at this point in the history
  • Loading branch information
margunwa123 committed Sep 26, 2022
1 parent af30687 commit c879ad5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 41 deletions.
7 changes: 4 additions & 3 deletions src/options_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,10 @@ class OptionsPageManager {
return permissions.origins.includes(url);
});
const stackoverflow = document.getElementById('stackoverflow');
stackoverflow.checked = OPTIONAL_PERMISSIONS_URLS['stackoverflow'].every((url) => {
return permissions.origins.includes(url);
});
stackoverflow.checked =
OPTIONAL_PERMISSIONS_URLS['stackoverflow'].every((url) => {
return permissions.origins.includes(url);
});
}

// Load options from browser.storage.sync to the DOM.
Expand Down
45 changes: 7 additions & 38 deletions src/search_engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ class GoogleSearch {
observer.observe(container, {
attributes: false,
childList: true,
subtree: false,
subtree: true,
});
}

Expand Down Expand Up @@ -1365,46 +1365,15 @@ class StackOverflow {
);
}

onChangedResults(callback) {
const SO_CONTAINER_SELECTOR = '.js-search-results';
const resultsObserver = new MutationObserver(
async (mutationsList, observer) => {
callback(true);
},
);
let lastLoadedURL = null;
const pageObserver = new MutationObserver(
async (mutationsList, observer) => {
const url = window.location.pathname + window.location.search;
if (url === lastLoadedURL) {
return;
}

resultsObserver.disconnect();

const container = document.querySelector(SO_CONTAINER_SELECTOR);
if (!container) {
return;
}

lastLoadedURL = url;
callback(false);

resultsObserver.observe(container, {
attributes: false,
childList: true,
subtree: true,
});
},
);
}

getSearchResults() {
const postSelector = 'div.s-post-summary'
const linkSelector = 'a[href*="/questions"]:not([href*="/questions/tagged"])'
const resultSelector = `${postSelector} ${linkSelector}`;
const includedElements = [
// Question search result
{
nodes: document.querySelectorAll('div.s-post-summary a[href*="/questions"]:not([href*="/questions/tagged"])'),
highlightClass: 'div.s-post-summary'
nodes: document.querySelectorAll(resultSelector),
highlightClass: 'div.s-post-summary',
},
];
return getSortedSearchResults(includedElements, []);
Expand Down Expand Up @@ -1457,7 +1426,7 @@ const getSearchEngine = (options) => {
new GoogleScholar(options),
new Amazon(options),
new Github(options),
new StackOverflow(options)
new StackOverflow(options),
];
// Switch over all compatible search engines
const href = window.location.href;
Expand Down

0 comments on commit c879ad5

Please sign in to comment.