Pixiv Bookmark async

Add a bookmark in Pixiv on background using Fetch API.

当前为 2017-09-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

/*
// ==UserScript==
// @id             pixiv-bookmark-async
// @name           Pixiv Bookmark async
// @version        0.0.1
// @namespace      http://hkgsherlock.github.io/
// @author         hkgsherlock
// @description    Add a bookmark in Pixiv on background using Fetch API.
// @match          https://www.pixiv.net/member_illust.php?*
// @match          https://www.pixiv.net/setting_user.php
// @grant        none
// ==/UserScript==
*/

const PIXIV_API_URL = 'https://www.pixiv.net/rpc/index.php';
const BUTTON_TARGET_QUERY = '.bookmark-container .add-bookmark';

(() => {
	document.querySelector(BUTTON_TARGET_QUERY).addEventListener('click', (e) => {
		e.preventDefault();

		const queryString = document.location.search.substr(1)
			.split('&')
			.map((e) => {
				const [key, value] = e.split('=');
				return {[key]: value};
			})
			.reduce((a,b)=>Object.assign(a,b),{});

		// not ranbu sry (; _ ;)
		// https://google.com/search?q=pixiv+context+token
		const pixiv_context_token = window.pixiv.context.token;
		const illust_id = queryString.illust_id;
		const restricted = !!document.querySelector('.work-info .meta [class^="r-18"]');
		const comment = null;
		const tags = [...document.querySelectorAll('.tags .tag .text')].map((e) => e.innerText).join('+');

		fetch(PIXIV_API_URL, {
			method: "POST",
			headers: {
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
			},
  			credentials: "same-origin",
			body: [
				'mode=save_illust_bookmark',
				`illust_id=${illust_id}`,
				`restrict=${!!restricted?1:0}`,
				'comment'+(!!comment?`=${comment}`:''),
				'tags'+(!!tags?`=${tags}`:''),
				`tt=${pixiv_context_token}`,
			].join('&'),
		})
			.then((r) => r.json())
			.then((data) => {
				if (data.error) throw new Error(message || body.message);
				document.querySelector(BUTTON_TARGET_QUERY)
					.replaceWith(((id) => {
						const a = document.createElement('a');
						a.setAttribute('href', `bookmark_add.php?type=illust&illust_id=${id}`);
						a.classList.add('_bookmark-toggle-button');
						a.classList.add('bookmarked');
						a.classList.add('edit-bookmark');
						a.appendChild((() => {
							const span = document.createElement('span');
							span.classList.add('description');
							span.innerText = "ブックマークを編集";
						    return span;
						})());
						// a.appendChild((() => {
						// 	const span = document.createElement('span');
						// 	span.setAttribute('style', 'animation:nice-zoom-smile 1s forwards cubic-bezier(0.9, -1.5, 0.4, 1);position:absolute;top:-1px;left:33px;width:32px;height:32px;background-image:url(../images/bookmark-star-on.svg);background-position:center;background-repeat:no-repeat;background-size:cover;visibility:hidden;');
						//     return span;
						// })());
						return a;
					})(illust_id));
			})
			.catch((err) => console.error(err));
	});
})();