// ==UserScript==
// @name Pixiv Ajax Bookmark Mod
// @namespace com.SaddestPanda.net.moe
// @version 2.5.2
// @description You can bookmark without going to the related pages. ページ遷移なく非同期的にブックマークします。
// @include /^https?:\/\/www.pixiv.net\/member_illust.php.*illust_id=.*/
// @include /^https?:\/\/www.pixiv.net\/.*?artworks\/.*/
// @include /^https?:\/\/www.pixiv.net\/users\/.*/
// @include /^https?:\/\/www.pixiv.net\/(member.php|member_illust.php|bookmark.php|mypixiv_all.php)\?id=.*/
// @exclude /^https?:\/\/www.pixiv.net\/member.*mode=manga.*/
// @homepage https://gf.qytechs.cn/en/scripts/22767-pixiv-ajax-bookmark-mod
// @supportURL https://gf.qytechs.cn/en/scripts/22767-pixiv-ajax-bookmark-mod/feedback
// @author qa2 & SaddestPanda
// @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js
// @grant none
// @run-at document-idle
// @noframes
// ==/UserScript==
//TODO: Might convert to mutation method when a remake is required.
//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 = "", tagsInterval = "", TheInterval = "", resCheckerInterval = "", currLocation = "", restartInterval = "", theToken = "";
pageisNew();
function pageisNew() {
clearInterval(restartInterval);
var TheInterval = setInterval(function () {
try {
if ($(".gtm-main-bookmark").length == 1) {
getthetoken();
tagsInterval = setInterval(startingUp, 200);
clearInterval(TheInterval);
}
} catch (e) {}
}, 150);
}
function startingUp() {
tags = "";
clearInterval(resCheckerInterval);
currLocation = document.location.href;
resCheckerInterval = setInterval(restartChecker, 150);
//get all tags
$("footer >").children().each(function () {
try {
tags += decodeURIComponent($(this).find("a")[0].href).split("tags/")[1].split("/artworks")[0] + " ";
} catch (e) {}
});
if (tags != "") {
//cancel if already bookmarked
if ($(".gtm-main-bookmark").length != 1) {
clearInterval(tagsInterval);
return;
} else {
clearInterval(tagsInterval);
//Set the button action
$(".gtm-main-bookmark")[0].href = "javascript:void(0)";
$(".gtm-main-bookmark").on("click", function () {
bkm(-1);
return false;
});
//Set the hover buttons
var hoverButtons = document.createElement("div");
$(".gtm-main-bookmark").after(hoverButtons);
$(".gtm-main-bookmark")[0].nextSibling.outerHTML = '<div id="pabmButtonsContainer" style="position:absolute;width:54px;display:flex;flex-direction:row;justify-content:space-around;background-color:rgba(0,0,0,.2);padding:3px 8px 2px 8px;height:25px;/*! border: 2px black solid; */border-radius:15vw;margin-left:-18px!important;margin-top:-29px;z-index:555;filter:opacity(100%);/*! left: -55px; */display:none"><div class="pabmButtons pabmButtonPublic"><svg xmlns="http://www.w3.org/2000/svg" width="24" viewBox="0 0 24 24" height="24" style="height:22px;margin-top:1px"><path fill="#ff4060" d="M16 3a6 6 0 016 6c0 4.868-3.158 8.815-9.106 11.789a2 2 0 01-1.788 0C5.158 17.815 2 13.868 2 9a6 6 0 016-6c1.425 0 2.85.501 4 1.351C13.15 3.501 14.575 3 16 3z"></path></svg></div><div class="pabmButtons pabmButtonPrivate"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 32 32"><path fill-rule="evenodd" clip-rule="evenodd" d="M21 5.5a7 7 0 017 7c0 5.77-3.703 10.652-10.78 14.61a2.5 2.5 0 01-2.44 0C7.703 23.152 4 18.27 4 12.5a7 7 0 017-7c1.83 0 3.621.914 5 2.328C17.379 6.414 19.17 5.5 21 5.5z" fill="#FF4060"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M29.98 20.523A3.998 3.998 0 0132 24v4a4 4 0 01-4 4h-7a4 4 0 01-4-4v-4c0-1.489.814-2.788 2.02-3.477a5.5 5.5 0 0110.96 0z" fill="#fff"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M28 22a2 2 0 012 2v4a2 2 0 01-2 2h-7a2 2 0 01-2-2v-4a2 2 0 012-2v-1a3.5 3.5 0 117 0v1zm-5-1a1.5 1.5 0 013 0v1h-3v-1z" fill="#1F1F1F"></path></svg></div></div>';
$(".pabmButtonPublic").on("click", function () {
bkm(0);
return false;
});
$(".pabmButtonPrivate").on("click", function () {
bkm(1);
return false;
});
var hoverCss = document.createElement("style");
$(".gtm-main-bookmark").before(hoverCss);
$(".gtm-main-bookmark")[0].previousSibling.outerHTML = '<style id="pabmButtonsStyle">#pabmButtonsContainer:hover,.gtm-main-bookmark:hover~#pabmButtonsContainer{display:flex!important}.pabmButtons>svg:hover{filter:contrast(180%);stroke:#fff;stroke-width:.15em;stroke-opacity:35%}</style>';
}
} else {
clearInterval(tagsInterval);
return;
}
}
function restartChecker() {
if (document.location != currLocation) {
clearInterval(resCheckerInterval);
clearInterval(restartInterval);
restartInterval = setInterval(restartingSoon, 200);
}
}
function restartingSoon() {
try {
if ($(".gtm-main-bookmark")[0].disabled == false) {
pageisNew();
}
} catch (e) {}
}
function getthetoken() {
var gettingCookie = getCookie(dataLayer[0].user_id.toString() + "pixivajaxbm");
if(gettingCookie != "") {
theToken = gettingCookie;
}
if (theToken == "") {
var illustid = $("link[rel=canonical]")[0].href.split("artworks/")[1];
$.ajax({
url: "https://www.pixiv.net/bookmark_add.php?type=illust&illust_id=" + illustid,
type: 'GET',
tryCount: 0,
retryLimit: 2,
success: function(sss) {
//do something
theToken = $(sss).find("form")[1].children[1].value;
//add it to cookies (15 mins and userid)
var expires = (new Date(Date.now()+ 900*1000)).toUTCString();
document.cookie = dataLayer[0].user_id.toString() + "pixivajaxbm=" + theToken + "; expires=" + expires + ";path=/;";
},
error: function(xhr, textStatus, errorThrown) {
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
$.ajax(this);
return;
}
return;
/*
if (textStatus == 'timeout') {
}
if (xhr.status == 500) {
//handle error
} else {
//handle error
}
*/
}
});
}
}
// ajaxでブックマークする関数
function bkm(asPrivate) {
//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 illustid = Object.keys(globalInitData.preload.illust)[0];
var illustid = $("link[rel=canonical]")[0].href.split("artworks/")[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 = theToken;
if (!add_all_tags) {
tags = "";
}
//var illusttype = "illust";
if (asPrivate >= 0) {
bkm_restrict = asPrivate;
} else {
try {
if ($("footer >")[0].firstChild.innerText == "R-18" && r18private) {
bkm_restrict = 1;
}
} catch (e) {}
}
if (givelike) {
$(".gtm-main-bookmark").parent()[0].nextSibling.children[0].click();
}
$.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 () {
if (bkm_restrict) {
//INSERT THE LOCKED HEART (PRIVATE BOOKMARK) SVG https://yoksel.github.io/url-encoder/
$(".gtm-main-bookmark svg")[0].outerHTML = decodeURIComponent("%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='32' height='32' class='j89e3c-1 kcOjCr' style='color: rgb(255, 64, 96);'%3E%3Cdefs%3E%3Cmask id='uid-mask-2'%3E%3Crect x='0' y='0' width='32' height='32' fill='white'%3E%3C/rect%3E%3Cpath d='M16,11.3317089 C15.0857201,9.28334665 13.0491506,7.5 11,7.5%0AC8.23857625,7.5 6,9.73857647 6,12.5 C6,17.4386065 9.2519779,21.7268174 15.7559337,25.3646328%0AC15.9076021,25.4494645 16.092439,25.4494644 16.2441073,25.3646326 C22.7480325,21.7268037 26,17.4385986 26,12.5%0AC26,9.73857625 23.7614237,7.5 21,7.5 C18.9508494,7.5 16.9142799,9.28334665 16,11.3317089 Z' class='j89e3c-0 kBfARi'%3E%3C/path%3E%3C/mask%3E%3C/defs%3E%3Cg mask='url(%23uid-mask-2)'%3E%3Cpath d='%0AM21,5.5 C24.8659932,5.5 28,8.63400675 28,12.5 C28,18.2694439 24.2975093,23.1517313 17.2206059,27.1100183%0AC16.4622493,27.5342993 15.5379984,27.5343235 14.779626,27.110148 C7.70250208,23.1517462 4,18.2694529 4,12.5%0AC4,8.63400691 7.13400681,5.5 11,5.5 C12.829814,5.5 14.6210123,6.4144028 16,7.8282366%0AC17.3789877,6.4144028 19.170186,5.5 21,5.5 Z'%3E%3C/path%3E%3Cpath d='M16,11.3317089 C15.0857201,9.28334665 13.0491506,7.5 11,7.5%0AC8.23857625,7.5 6,9.73857647 6,12.5 C6,17.4386065 9.2519779,21.7268174 15.7559337,25.3646328%0AC15.9076021,25.4494645 16.092439,25.4494644 16.2441073,25.3646326 C22.7480325,21.7268037 26,17.4385986 26,12.5%0AC26,9.73857625 23.7614237,7.5 21,7.5 C18.9508494,7.5 16.9142799,9.28334665 16,11.3317089 Z' class='j89e3c-0 kBfARi'%3E%3C/path%3E%3C/g%3E%3Cpath d='M29.9796 20.5234C31.1865 21.2121 32 22.511 32 24V28%0AC32 30.2091 30.2091 32 28 32H21C18.7909 32 17 30.2091 17 28V24C17 22.511 17.8135 21.2121 19.0204 20.5234%0AC19.2619 17.709 21.623 15.5 24.5 15.5C27.377 15.5 29.7381 17.709 29.9796 20.5234Z' class='j89e3c-2 jTfVcI' style='fill: rgb(255, 255, 255); fill-rule: evenodd; clip-rule: evenodd;'%3E%3C/path%3E%3Cpath d='M28 22C29.1046 22 30 22.8954 30 24V28C30 29.1046 29.1046 30 28 30H21%0AC19.8954 30 19 29.1046 19 28V24C19 22.8954 19.8954 22 21 22V21C21 19.067 22.567 17.5 24.5 17.5%0AC26.433 17.5 28 19.067 28 21V22ZM23 21C23 20.1716 23.6716 19.5 24.5 19.5C25.3284 19.5 26 20.1716 26 21V22H23%0AV21Z' class='j89e3c-3 fZVtyd' style='fill: rgb(31, 31, 31); fill-rule: evenodd; clip-rule: evenodd;'%3E%3C/path%3E%3C/svg%3E");
} else {
}
$(".gtm-main-bookmark svg")[0].style.fill = "#ff4060";
$(".gtm-main-bookmark svg path")[0].style.fill = "white";
$(".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;
});
$("#pabmButtonsContainer")[0].style.visibility="hidden";
//$("#pabmButtonsContainer")[0].remove();
//$("#pabmButtonsStyle")[0].remove();
}
},
});
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}