forked from toydotgame/old-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplace.js
129 lines (118 loc) · 4.21 KB
/
replace.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* CREATED: 2021-10-19
* AUTHOR: toydotgame
* GOOOOL: koke228
* Main replacement script that handles boilerplate classes
*/
//var logoUrl = "https://upload.wikimedia.org/wikipedia/commons/3/3e/Google_2011_logo.png";
//var favicon = "https://www.w3schools.com/favicon.ico";
var logoUrlDefault = browser.runtime.getURL('resources/logo.png');
var logoUrlNoBear = browser.runtime.getURL('resources/logo_nobear.png');
var logoUrlGoyda = browser.runtime.getURL('resources/logo_goyda.png');
var faviconDefault = browser.runtime.getURL('resources/favicon.ico');
browser.storage.local.get(['logoChoice', 'noFavicon']).then((result) => {
var logoUrl;
switch(result.logoChoice) {
case 'noBear':
logoUrl = logoUrlNoBear;
break;
case 'goyda':
logoUrl = logoUrlGoyda;
break;
case 'noChange':
return;
default:
logoUrl = logoUrlBear;
}
var favicon = result.noFavicon ? null : faviconDefault;
var homepageLogo = [".lnXdpd", ".k1zIA", ".SuUcIb"]; // Homepage logo, its container, and the Doodle share button
var searchLogo = [".jfN4p", ".TYpZOd"]; // PNG and SVG (respectively) results page logos
var randRow = [".IUOThf", ".XtQzZd"]; // Classes of actual button div and then the navbar which is left too high
if (favicon) {
RunWhenReady(["head"], function(loadedElement) {
loadedElement.append(Object.assign(document.createElement("link"),{rel:"icon", href:favicon}));
});
}
var subdomain = window.location.host.split(".")[0];
var page = "/" + location.pathname.split("/")[1];
var isImageSearch = false;
if(new URLSearchParams(window.location.search).get("tbm") == "isch") { // Query string `&tbm=isch` only present on Images results, where the logo is an SVG
isImageSearch = true;
}
RunWhenReady([
homepageLogo[1], searchLogo[0], searchLogo[1]
], function(loadedElement) {
Main();
});
/*
* void Main()
* Calls either function depending on what page is loaded right now
*/
function Main() {
switch(page) {
case "/":
case "/webhp":
case "/imghp":
SwapHomepageLogo();
break;
case "/search":
SwapResultsLogo();
}
}
/*
* void SwapHomepageLogo()
* Replaces Google Doodles and the regular logo image with the old logo on the Google homepages
*/
function SwapHomepageLogo() {
if(!(page == "/imghp" || subdomain == "images")) {
document.querySelector(homepageLogo[1]).outerHTML = '<div style="margin-top:auto; max-height:92px;"><img class="' + homepageLogo[0].split(".")[1] + '"></div>';
}
document.querySelector(homepageLogo[0]).src = logoUrl;
document.querySelector(homepageLogo[0]).srcset = ""; // Clear override
// Override Doodle size styles:
document.querySelector(homepageLogo[0]).width = "272";
document.querySelector(homepageLogo[0]).height = "100";
// Remove share button (only present on Doodles):
try {
document.querySelector(homepageLogo[2]).remove();
} catch(TypeError) {}
}
/*
* void SwapResultsLogo()
* Replaces small logo on search pages
* TODO: Doodle compatibility
*/
function SwapResultsLogo() {
// Remove random bar of buttons Google thought'd be a good idea to add:
RunWhenReady([randRow[0]], function (loadedElement) {
document.querySelector(randRow[0]).remove();
document.querySelector(randRow[1]).style.height = "57px";
});
if(!isImageSearch) {
document.querySelector(searchLogo[0]).src = logoUrl;
document.querySelector(searchLogo[0]).height = "33";
return;
}
}
/*
* void RunWhenReady(String[] selectors, function code)
* Takes code and runs it when at least one of the input querySelectors is detected.
* Returns a DOMObject `loadedElement`, corresponding to the earliest loaded element
* that matches a selector.
* TODO: Asynchronously create a new thread to run this code.
*/
function RunWhenReady(selectors, code) {
var loadedElement;
var observer = new MutationObserver(function (mutations, mutationInstance) {
for(var i = 0; i < selectors.length; i++) {
if(document.querySelector(selectors[i]) != null) {
loadedElement = document.querySelector(selectors[i]);
code(loadedElement);
mutationInstance.disconnect();
break;
}
}
});
observer.observe(document, {childList: true, subtree: true});
}
});