Skip to content

Commit

Permalink
Remove "People also ask" results from navigable results.
Browse files Browse the repository at this point in the history
This should resolve #79.
Also, scroll to the top of the page when trying to navigate up from the
first result. This makes it easier to see search result cards when
navigating down and then back up.
  • Loading branch information
infokiller committed Oct 6, 2018
1 parent bd9b9ff commit 1ed0aac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
6 changes: 5 additions & 1 deletion assets/chrome_webstore_description.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
New in 0.3.2
* Scroll to top of the page when navigating to the "previous" result from the
first result and wrapping is off.
* Remove "People also ask" results from navigable results.

New in 0.3.1
* Fixed unpredictable scrolling in results navigation.
See also https://github.com/infokiller/web-search-navigator/issues/35
Expand All @@ -24,7 +29,6 @@ New in 0.2.11:
* Fixed options not being configurable for Firefox.

New in 0.2.9:

* Removed tabs permission which caused a scary Chrome warning about a new
permission in the extension to read "browsing history". The issue was related to a new feature for opening a search result in the background- see the discussion in https://github.com/infokiller/web-search-navigator/issues/53

Expand Down
58 changes: 33 additions & 25 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,30 @@ Object.assign(extension, {
});

/**
* @param {...[Element[], function|null]} results The array of tuples.
* @param {...[Element[], function|null]} includedNodeLists An array of tuples.
* Each tuple contains collection of the search results optionally accompanied
* with their container selector.
* @constructor
*/
function SearchResultCollection(...results) {
function SearchResultCollection(includedNodeLists, excludedNodeLists) {
/**
* @type {SearchResult[]}
*/
this.items = [];
for (let i = 0; i < results.length; i++) {
const params = results[i];
const nodes = params[0];
const containerSelector = params[1];
excludedResultsSet = new Set();
for (const nodes of excludedNodeLists) {
for (const node of nodes) {
excludedResultsSet.add(node);
}
}
for (const result of includedNodeLists) {
const nodes = result[0];
const containerSelector = result[1];
for (let j = 0; j < nodes.length; j++) {
const node = nodes[j];
this.items.push(new SearchResult(node, containerSelector));
if (!excludedResultsSet.has(node)) {
this.items.push(new SearchResult(node, containerSelector));
}
}
}
// Sort items by their document position.
Expand All @@ -169,7 +176,7 @@ function SearchResultCollection(...results) {
}
});
this.focusedIndex = 0;
this.focus = function(index, scrollToResult=true) {
this.focus = function(index, scrollToResult = true) {
if (this.focusedIndex >= 0) {
let item = this.items[this.focusedIndex];
// Remove highlighting from previous item.
Expand All @@ -195,26 +202,24 @@ function SearchResultCollection(...results) {
this.focusedIndex = index;
};
this.focusNext = function(shouldWrap) {
let nextIndex = 0;
if (this.focusedIndex < this.items.length - 1) {
nextIndex = this.focusedIndex + 1;
} else if (!shouldWrap) {
nextIndex = this.focusedIndex;
this.focus(this.focusedIndex + 1);
} else if (shouldWrap) {
this.focus(0);
}
this.focus(nextIndex);
};
this.focusPrevious = function(shouldWrap) {
let previousIndex = this.items.length - 1;
if (this.focusedIndex > 0) {
previousIndex = this.focusedIndex - 1;
} else if (!shouldWrap) {
previousIndex = this.focusedIndex;
this.focus(this.focusedIndex - 1);
} else if (shouldWrap) {
this.focus(this.items.length - 1);
} else {
window.scrollTo(window.scrollX, 0);
}
this.focus(previousIndex);
};
}

const scrollToElement = (element) => {
const scrollToElement = element => {
const elementBounds = element.getBoundingClientRect();
// firefox displays tooltip at the bottom which obstructs the view
// as a workaround ensure extra space from the bottom in the viewport
Expand Down Expand Up @@ -252,13 +257,16 @@ const getGoogleSearchLinks = () => {
// The nodes are returned in the document order, which is what we want.
return new SearchResultCollection(
[
document.querySelectorAll('#search .r > a:first-of-type'),
n => n.parentElement.parentElement
[
document.querySelectorAll('#search .r > a:first-of-type'),
n => n.parentElement.parentElement
],
[document.querySelectorAll('div.zjbNbe > a'), null],
[document.querySelectorAll('div.eIuuYe a'), null], // shopping results
[document.querySelectorAll('#pnprev, #pnnext'), null]
],
[document.querySelectorAll('div.zjbNbe > a'), null],
[document.querySelectorAll('div.eIuuYe a'), null], // shopping results
[document.querySelectorAll('#pnprev, #pnnext'), null]
[document.querySelectorAll('#search .kp-blk .r > a:first-of-type')]
);
}
};

extension.init();
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Web Search Navigator",
"version": "0.3.1",
"version": "0.3.2",
"description": "Keyboard shortcuts for Google™ search.",
"author": "Web Search Navigator",
"icons": {
Expand Down

0 comments on commit 1ed0aac

Please sign in to comment.