-
-
Save planiko/0e25a846680b35246d1e8a49335120d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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;' | |
+ ' }' | |
+ '</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)} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment