-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearchEnginesBookmarklet.js
68 lines (55 loc) · 2.87 KB
/
SearchEnginesBookmarklet.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
(function(){
const scriptsDomain = "//medialab.github.io/SearchEnginesBookmarklet/";
// const scriptsDomain = "//localhost:4443/"; // for debug using `node serve-https.js`
if (window.artoo !== undefined && document.getElementById("BMoverlay")) return;
if (window.SearchEnginesBookmarklet === undefined) {
window.alert("It seems you are using an old version of this tool.\n\nThe new version works with more search engines such as DuckDuckGo, Baidu, Bing and Qwant.\n\nPlease remove it from your bookmarks and reinstall it by visiting the page that will open in a new tab (first allow it to pop-up).");
window.open("https://medialab.github.io/SearchEnginesBookmarklet/");
}
const moreResults = "switch_more_results.js",
pagination = "extract_pagination_results.js",
autoscroll = "extract_autoscroll_results.js",
images = "extract_image_results.js";
const href = window.location.href;
const injectScriptWithArtoo = function(queryArg, script){
const searchRegexp = new RegExp("[#?&]" + queryArg + "="),
replaceRegexp = new RegExp("^.*[#?&]" + queryArg + "=([^#?&]+).*$"),
query = (~href.search(searchRegexp) ? href.replace(replaceRegexp, '$1') : undefined);
if (!query) return window.alert("Please input your search query first.");
var body = document.getElementsByTagName("body")[0];
if (!body) {
body = document.createElement("body");
document.documentElement.appendChild(body);
}
if (typeof body !== "object") return window.alert("Could not find any webpage content...");
var a = document.createElement("script");
console.log("Loading artoo.js...");
a.src = "//medialab.github.io/artoo/public/dist/artoo-latest.min.js";
a.type = "text/javascript";
a.id = "artoo_injected_script";
if (script) a.setAttribute("settings", JSON.stringify({scriptUrl: scriptsDomain + script}));
body.appendChild(a);
};
// Google
if(~href.search(/:\/\/(www|scholar)?\.google\.[^/]+\//)){
injectScriptWithArtoo("q", ~href.search("&udm=2") ? images : (href.includes("num=") ? pagination : moreResults));
}
// DuckDuckGo
else if(~href.search(/:\/\/([^.]+\.)?duckduckgo\.[^/]+\//)){
injectScriptWithArtoo("q", ~href.search("&iax=") ? images : autoscroll);
}
// Baidu
else if(~href.search(/:\/\/([^.]+\.)?baidu\.[^/]+\//)){
injectScriptWithArtoo("wd", href.includes("rn=") ? pagination : moreResults);
}
// Qwant
else if(~href.search(/:\/\/([^.]+\.)?qwant\.[^/]+\//)){
injectScriptWithArtoo("q", autoscroll);
}
// Bing
else if(~href.search(/:\/\/([^.]+\.)?bing\.[^/]+\//)){
HTMLBodyElement.prototype.appendChild = Node.prototype.appendChild;
injectScriptWithArtoo("q", href.includes("count=") ? pagination : moreResults);
}
else window.alert("You can only use this bookmarklet on Google (web, images & Scholar), DuckDuckGo (web & images), Baidu, Qwant and Bing.");
}).call(this);