-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bangdd.js
88 lines (73 loc) · 2.63 KB
/
bangdd.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
77
78
79
80
81
82
83
84
85
86
87
88
// Configuration Object
const CONFIG = {
ID_SEARCH_FORM: "search_form_input",
ID_SEARCH_BUTTON: "search_button",
ID_DUCKBAR: "#react-duckbar ul",
BANG_TO_ADD: "!g"
};
/** Most recently used or most commonly used bangs will start appearing from here */
function bangToAdd() {
return CONFIG.BANG_TO_ADD;
}
/** Allow user to configure */
function haveToClickSearch() {
return true;
}
/** Main Action on button click */
function onTimeToBang(event) {
const searchFormInput = document.getElementById(CONFIG.ID_SEARCH_FORM);
const currentSearch = searchFormInput.value;
const bang = bangToAdd();
if (!currentSearch.includes(bang)) {
searchFormInput.value = currentSearch + " " + bang;
}
if (haveToClickSearch()) {
document.getElementById(CONFIG.ID_SEARCH_BUTTON).click();
}
}
function createButton() {
const existingLis = document.querySelectorAll(CONFIG.ID_DUCKBAR + ' li');
if (existingLis.length === 0) {
console.error('No existing li elements found');
return null;
}
const existingLi = existingLis.length >= 2 ? existingLis[existingLis.length - 2] : existingLis[existingLis.length - 1];
const existingA = existingLi.querySelector('a');
const liClassNames = existingLi ? existingLi.className : '';
const aClassNames = existingA ? existingA.className : '';
const div = document.createElement('div');
div.innerHTML = `<li id="bang_it" class="${liClassNames}">
<a href="#" class="${aClassNames}">Google</a>
</li>`;
const bang_it = div.firstChild;
bang_it.addEventListener("click", (event) => onTimeToBang(event));
return bang_it;
}
function insertInCorrectPosition(bang_it) {
const existingButton = document.getElementById(bang_it.id);
if (existingButton) {
existingButton.remove();
console.log('Removed existing button');
}
insertAfter(bang_it, document.querySelector(CONFIG.ID_DUCKBAR).lastChild);
console.log('Inserted bang it element');
}
/* Helper Methods */
function insertAfter(el, referenceNode) {
referenceNode.parentNode.insertBefore(el, referenceNode.nextSibling);
}
/* End of helper methods */
function onExtensionLoading() {
if (document.getElementById('bang_it')) {
return;
}
const bang_it = createButton();
if (bang_it) {
insertInCorrectPosition(bang_it);
}
}
document.addEventListener('DOMContentLoaded', onExtensionLoading);
onExtensionLoading();
// Run onExtensionLoading() after 1 seconds to ensure that the page has loaded completely
setTimeout(onExtensionLoading, 1000);
setTimeout(onExtensionLoading, 5000);