-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
284 lines (269 loc) · 10.1 KB
/
content.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"use strict";
/* global chrome */
/**
* How the 'Result hidden' element is added to the DOM. Varies by provider.
*/
const HiddenLinkPlacement = Object.freeze({
"Prepend": 1,
"InsertBefore": 2
});
/**
* A search results provider.
*/
class Provider {
/**
* Provider constructor.
* @param {string} name Provider name.
* @param {string} resultClass Result elements' class.
* @param {HiddenLinkPlacement} hiddenLinkPlacement How to insert a new 'Result hidden' element for this provider.
* @param {boolean} dynamicPaging Whether new results are paged in dynamically.
* @param {string} dynamicResultsQuery The point at which to monitor for dynamic changes when new results are loaded in.
*/
constructor(name, resultClass, hiddenLinkPlacement = HiddenLinkPlacement.Prepend, dynamicPaging = false, dynamicResultsQuery = "") {
this.name = name;
this.resultClass = resultClass;
this.hiddenLinkPlacement = hiddenLinkPlacement;
this.dynamicPaging = dynamicPaging;
this.dynamicResultsQuery = dynamicResultsQuery;
}
/**
* Returns an HTMLCollectionOf<Element> representing the individual search
* page elements which require processing, i.e. those which have not already
* been hidden.
*/
getResultElements() {
return Array.from(document.getElementsByClassName(this.resultClass))
.filter((element) => !element.classList.contains("hidden"));
}
/**
* Locate and extract the HREF from a single search page result.
* @param {Element} resultElement - The search page result element.
* @param {string} [linkClass] - The link element's class. If present, this will be used instead of just looking for anchor tags.
* @returns {string} - The URL, or "" if not found.
*/
getLinkFromResultElement(resultElement, linkClass) {
let resultHrefs = undefined;
if (typeof(linkClass) === "undefined") {
resultHrefs = resultElement.getElementsByTagName("a");
}
else {
resultHrefs = resultElement.getElementsByClassName(linkClass);
}
return resultHrefs.length === 0 ? "" : resultHrefs[0].href.toString();
}
/**
* Locate the search result associated with a 'Hide result' link.
* @param {Element} linkElement - The 'Hide result' link.
*/
getResultFromHideLink(linkElement) {
let result = undefined;
if (this.name === "Bing" || this.name === "Yahoo" || this.name === "SearchEncrypt" || this.name === "Dogpile") {
result = linkElement.nextElementSibling;
}
else {
result = linkElement.parentElement.getElementsByClassName(this.resultClass)[0];
}
return result;
}
/**
* Replace a result with 'Result hidden' text.
* @param {Element} result - The result element.
*/
hideResult(result) {
const noteDiv = this.createResultHiddenElement(result);
if (this.hiddenLinkPlacement === HiddenLinkPlacement.InsertBefore) {
result.parentElement.insertBefore(noteDiv, result);
}
else {
result.parentElement.prepend(noteDiv);
}
result.classList.add("hidden");
}
/**
* Create the 'Result hidden (<domain>)' element.
* @param {Element} result - The search result element.
*/
createResultHiddenElement(result) {
const noteDiv = document.createElement("div");
noteDiv.innerHTML = chrome.i18n.getMessage("resultHidden") +
` (${new URL(this.getLinkFromResultElement(result)).hostname})`;
noteDiv.classList.add('result-hidden');
// When the 'Result hidden' text is clicked, unhide the
// result.
noteDiv.addEventListener("click", ev => this.unhideResult(ev));
return noteDiv;
}
/**
* Show a previously-hidden result when our message is clicked.
* @param {Event} ev - The mouse click event trigger.
*/
unhideResult(ev) {
observer.disconnect();
const resultElement = this.getResultFromHideLink(ev.target);
resultElement.classList.remove("hidden");
resultElement.classList.add("unhidden");
ev.currentTarget.remove();
ev.stopPropagation();
setupObserver();
}
/**
* Add our 'Hide' link to a specific search result.
* @param {Element} result - The search result element to add our link to.
*/
addLink(result) {
const a = document.createElement("a");
a.appendChild(document.createTextNode(chrome.i18n.getMessage("hidePrompt")));
a.title = chrome.i18n.getMessage("hidePromptTitle");
a.href = "javascript:;";
a.className = "hide-result-link";
const resultLink = this.getLinkFromResultElement(result);
if (resultLink.length === 0) {
return;
}
a.setAttribute("data-link", resultLink);
// When the user clicks on the link, add to the list of hidden sites.
a.addEventListener("click", function(mouse_event) {
chrome.storage.sync.get(["hidden_sites"], (result) => {
let hiddenSites = result.hidden_sites;
if (hiddenSites === undefined) {
hiddenSites = [];
}
const hostname = new URL(mouse_event.target.dataset.link).hostname;
if (!hiddenSites.includes(hostname)) {
hiddenSites.push(hostname);
}
chrome.storage.sync.set({"hidden_sites": hiddenSites});
});
event.stopPropagation();
});
// TODO: separate from surrounding clickable div to prevent accidental
// navigation to the site itself
result.appendChild(a);
}
}
let searchProvider = function () {
const hostname = document.domain;
if (hostname.startsWith("duckduckgo.")) {
return new Provider("Duck Duck Go", "result__body links_main",
HiddenLinkPlacement.Prepend, true, "div.results--main");
}
else if (hostname.includes(".bing.")) {
return new Provider("Bing", "b_algo", HiddenLinkPlacement.InsertBefore);
}
else if (hostname.includes(".google.")) {
return new Provider("Google", "rc");
}
else if (hostname.includes("search.yahoo.com")) {
return new Provider("Yahoo", "algo-sr");
}
else if (hostname.includes(".ask.com")) {
return new Provider("Ask.com", "PartialSearchResults-item");
}
else if (hostname.includes("yandex.ru")) {
return new Provider("Yandex", "serp-item");
}
else if (hostname.includes("searchencrypt.com")) {
return new Provider("SearchEncrypt", "web-result",
HiddenLinkPlacement.InsertBefore, true, "div#app");
}
else if (hostname.includes("dogpile.com")) {
return new Provider("Dogpile", "web-bing__result",
HiddenLinkPlacement.InsertBefore);
}
// else if (hostname.includes(".baidu.com")) {
// let p = new Provider("Baidu", "result c-container");
// p.getLinkFromResultElement = (result) => {
// let hrefs = Array.from(result.getElementsByTagName("a"))
// .filter(x => x.classList.contains("c-showurl"))
// .map(x => x.innerText);
// if (hrefs.length > 0) {
// return new URL(hrefs[0].trim()).href.toString();
// }
// }
// return p;
// }
else {
return new Provider("Unknown", "");
}
}();
Object.freeze(searchProvider);
/**
* Determine whether a result should be hidden on the page.
* @param {string} link - The link to examine.
* @param {*} hiddenSites - The array of site hostnames the user wants to hide.
*/
function shouldHide(link, hiddenSites) {
if (hiddenSites === undefined || hiddenSites.length === 0) { return false; }
return hiddenSites.includes(new URL(link).hostname);
}
/**
* Wait until the search page has finished rendering and the results are
* available to process.
*/
async function waitForElements() {
while(searchProvider.getResultElements().length === 0) {
await new Promise(r => setTimeout(r, 500));
}
return searchProvider.getResultElements();
}
function processResults(results) {
const resultElements = results;
// Get the user's preferences from storage
chrome.storage.sync.get(["hidden_sites"], (stored) => {
for (const result of resultElements) {
const link = searchProvider.getLinkFromResultElement(result);
if (shouldHide(link, stored.hidden_sites)) {
searchProvider.hideResult(result);
}
else {
result.classList.remove("hidden");
}
// Add the link if it is not present already
if (result.lastChild.text === undefined ||
result.lastChild.text !== chrome.i18n.getMessage("hidePrompt")) {
searchProvider.addLink(result);
}
}
})
}
function processSearchPage() {
waitForElements()
.then(results => processResults(results))
.catch(reason => console.error(reason));
}
/**
* When the hidden sites list is altered, process the page again.
*/
chrome.storage.onChanged.addListener((changes) => {
if (changes.hidden_sites !== undefined &&
changes.hidden_sites.newValue !== undefined) {
changes.hidden_sites.newValue.forEach((site) => {
console.log(site);
});
}
processSearchPage();
});
let observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
processSearchPage();
});
/**
* For providers which load new results dynamically.
*/
async function setupObserver() {
if (searchProvider.dynamicPaging) {
let query = searchProvider.dynamicResultsQuery;
while (document.querySelector(query) === null) {
await new Promise(r => setTimeout(r, 500));
}
observer.observe(document.querySelector(query), {
subtree: true,
childList: true
});
console.log(`Observer setup for ${searchProvider.name}`);
}
}
setupObserver();
processSearchPage();