Pixiv Auto Like

Click **like** automatically in new illust pages

当前为 2018-06-14 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Pixiv Auto Like
// @name:zh-TW   Pixiv 自動點讚
// @description        Click **like** automatically in new illust pages
// @description:zh-TW  在新版頁面自動點讚
// @namespace    https://github.com/FlandreDaisuki
// @version      1.0.2
// @author       FlandreDaisuki
// @include      *://www.pixiv.net/member_illust.php?*&mode=medium
// @include      *://www.pixiv.net/member_illust.php?mode=medium&*
// @grant        none
// @compatible   firefox
// @compatible   chrome
// @noframes
// ==/UserScript==
/* eslint-disable no-restricted-globals, camelcase, no-param-reassign */
/* global globalInitData */

const liked = new Set();

// wait until React ready... fucking slow...
const tid = setInterval(() => {
  const mainImgA = document.querySelector('figure a');
  if (mainImgA) {
    clearInterval(tid);
    main(mainImgA);
  }
}, 500);

function main(targetElement) {
  postLike();

  const config = {
    attributes: true,
    childList: true,
    characterData: true,
    subtree: true,
  };
  const observer = new MutationObserver(postLike);
  observer.observe(targetElement, config);
}

async function postLike() {
  const likeBtn = document.querySelector('button.Ki5EGTG');
  const likeSVG = document.querySelector('svg.v2zpsfm');
  const sp = new URLSearchParams(location.search);
  const illust_id = sp.get('illust_id');

  if (liked.has(illust_id)) {
    changeLikedStyle(likeBtn, likeSVG);
    return;
  }

  if (likeBtn && !likeBtn.classList.contains('_2iDv0r8')) {
    const resp = await fetch('/ajax/illusts/like', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'x-csrf-token': globalInitData.token,
      },
      credentials: 'same-origin',
      body: JSON.stringify({
        illust_id,
      }),
    });

    const data = await resp.json();
    if (!data.error) {
      changeLikedStyle(likeBtn, likeSVG);
      liked.add(illust_id);
    }
  }
}

function changeLikedStyle(likeBtn, likeSVG) {
  if (likeBtn) {
    likeBtn.classList.add('_2iDv0r8');
  }
  if (likeSVG) {
    likeSVG.classList.add('_1YUwQdz');
    likeSVG.innerHTML = `
<path d="M5,7.08578644 L9.29289322,2.79289322 C9.68341751,2.40236893 10.3165825,2.40236893 10.7071068,
  2.79289322 C11.0976311,3.18341751 11.0976311,3.81658249 10.7071068,4.20710678 L5,9.91421356 L2.29289322,
  7.20710678 C1.90236893,6.81658249 1.90236893,6.18341751 2.29289322,5.79289322 C2.68341751,5.40236893 3.31658249,
  5.40236893 3.70710678,5.79289322 L5,7.08578644 Z"></path>`;
  }
}