Created
May 16, 2016 17:44
-
-
Save robinp7720/18c0e14516b20f98d381c559a07677f1 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 better search | |
// @namespace Mysunland | |
// @include https://scratch.mit.edu/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
var $; | |
// Add jQuery | |
(function () { | |
if (typeof unsafeWindow.jQuery == 'undefined') { | |
var GM_Head = document.getElementsByTagName('head') [0] || document.documentElement, | |
GM_JQ = document.createElement('script'); | |
GM_JQ.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; | |
GM_JQ.type = 'text/javascript'; | |
GM_JQ.async = true; | |
GM_Head.insertBefore(GM_JQ, GM_Head.firstChild); | |
} | |
GM_wait(); | |
}) (); | |
// Check if jQuery's loaded | |
function GM_wait() { | |
if (typeof unsafeWindow.jQuery == 'undefined') { | |
window.setTimeout(GM_wait, 100); | |
} else { | |
$ = unsafeWindow.jQuery.noConflict(true); | |
letsJQuery(); | |
} | |
}// All your GM code must be inside this function | |
function letsJQuery() { | |
// Create search suggestions box | |
$('.search').append('<ul class="answers"></ul>'); | |
$('.answers').css({ | |
background: '#219ddb', | |
margin: '0', | |
'margin-top': '-7px', | |
}) | |
$('head').append('<style> .answers li a{color: white !important;}</style>'); | |
$('input[name="q"]').attr('autocomplete','off').css({'border-bottom-left-radius':'0','border-bottom-right-radius':'0'}) | |
$('input[name="q"]').on('input propertychange paste', function () { | |
$.ajax('https://mysunland.org/scratch/ajax/search.php', { | |
data: { | |
search: $('input[name="q"]').val() | |
}, | |
method: 'GET', | |
dataType: 'json' | |
}).done(function (data) { | |
console.log(data) | |
var count = 0; | |
users = data['hits']['hits']; | |
var length = users.length; | |
$('.answer').remove(); | |
for (var i = 0; i < length; i++) { | |
var item = users[i]['_source']; | |
if ($('#' + item.username).length > 0) { | |
$('#' + item.username).removeClass('old-answer'); | |
} else { | |
html = '<li class="answer" id="' + item.username + '"><a href="https://scratch.mit.edu/users/' + item.username + '/">' + item.username + '</a></li>'; | |
$('.answers').append(html); | |
} | |
} | |
}); | |
}); | |
} | |
console.log('Script loaded!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment