-
-
Save molhanec/8675269529244af12ea236f7cea26d8f to your computer and use it in GitHub Desktop.
// ==UserScript== | |
// @name Google Bookmarks fix | |
// @match https://www.google.com/bookmarks/* | |
// @grant none | |
// @require https://code.jquery.com/jquery-1.12.4.min.js | |
// @require https://code.jquery.com/ui/1.12.1/jquery-ui.js | |
// ==/UserScript== | |
$(function() { | |
try { | |
const MAX_SUGGESTIONS = 20; // get first 20 results | |
$("head").append ( | |
'<link ' | |
+ 'href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" ' | |
+ 'rel="stylesheet" type="text/css">' | |
+ '<style>' | |
+ ' .ui-autocomplete {' | |
+ ' max-height: 600px;' | |
+ ' overflow-y: auto;' | |
+ ' overflow-x: hidden;' | |
+ ' }' | |
+ ' #ac-list { display: none }' | |
+ '</style>' | |
); | |
function split( val ) { | |
return val.split( /,\s*/ ); | |
} | |
function extractLast( term ) { | |
return split( term ).pop(); | |
} | |
/* | |
Order of preference: | |
1. words starting with term matching case | |
2. words starting with term ignoring case | |
3. words containing the term elsewhere matching case | |
4. words containing the term elsewhere ignoring case | |
5. other than that we rely on the order of the input list; | |
in the case of Google Bookmarks it looks like it is | |
alphabetically sorted | |
Never return more than MAX_SUGGESTIONS results. | |
*/ | |
function getSuggestions( list, term ) { | |
const lowerCaseTerm = term.toLowerCase(); | |
// prefer words starting with term | |
let suggestions = list.filter( word => word.startsWith(term) ); | |
if (suggestions.length < MAX_SUGGESTIONS) { | |
suggestions = suggestions.concat(list.filter( | |
word => word.toLowerCase().startsWith(lowerCaseTerm) && !word.startsWith(term) | |
)); | |
} | |
// words containing term anywhere inside | |
if (suggestions.length < MAX_SUGGESTIONS) { | |
suggestions = suggestions.concat(list.filter( | |
word => word.includes(term) && !word.toLowerCase().startsWith(lowerCaseTerm) | |
)); | |
} | |
if (suggestions.length < MAX_SUGGESTIONS) { | |
suggestions = suggestions.concat(list.filter( | |
word => word.toLowerCase().includes(lowerCaseTerm) && !word.includes(term) && !word.toLowerCase().startsWith(lowerCaseTerm) | |
)); | |
} | |
return suggestions.slice(0, MAX_SUGGESTIONS); | |
} | |
$( "#bkmk_label_1, #gbqfq, .kd-textbox" ) | |
// don't navigate away from the field on tab when selecting an item | |
.on( "keydown", function( event ) { | |
if ( event.keyCode === $.ui.keyCode.TAB && | |
$( this ).autocomplete( "instance" ).menu.active ) { | |
event.preventDefault(); | |
} | |
}) | |
.autocomplete({ | |
minLength: 0, | |
source: function( request, response ) { | |
// delegate back to autocomplete, but extract the last term | |
/*response( $.ui.autocomplete.filter( | |
window.labelList, extractLast( request.term ) ) );*/ | |
const term = extractLast( request.term ); | |
const suggestions = getSuggestions( window.labelList, term ); | |
response( suggestions ); | |
}, | |
focus: function() { | |
// prevent value inserted on focus | |
return false; | |
}, | |
select: function( event, ui ) { | |
var terms = split( this.value ); | |
// remove the current input | |
terms.pop(); | |
// add the selected item | |
terms.push( ui.item.value ); | |
// add placeholder to get the comma-and-space at the end | |
terms.push( "" ); | |
this.value = terms.join( ", " ); | |
return false; | |
} | |
}); | |
} | |
catch(e){console.log(e)} | |
}); |
Please how to execute this script in the righ way?
Can you post on how we can enable this function? Sorry - Newbie here (not to Computing but to this :-))
Thanks! Works better than original autosuggestions.
For those that asked, install Tampermonkey Chrome extension. Once installed, click on extension icon, then "Create a new script". Paste the code from this gist in the editor, and click File > Save.
Thanks for the great code and @planiko for easy instructions.
On line 10 I get the following
Parsing Error: Unexpected token $
I am guessing that might be because of the version of Chrome I am using?
Mine is: Version 67.0.3396.87 (Official Build) (32-bit)
Many thanks to Molhanec for writing the code and Planiko for the installation instructions.
Your Userscript works awesome! I have Tampermonkey and Violentmonkey in two different browsers and when I clicked on "Raw" on your userscript page in GitHubGist, it installed in both and it works great. Thank You.
Works for me. Good job !