-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle-shruggie.user.js
76 lines (67 loc) · 2.33 KB
/
google-shruggie.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// ==UserScript==
// @name Google Shruggie
// @id google_shruggie
// @version 0.1
// @author Ori Avtalion <ori@avtalion.name>
// @namespace name.avtalion.ori.google_shruggie
// @description Enriches Google searches with Unicode emoticons based on queries
// @license Public Domain
// @downloadURL https://github.com/salty-horse/gm-scripts/raw/master/google-shruggie.user.js
// @updateURL https://github.com/salty-horse/gm-scripts/raw/master/google-shruggie.user.js
// @run-at document-end
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @include *://www.google.*/search?*
// @include *://ipv6.google.*/search?*
// @include *://www.google.*/webhp?*
// @include *://ipv6.google.*/webhp?*
// ==/UserScript==
(function() {
"use strict";
const EMOTICONS = {
"shruggie": "¯\\_(ツ)_/¯",
"look of disapproval": "ಠ_ಠ",
};
// Function taken from <http://commons.oreilly.com/wiki/index.php/Greasemonkey_Hacks/Those_Not_Included_in_This_Classification#Refine_Your_Google_Search>
function getCurrentSearchText(isPageLoad) {
if (isPageLoad) {
const hash = window.location.hash;
if (hash) {
const hash_match = hash.match(/&?\bq=([^&]*)/);
if (hash_match && hash_match.length > 1) {
return hash_match[1];
}
}
}
var elmForm = document.forms.namedItem('tsf');
if (!elmForm) { return; }
var elmSearchBox = elmForm.elements.namedItem('q');
if (!elmSearchBox) { return; }
var usQuery = elmSearchBox.value;
if (!usQuery) { return; }
return usQuery;
}
function checkQuery(isPageLoad) {
var search_term = getCurrentSearchText(isPageLoad);
if (search_term) {
search_term = search_term.toLowerCase().replace(/\+/g, ' ');
}
$('#salty_shruggie').remove();
const emoticon = EMOTICONS[search_term];
if (emoticon) {
var elem = $('<div id="salty_shruggie" class="lr_container_mod"><div class="vk_ans">' + emoticon + '</div></div>');
// HACK: Wait a bit for the element to load
if (isPageLoad) {
window.setTimeout(function(){$('#center_col').prepend(elem);}, 500)
} else {
$('#center_col').prepend(elem);
}
}
}
// Page load
checkQuery(/*isPageLoad=*/true);
// Listen to changes to query
$('#lst-ib').on('input', function() {
checkQuery(/*isPageLoad=*/false);
});
})();