☰

Danbooru hover preview

hover over pics to preview them à la 4chan X

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        Danbooru hover preview
// @namespace   makamys
// @description hover over pics to preview them à la 4chan X
// @match       *://*.donmai.us/*
// @version     9
// @grant       none
// @license     Unlicense
// ==/UserScript==

// from http://greasemonkey.win-start.de/patterns/add-css.html
function addGlobalStyle(css) {
    var head, style;
    head = document.getElementsByTagName('head')[0];
    if (!head) { return; }
    style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = css;
    head.appendChild(style);
}

addGlobalStyle(`
#ihover {
  position: fixed;
  max-height: 100%;
  z-index: 10000;
  pointer-events: none;
}
`);

function main(){
  let ihover = null;
  let urlCache = {};
  
  let hoverUI = document.createElement('div');
  hoverUI.id = "hoverUI";
  document.body.prepend(hoverUI);

  function installListener(thumb){
    thumb.addEventListener("mouseenter", event => {
      hoverUI.innerHTML = `<img id="ihover"></img>`;
      ihover = document.getElementById("ihover");

      let article = event.currentTarget.closest("article");
      let id = article.getAttribute("data-id");

      let previewURL = urlCache[id];

      ihover.setAttribute("data-id", id)
      if(!previewURL){
        fetch("https://danbooru.donmai.us/posts/" + id + ".json")
        .then(response => response.json())
        .then(
            data => {
              previewURL = data["large_file_url"];
              urlCache[id] = previewURL;
              if(ihover.getAttribute("data-id") == id) {
                ihover.setAttribute("src", previewURL);
              }
            }
        );
      } else {
        ihover.setAttribute("src", previewURL);
      }
    });

    thumb.addEventListener("mouseleave", _ => {
      hoverUI.innerHTML = "";
    });
  }

  document.querySelectorAll("article img.post-preview-image").forEach(thumb => {
    installListener(thumb);
  });

  // Unlimited Paginator Works compat
  new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      mutation.addedNodes.forEach(addedNode => {
        if (addedNode.tagName == "ARTICLE") {
          addedNode.querySelectorAll("img.post-preview-image").forEach(thumb => {
            installListener(thumb);
          });
        }
      });
    });
  }).observe(document.querySelector('.posts-container'), {childList: true});
}

main();