Bobby's Pixiv Utils

7/2/2024, 8:37:14 PM

目前為 2025-01-29 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Bobby's Pixiv Utils
// @namespace   https://github.com/BobbyWibowo
// @match       *://www.pixiv.net/*
// @match       *://pixiv.net/*
// @icon        https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @grant       GM_addStyle
// @run-at      document-end
// @version     1.0.7
// @author      Bobby Wibowo
// @license     MIT
// @description 7/2/2024, 8:37:14 PM
// @noframes
// ==/UserScript==

(function () {
  'use strict'

  /** CONFIG **/

  const SELECTORS_IMAGE = '.jtUPOE > li, .gmoaNn > li, .hjtPnz > li, .boBnlf > div, .hkzusx > div, .ranking-item';
  const SELECTORS_ILLUST_CONTROLS = '.gMEAWM';
  const SELECTORS_BOTTOM_RIGHT_CONTROLS = '.iHfghO, .cGfNRT, ._layout-thumbnail';

  /** STYLES **/

  GM_addStyle(`
.pu_edit_bookmark {
  color: rgb(245, 245, 245);
  background: rgba(0, 0, 0, 0.32);
  display: block;
  box-sizing: border-box;
  padding: 0px 6px;
  margin-top: 7px;
  margin-right: 2px;
  border-radius: 10px;
  font-weight: bold;
  font-size: 10px;
  line-height: 20px;
  height: 20px;
}

.gMEAWM .pu_edit_bookmark {
  font-size: 12px;
  height: 24px;
  line-height: 24px;
  margin-top: 5px;
  margin-right: 7px;
}

._layout-thumbnail .pu_edit_bookmark {
	position: absolute;
	right: calc(50% - 71px);
	bottom: 4px;
	z-index: 2;
}

.iHfghO, .cGfNRT {
  display: flex;
  justify-content: flex-end;
}
`);

  /** UTILS **/

  const observerFactory = function (option) {
    let options;
    if (typeof option === 'function') {
      options = {
        callback: option,
        node: document.getElementsByTagName('body')[0],
        option: { childList: true, subtree: true }
      };
    } else {
      options = $.extend({
        callback: () => {},
        node: document.getElementsByTagName('body')[0],
        option: { childList: true, subtree: true }
      }, option);
    }
    const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

    const observer = new MutationObserver((mutations, observer) => {
      options.callback.call(this, mutations, observer);
    });

    observer.observe(options.node, options.option);
    return observer;
  };

  /** MAIN **/

  const doIllustControls = element => {
    // Skip if already modified
    if (element.getElementsByClassName('pu_edit_bookmark').length) {
      return;
    }

    const match = window.location.href.match(/artworks\/(\d+)/);
    if (!match || !match[1]) {
      return;
    }

    const elemContainer = document.createElement('div');
    const elem = document.createElement('a');
    elem.className = 'pu_edit_bookmark';
    elem.href = `https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=${match[1]}`;
    elem.innerText = 'Edit bookmark';

    elemContainer.appendChild(elem);
    element.appendChild(elemContainer);
  }

  observerFactory((mutations, observer) => {
    for (let i = 0, len = mutations.length; i < len; i++) {
      const mutation = mutations[i];

      // Whether to change nodes
      if (mutation.type !== 'childList') {
        continue;
      }

      const illustControls = mutation.target.querySelector(SELECTORS_ILLUST_CONTROLS);
      if (illustControls) {
        doIllustControls(illustControls);
      }

      // Find all images
      const images = mutation.target.querySelectorAll(SELECTORS_IMAGE);

      // Modify
      for (const image of images) {
        // Skip if already modified
        if (image.getElementsByClassName('pu_edit_bookmark').length) {
            continue;
        }

        const link = image.querySelector('a[href*="artworks/"]');
        const bottomRightControls = image.querySelector(SELECTORS_BOTTOM_RIGHT_CONTROLS);
        console.log(link, bottomRightControls);
        if (!link || !bottomRightControls) {
          continue;
        }

        const match = link.href.match(/artworks\/(\d+)/);
        if (!match || !match[1]) {
          continue;
        }

        const elemContainer = document.createElement('div');
        const elem = document.createElement('a');
        elem.className = 'pu_edit_bookmark';
        elem.href = `https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=${match[1]}`;
        elem.innerText = 'Edit bookmark';

        elemContainer.appendChild(elem);
        bottomRightControls.insertBefore(elemContainer, bottomRightControls.firstChild);
      }
    }
  });

})()