Skip to content

Instantly share code, notes, and snippets.

@molhanec
Last active July 3, 2020 00:36
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save molhanec/8675269529244af12ea236f7cea26d8f to your computer and use it in GitHub Desktop.
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)}
});
@wpbns
Copy link

wpbns commented Jun 23, 2018

Awesome!!! Thanks for this. It has resolved the Google Chrome (Google Bookmark Label issue). :-) +1:

@dirdigeng
Copy link

+1 - Awesome work!

@amagard
Copy link

amagard commented Jun 28, 2018

Works for me. Good job !

@kmisc
Copy link

kmisc commented Jul 20, 2018

Please how to execute this script in the righ way?

@jkanagaraj
Copy link

Can you post on how we can enable this function? Sorry - Newbie here (not to Computing but to this :-))

@planiko
Copy link

planiko commented Aug 12, 2018

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.

@escales
Copy link

escales commented Aug 19, 2018

Thanks for the great code and @planiko for easy instructions.

@python72
Copy link

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)

@RHebensperger
Copy link

Many thanks to Molhanec for writing the code and Planiko for the installation instructions.

@marktedrow1
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment