Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgleba/cd27d9b601b61a334b15af7ecae3d4e1 to your computer and use it in GitHub Desktop.
Save dgleba/cd27d9b601b61a334b15af7ecae3d4e1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name gheil 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==
// greg heil(cc-by-sa)18/07/09
var DONE = 0;
var MAX_SUGGESTIONS=108;
var Slices = [];
var NS=0;
var Sug = [];
var semic = ";"; var Nsemic=0; var Psemic=0;
var colon = ":"; var Ncolon=0; var Pcolon=0;
var lsq = "["; var Nlsq=0; var Plsq=0;
var rsq = "]"; var Nrsq=0; var Prsq=0;
var csq = 0;
// http://userscripts-mirror.org/scripts/review/100527
// Google Bookmarks Plus By Nibras — Last update Sep 7, 2011
var l_submit_button =document.getElementsByClassName('kd-button-submit kd-button')[0];
var x=l_submit_button.innerHTML;
// l_submit_button.onclick();
$(function() {
try {
$("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: 108px;'
+ ' overflow-y: auto;'
+ ' overflow-x: hidden;'
+ ' }'
+ '.ui-widget-content { background: #fc9; } '
+ '.ui-state-active{ background: #2c9; } '
// + '.ui-state-focus{ background: #2c9; } '
// + '.ui-widget-content tr{ background: #0; } '
// + '.ui-state-highlight { background: #2c9; } '
// + '.ui-widget-content selected{ background: #2c9; } '
// + '.ui-widget selected{ background: #2c9; }'
// + '.ui-widget select{ background: #2c9; }'
// + '.autocomplete-selected { background: #f48; }'
+ '</style>'
);
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
function getSuggestions( list, term ) {
Slices = []; NS=0;
Nsemic=0; Psemic=term.indexOf(semic);
while(Psemic>=0){
Nsemic++;
term = term.slice(0,Psemic) + term.slice(Psemic+1);
Psemic=term.indexOf(semic);
}
Nlsq=0; Plsq=term.indexOf(lsq);
while(Plsq>0){
Nlsq++;
term = term.slice(0,Plsq) + term.slice(Plsq+1);
Plsq = term.indexOf(lsq);
}
Nrsq=0; Prsq=term.indexOf(rsq);
while(Prsq>0){
Nrsq++;
term = term.slice(0,Prsq) + term.slice(Prsq+1);
Prsq = term.indexOf(rsq);
}
const LcTerm = term.toLowerCase();
const SpTerm = " "+term;
const spterm = " "+term.toLowerCase();
// prefer words starting with term, Match Case
// + Sp + Term
Slices.push(0);
let suggestions = list.filter( word => word.startsWith(term) );
Sug = suggestions;
// suggestions = suggestions.concat("-term"); // cmt
Slices.push(suggestions.length);
// + Sp + Term
Sug = list.filter( el=>el.includes(SpTerm) );
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
// suggestions = suggestions.concat("-- term"); // cmt
Slices.push(suggestions.length);
// + "_" + Term
Sug = list.filter( el=>el.includes("_"+term) );
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
// suggestions = suggestions.concat("---_term"); // cmt
Slices.push(suggestions.length);
// + "-" + Term
Sug = list.filter( el=>el.includes("-"+term) );
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
Slices.push(suggestions.length);
// CamelCase
Sug = list.filter( el=>el.includes(term.replace(/^[a-z]/g,function(f){return f.toUpperCase()})) );
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
Slices.push(suggestions.length);
// + LcTerm
Sug = list.filter( el=>el.toLowerCase().startsWith(LcTerm));
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
Slices.push(suggestions.length);
// inside, Match Case
// suggestions = suggestions.concat("----------Inside=Case");
Sug = list.filter(el=>el.includes(term));
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
Slices.push(suggestions.length);
// suggestions = suggestions.concat("----------Inside");
Sug = list.filter(el=>el.includes(LcTerm));
suggestions= suggestions.concat(Sug.filter(el=>!suggestions.includes(el)));
Slices.push(suggestions.length);
Sug=suggestions;
csq=(Nlsq-Nrsq); //+Slices.length;
while(csq>=Slices.length) csq-=Slices.length;
while(csq< 0) csq+=Slices.length;
Sug = Sug.slice(Slices[csq]).concat(Sug.slice(0,Slices[csq]));
suggestions=Sug;
// return suggestions;
return suggestions.slice(0, MAX_SUGGESTIONS);
// return suggestions.slice(0, suggestions.length - 1);
}
// suggestions = Sug.slice(shift(Slices),Sug.length-1);
// adding .kd-textbox CSS class to selector fixes the problem
$( "#bkmk_label_0, #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({
autoFocus: true,
minLength: 0,
delay: 5,
// position: { my : " right top", at: " center bottom ",collision: "none" },
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 );
},
create: function( event, ui ){
// Have not been able to get "," at end :-)
// Have not been able to remove " " as denovo terms
// So always best to begin with ","
var terms = split( this.value+" " );
this.value = terms.join( ", " );
return false;
},
search: function(event, ui) {
},
focus: function() {
var terms = split( this.value );
var term=terms.pop();
// Put ";" in a field: field deleted & form submited:
if(0<=term.indexOf(semic)){
terms.push( "" );
this.value = terms.join( ", " );
l_submit_button.onclick();
}
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
var term=terms.pop();
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