Pixiv Ajax Bookmark Mod

You can bookmark without going to the related pages. ページ遷移なく非同期的にブックマークします。

目前為 2018-08-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name             Pixiv Ajax Bookmark Mod
// @namespace        com.SaddestPanda.net.moe
// @version          2.1.1
// @description      You can bookmark without going to the related pages. ページ遷移なく非同期的にブックマークします。
// @include          http://www.pixiv.net/member_illust.php*illust_id=*
// @include          https://www.pixiv.net/member_illust.php*illust_id=*
// @exclude          http://www.pixiv.net/member*mode=manga*
// @exclude          https://www.pixiv.net/member*mode=manga*
// @homepage         https://greasyfork.org/en/scripts/22767-pixiv-ajax-bookmark-mod
// @supportURL       https://greasyfork.org/en/scripts/22767-pixiv-ajax-bookmark-mod/feedback
// @author           qa2 & SaddestPanda
// @require          https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @grant            none
// @run-at           document-idle
// @noframes
// ==/UserScript==

//When you add an illustration to bookmarks also give it a like.
// 1 でしたらブックマークした時、同時に「いいね!」もします。
var givelike = 1;

//R18 illustrations are added as private bookmarks.
// 1 でしたらイラストはR-18であった場合プライベートブックマークにします。
var r18private = 1;

// If set to "1": Always add to private bookmarks list.
//ブックマークする作品をいつも非公開にするかどうか 0:公開 1:非公開
var bkm_restrict = 0;

//Add all tags to the bookmark.
//作品に登録されているすべてのタグをブックマークタグとして追加
var add_all_tags = 1;

var tags, useNewDesign = "", tagsInterval = "";

var TheInterval = setInterval(function () { //Check if the page is using the new design.
	try {
		if (globalInitData.token != null) {
			useNewDesign = 1;
			tagsInterval = setInterval(startingUp, 125);
			clearInterval(TheInterval);
		}
	} catch (e) {}
	try {
		if (pixiv.context.token != null) {
			useNewDesign = 0;
			oldstartingUp();
			clearInterval(TheInterval);
		}
	} catch (e) {}
}, 125);

function startingUp() {
	tags = "";
    //get all tags
	$("footer >").children().each(function () {
		try {
			tags += decodeURIComponent($(this).find("a")[0].href).split("&word=")[1] + " ";
		} catch (e) {}
	});

	if (tags != "") {
        //cancel if already bookmarked
		if ($(".gtm-main-bookmark").length != 1) {
			clearInterval(tagsInterval);
			return;
		} else {
            //Set the button action
			$(".gtm-main-bookmark")[0].href = "javascript:void(0)";
			$(".gtm-main-bookmark").on("click", function () {
				bkm();
				return false;
			});
			//console.log(tags);
			clearInterval(tagsInterval);
		}
	}
}

// ajaxでブックマークする関数
function bkm() {
	//var illustid = $("input[name=illust_id").val();
	//var tt = $("input[name=tt]").val();
	//var type = $("input[name=type]:eq(1)").val();
	//var illustid = pixiv.context.queries.id;
	var illustid = document.location.href.split("illust_id=")[1];
	var url = "https://www.pixiv.net/bookmark_add.php?id=" + illustid;    //https://www.pixiv.net/bookmark_add.php?rest=show&type=illust&p=1&illust_id=70318176
	var tt = globalInitData.token;
	if (!add_all_tags) {
		tags = "";
	}
	//var illusttype = "illust";

	try {
		if ($("footer >")[0].firstChild.innerText == "R-18" && r18private) {
			bkm_restrict = 1;
		}
	} catch (e) {}

	$.ajax({
		url: url,
		type: 'POST',
		dataType: 'json',
		data: {
			mode: "add",
			tt: tt,
			id: illustid,
			from_sid: "",
			type: "illust",
			comment: "",
			tag: tags,
			restrict: bkm_restrict,
			success: function () {
				$(".gtm-main-bookmark svg")[0].style.fill = "#ff4060";
				$(".gtm-main-bookmark svg path")[0].style.fill = "#ff4060";
				$(".gtm-main-bookmark")[0].href = "https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=" + illustid;
				$(".gtm-main-bookmark").unbind("click");
                $(".gtm-main-bookmark").on("click", function (e) {
                    e.preventDefault();
                    window.location = "https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=" + illustid;
                    return false;
                });
			}
		},
	});

	if (givelike) {
		$(".gtm-main-bookmark").parent()[0].nextSibling.children[0].click();
	}
}


/////// BELOW FUNCTIONS ARE ///////
/////// FOR THE OLD DESIGN ///////
function oldstartingUp() {
	tags = "";
	$(".tag > .text").contents().not($('span')).each(function () {
		tags += $(this).text() + " ";
	});

	$(".add-bookmark").on("click", function () {
		//e.preventDefault();
		oldbkm();
		return false;
	});

}

function oldbkm() {
	//var illustid = $("input[name=illust_id").val();
	//var tt = $("input[name=tt]").val();
	//var type = $("input[name=type]:eq(1)").val();
	var illustid = pixiv.context.illustId;
	var url = "https://www.pixiv.net/bookmark_add.php?id=" + illustid;
	var tt = pixiv.context.token;
	var type = pixiv.context.type;

	if ($(".r-18")[0] != null && r18private) {
		bkm_restrict = 1;
	}

	$.ajax({
		url: url,
		type: 'POST',
		dataType: 'json',
		data: {
			mode: "add",
			tt: tt,
			id: illustid,
			type: type,
			//from_sid: "",
			comment: "",
			tag: tags,
			restrict: bkm_restrict,
			success: function () {
				if ($(".add-bookmark").text() == "ブックマークに追加") {
					$(".add-bookmark").html($('<span>', {
						class: 'description',
						html: "ブックマークを編集"
					}));
				} else if ($(".add-bookmark").text() == "添加收藏") {
					$(".add-bookmark").html($('<span>', {
						class: 'description',
						html: "编辑收藏"
					}));
				} else {
					$(".add-bookmark").html($('<span>', {
						class: 'description',
						html: "Edit Bookmark"
					}));
				}
				$(".add-bookmark")[0].classList.add("edit-bookmark");
				$(".add-bookmark")[0].classList.add("bookmarked");
				$(".edit-bookmark")[0].classList.remove("add-bookmark");
			}
		},
	});

	if (givelike) {
		$("._nice-button").trigger("click");
	}
}