Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name DuckDuckGo-Google-YahooJp Search
- // @namespace https://greasyfork.org/en/users/85671-jcunews
- // @version 1.0.1
- // @license AGPLv3
- // @author jcunews
- // @description Combined web search for DuckDuckGo, Google (.com), and Yahoo Japan (.co.jp). Access the search from the script's GM menu. Search results will be provided in a new tab.
- // @match *://*/*
- // @grant GM_registerMenuCommand
- // @grant GM_xmlhttpRequest
- // @connect html.duckduckgo.com
- // @connect www.google.com
- // @connect search.yahoo.co.jp
- // ==/UserScript==
- (() => {
- let
- qry = "",
- engs = [
- {name: "DuckDuckGo", url: "https://html.duckduckgo.com/html?q=", result: null},
- {name: "Google", url: "https://www.google.com/search?q=", result: null},
- {name: "YahooJp", url: "https://search.yahoo.co.jp/search?p=", result: null}
- ],
- cnt = engs.length;
- function chkReq(a) {
- if (cnt > 0) return;
- (a = document.createElement("DIV")).id = "content";
- a.innerHTML = `
- <h3>Search results for <i id="q"></i></h3>
- <div id="entries">
- ${
- ((r, i) => {
- r = "";
- for (i = 0; i < 99; i++) {
- s = engs.map(e => e.result ? (e.result.children[i] ? e.result.children[i].outerHTML : "") : "").join("");
- if (s) {
- r += s
- } else break
- }
- return r
- })()
- }
- </div>
- `;
- a.querySelector("#q").textContent = qry;
- open(URL.createObjectURL(new Blob([`<!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>DuckDuckGo-Google-YahooJp Search</title>
- <style>
- #content { margin: 0 auto }
- @media (orientation: landscape) {
- #content { max-width: 56.25vw }
- }
- #entries > * { margin: .5em 0 }
- #entries > *:nth-child(2n) { background-color: #eee }
- h2, h3, p { margin: 0 }
- h2 { font-size: 14pt }
- figure { display: inline-block; margin: 0 .2em 0 0; width: 116px; height: 65px }
- figure img { width: 100%; height: 100%; object-fit: contain; background: #000 }
- </style>
- </head>
- <body>
- ${a.outerHTML}
- </body>
- </html>`
- ], {type: "text/html"})), "_blank")
- }
- function doerror() {
- cnt--;
- chkReq();
- }
- function doload(e, l, s) {
- if (this.status < 400) {
- (e = document.createElement("DIV")).innerHTML = this.responseText.match(/<body(?:\s|\S)+<\/body>/i)[0];
- switch (this.context.name) {
- case "DuckDuckGo":
- e.innerHTML = Array.from(e.querySelectorAll(".result__body")).map(l => {
- l.querySelectorAll("a").forEach(f => {
- f.rel = "noreferrer";
- f.removeAttribute("ping")
- });
- l.querySelectorAll("script,.result__icon").forEach(f => f.remove());
- l.querySelectorAll(".result__snippet").forEach((f, g) => {
- (g = document.createElement("DIV")).innerHTML = f.innerHTML;
- f.replaceWith(g);
- });
- return l.outerHTML
- }).join("");
- break;
- case "Google":
- e.innerHTML = Array.from(e.querySelectorAll(".g>div:not([class])")).map(l => {
- l.querySelectorAll("a").forEach(f => {
- f.rel = "noreferrer";
- f.removeAttribute("ping")
- });
- l.querySelectorAll("br,script,.B6fmyf,.IsZvec~*").forEach(f => f.remove());
- l.querySelectorAll("a").forEach(f => f.removeAttribute("onmousedown"));
- return l.outerHTML
- }).join("");
- break;
- case "YahooJp":
- e.innerHTML = Array.from(e.querySelectorAll("section")).map((l, a) => {
- if (l.querySelector("a") && !l.querySelector('a[href*="//search.yahoo.co.jp"]')) {
- l.querySelectorAll("a").forEach(f => {
- f.rel = "noreferrer";
- f.removeAttribute("ping")
- });
- l.querySelectorAll("br,figcaption,script,.sw-Card__titleCiteWrapper--pullDown").forEach(f => f.remove());
- if ((a = l.querySelector(".sw-Card__titleCiteWrapper")) && a.nextElementSibling) a.parentNode.appendChild(a);
- return l.outerHTML
- } else return ""
- }).join("");
- break;
- }
- this.context.result = e;
- }
- cnt--;
- chkReq();
- }
- GM_registerMenuCommand("Search", s => {
- if ((s = prompt("Enter search query.", qry)) === null) return;
- qry = encodeURIComponent(s.trim());
- cnt = engs.length;
- engs.forEach((e, i) => {
- e.result = null;
- GM_xmlhttpRequest({
- method: "GET",
- url: e.url + qry,
- onerror: doerror,
- onload: doload,
- context: e
- })
- })
- })
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement