DuckDuckgGo Image Direct Link Patch

Make DuckDuckgGo Image search result entry's image size information as link to the direct image resource.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        DuckDuckgGo Image Direct Link Patch
// @namespace   https://greasyfork.org/en/users/85671-jcunews
// @version     1.0.3
// @license     AGPL v3
// @author      jcunews
// @description Make DuckDuckgGo Image search result entry's image size information as link to the direct image resource.
// @match       https://duckduckgo.com/*
// @grant       none
// @run-at      document-start
// ==/UserScript==

((dt, xo, ft, mo, ht) => {
  function processEntries() {
    if (!dt) {
      clearTimeout(ht);
      ht = setTimeout(processEntries, 500);
      return;
    }
    document.querySelectorAll(':is(.tile--img>.tile--img__dimensions,div[data-testid="zci-images"] figure>div>img+p):not(.linked_didlp)').forEach((e, a, u) => {
      e.classList.add("linked_didlp");
      (a = document.createElement("A")).textContent = e.textContent;
      a.style.cssText = "color:inherit";
      a.rel = "nofollow noopener noreferrer";
      u = e.previousElementSibling?.href ? e.previousElementSibling.href : e.closest('figure').querySelector('a').href;
      if (!dt.some((d, i) => {
        if (d.url === u) {
          a.href = d.image;
          dt.splice(i, 1);
          return true
        }
      })) a.href = 'javascript:void("Image URL is not found")';
      if (e.previousElementSibling?.href) {
        e.replaceChild(a, e.firstChild)
      } else {
        e.innerHTML = "";
        e.append(a)
      }
    });
  }

  xo = XMLHttpRequest.prototype.open;
  XMLHttpRequest.prototype.open = function(mtd, url) {
    if (/^\/?i\.js\?.*\bo=json/.test(url)) {
      this.addEventListener("load", (a, l, z) => {
        try {
          if (!(l = JSON.parse(this.responseText).results)) throw 0;
          Array.prototype.push.apply(dt, l);
        } catch(z) {}
        if (!mo) {
          mo = true;
          (new MutationObserver(recs => {
            clearTimeout(ht);
            ht = setTimeout(processEntries, 500);
          })).observe(document.body, {childList: true, subtree: true});
          processEntries();
        }
      })
    }
    return xo.apply(this, arguments);
  };
  ft = self.fetch;
  self.fetch = async function(url) {
    var r = await ft.apply(this, arguments);
    if (/^\/?i\.js\?.*\bo=json/.test(url)) {
      var rj = r.json;
      r.json = async function() {
        var r = await rj.apply(this, arguments);
        if (l = r.results) Array.prototype.push.apply(dt, l);
        if (!mo) {
          mo = true;
          (new MutationObserver(recs => {
            clearTimeout(ht);
            ht = setTimeout(processEntries, 500);
          })).observe(document.body, {childList: true, subtree: true});
          processEntries();
        }
        return r
      }
    }
    return r
  };

  dt = [];
})();