-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathviolentmonkeyscript.user.js
325 lines (272 loc) · 10.6 KB
/
violentmonkeyscript.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// ==UserScript==
// @name fireSeqSearchScript
// @namespace https://github.com/Endle/fireSeqSearch
// @version 0.1.4
// @description Everytime you use the search engine, FireSeqSearch searches your personal logseq notes.
// @author Zhenbo Li
// @match https://www.google.com/search*
// @match https://duckduckgo.com/*
// @match https://metager.org/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant GM.xmlHttpRequest
// ==/UserScript==
// MIT License
// Copyright (c) 2021-2023 Zhenbo Li
/*global GM*/
const fireSeqSearchDomId = "fireSeqSearchDom";
const fireSeqSearchScriptCSS = `
#fireSeqSearchDom {
margin: 1em 1em 1em 1em;
color: var(--theme-col-txt-snippet); /* duckduck color*/
}
#fireSeqSearchDom.experimentalLayout {
position: fixed;
top: 140px;
right: 12px;
width: 200px;
background-color: hsla(200, 40%, 96%, .8);
font-size: 12px;
border-radius: 6px;
z-index: 99999;
}
.fireSeqSearchTitleBar {
margin: 0.5em 0;
}
.hideSummary {
margin: 0 1em;
}
#fireSeqSearchDom ul {
margin: 0;
padding: 0.6em;
border: 1px dotted gray;
list-style: none;
line-height: 1.5em;
}
#fireSeqSearchDom ul li {
font-size: 15px;
}
#fireSeqSearchDom ul li + li {
margin-top: 0.4em;
}
#fireSeqSearchDom ul li a {
text-decoration: underline;
text-decoration-style: dotted;
text-decoration-thickness: 1px;
text-underline-offset: 2px;
}
#fireSeqSearchDom ul li::before {
content: ' ';
display: inline-block;
margin-right: 0.4em;
line-height: 1em;
width: 1em;
height: 1em;
transform: translateY(3px);
border-radius: 3px;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAAe1BMVEUAKzaFyMiKz88AJjIAKjaHy8sAHiuM0tIAGSgAGykAIC0AESIAFyYAIzAAHCoAFCQAEiBvq6wtVlw0XmQIMDtooqRAbnNdk5VPgYU4Y2lyr7B4trdHc3Z/wcEAHSctUFVimZoaP0hShIcoRksTLjQADBkAABNdjo8AAAzWDdSWAAABBklEQVQ4jc1S7XKDIBCEQ0DEL2oJVqOmiUl8/yfs6XTaePgA2T8w7N5xu3OMvSOESI5ejSnWM1Hqo/lUEa8a708WLyYAgG4zWv+lpQRXsdIDR2hLBGkn8RnazK4nB2+IIO9XQvZ5dsYf4FlHI4StcqiYGqeppvU4u+CogIvaXB56n53WrmBJYSprq5S6wB71ONZM5Cc/AAyuFXEUGF/aDLANg55b6hRRnjX/A6ZCExfTC4+KkBJB6uSrgOtv0iKHHc/hSr3ovUCGcs9bSQS0g7mQGSaSaTLvBNJFSRQ3+JfIfow3L5s7XJyVBR3uR5uZPG7PnsPQXedoJX4hzGNZlnt5VP7G+AHcFwwZX2F8QwAAAABJRU5ErkJggg==);
background-repeat: no-repeat;
background-size: 16px;
}
.fireSeqSearchHitSummary {
font-size: 0.9em
}
.fireSeqSearchHitSummary::before {
content: "\\00A0::\\00A0";
}
.fireSeqSearchHighlight {
padding: 0 4px;
color: black !important;
background-color: gold;
border-radius: 3px;
}
`;
function consoleLogForDebug(message) {
console.log(message); //skipcq: JS-0002
}
function addGlobalStyle(css) {
const head = document.getElementsByTagName("head")[0];
if (!head) { return; }
const style = document.createElement("style");
style.id = "fireSeqSearchScriptCSS";
// style.type = "text/css";
style.innerHTML = css;
head.appendChild(style);
}
function createElementWithText(type, text) {
const element = document.createElement(type);
element.textContent = text;
return element;
}
function createHrefToLogseq(record, serverInfo) {
const name = serverInfo.notebook_name;
const title = record.title;
const prettyTitle = title.replaceAll("%2F", "/");
const target = record.logseq_uri || `logseq://graph/${name}?page=${title}`;
const logseqPageLink = document.createElement('a');
const text = document.createTextNode(prettyTitle);
logseqPageLink.appendChild(text);
logseqPageLink.title = prettyTitle;
logseqPageLink.href = target;
consoleLogForDebug(logseqPageLink);
return logseqPageLink;
}
function checkUserOptions() {
const options = {
debugStr: "tampermonkey",
ExperimentalLayout: false,
ShowHighlight: true,
ShowScore: false
}
consoleLogForDebug(options);
return options;
}
async function appendResultToSearchResult(fetchResultArray, _container) {
const serverInfo = fetchResultArray[0];
const rawSearchResult = fetchResultArray[1];
const firefoxExtensionUserOption = await checkUserOptions();
consoleLogForDebug('Loaded user option: ' + JSON.stringify(firefoxExtensionUserOption));
function createTitleBarDom(count) {
const titleBar = createElementWithText("div");
titleBar.classList.add('fireSeqSearchTitleBar');
const hitCount = `<span>We found <b>${count.toString()}</b> results in your logseq notebook</span>`;
titleBar.insertAdjacentHTML("afterbegin",hitCount);
const btn = document.createElement("button");
btn.classList.add("hideSummary");
const text = document.createTextNode("Hide Summary (Tmp)");
btn.appendChild(text);
btn.onclick = function () {
// alert("Button is clicked");
for (const el of document.querySelectorAll('.fireSeqSearchHitSummary')) {
// el.style.visibility = 'hidden';
el.remove();
}
};
titleBar.appendChild(btn);
return titleBar;
}
function createFireSeqDom() {
const div = document.createElement("div");
div.setAttribute("id", fireSeqSearchDomId);
return div;
}
const dom = createFireSeqDom();
dom.appendChild(createTitleBarDom(rawSearchResult.length));
consoleLogForDebug(dom);
const hitList = document.createElement("ul");
consoleLogForDebug(rawSearchResult);
for (const rawRecord of rawSearchResult) {
// const e = document.createTextNode(record);
consoleLogForDebug(rawRecord);
const record = JSON.parse(rawRecord);
consoleLogForDebug(typeof record);
const li = createElementWithText("li", "");
if (firefoxExtensionUserOption.ShowScore) {
const score = createElementWithText("span", String(record.score));
li.appendChild(score);
}
const href = createHrefToLogseq(record, serverInfo);
li.appendChild(href);
li.append(' ')
if (firefoxExtensionUserOption.ShowHighlight) {
const summary = createElementWithText("span", "");
summary.innerHTML = record.summary;
summary.classList.add('fireSeqSearchHitSummary');
li.appendChild(summary);
}
// let e = wrapRawRecordIntoElement(record, serverInfo);
// e.style.
hitList.appendChild(li);
// consoleLogForDebug("Added an element to the list");
}
dom.appendChild(hitList);
if (firefoxExtensionUserOption.ExperimentalLayout) {
// Inspired by https://twitter.com/rockucn
// https://greasyfork.org/en/scripts/446492-%E6%90%9C%E7%B4%A2%E5%BC%95%E6%93%8E%E5%88%87%E6%8D%A2%E5%99%A8-search-engine-switcher/code
dom.classList.add("experimentalLayout");
}
function insertDivToWebpage(result) {
let contextId = "rcnt";
if (window.location.host.includes("duckduckgo.com")) {
contextId = "web_content_wrapper";
}
if (window.location.host.includes("searx")) { // https://github.com/Endle/fireSeqSearch/issues/103
contextId = "results";
}
if (window.location.host.includes("metager")) { // https://github.com/Endle/fireSeqSearch/issues/127
contextId = "results";
}
document.getElementById(contextId).insertAdjacentElement("beforebegin", result);
}
insertDivToWebpage(dom);
}
function getSearchParameterFromCurrentPage() {
let searchParam = "";
function getSearchParameterOfSearx() {
const inputBox = document.getElementById("q");
return inputBox.value;
}
function getSearchParameterOfMetager() {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('eingabe');
}
if (window.location.toString().includes("searx")) {
searchParam = getSearchParameterOfSearx();
} else if (window.location.toString().includes("metager")) {
searchParam = getSearchParameterOfMetager();
} else {
// https://stackoverflow.com/a/901144/1166518
const urlParams = new URLSearchParams(window.location.search);
searchParam = urlParams.get('q');
}
consoleLogForDebug(`Got search param: ${searchParam}`);
return searchParam;
}
(function() {
const searchParameter = getSearchParameterFromCurrentPage();
addGlobalStyle(fireSeqSearchScriptCSS);
GM.xmlHttpRequest({
method: "GET",
url: "http://127.0.0.1:3030/server_info",
onload(infoResponse) {
const server_info = JSON.parse(infoResponse.responseText);
consoleLogForDebug(server_info);
GM.xmlHttpRequest({
method: "GET",
url: `http://127.0.0.1:3030/query/${searchParameter}`,
onload(queryResponse) {
const hit = JSON.parse(queryResponse.responseText);
// consoleLogForDebug(hit);
consoleLogForDebug(typeof hit);
appendResultToSearchResult([server_info, hit])
.then((_e) => {
const highlightedItems = document.querySelectorAll('.fireSeqSearchHighlight');
consoleLogForDebug(highlightedItems);
})
.catch(error => {
consoleLogForDebug(error);
});
}
});
}
});
/*
//https://gomakethings.com/waiting-for-multiple-all-api-responses-to-complete-with-the-vanilla-js-promise.all-method/
Promise.all([
fetch("http://127.0.0.1:3030/server_info"),
fetch(`http://127.0.0.1:3030/query/${searchParameter}`)
]).then(function (responses) {
return Promise.all(responses.map(function (response) {return response.json();}));
}).then(function (data) {
consoleLogForDebug(data);
return appendResultToSearchResult(data);
}).then((_e) => {
const highlightedItems = document.querySelectorAll('.fireSeqSearchHighlight');
consoleLogForDebug(highlightedItems);
highlightedItems.forEach((element) => {
element.style.color = 'red';
});
}).catch(function (error) {
consoleLogForDebug(error);
});
*/
})();