Greasy Fork is available in English.
help you to open pixiv works by id
当前为
// ==UserScript==
// @name pixiv id helper
// @namespace https://bing.com
// @version 0.1
// @description help you to open pixiv works by id
// @match *://*/*
// @grant GM_openInTab
// ==/UserScript==
(function() {
'use strict';
// get the selected text
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
// check if the text is a valid pixiv id
function isValidPixivId(text) {
var regex = /^\d{5,12}$/; // a number between 5 and 12 digits
return regex.test(text);
}
// generate the pixiv url by id
function getPixivUrl(id) {
var baseUrl = "https://www.pixiv.net/member_illust.php?illust_id=";
return baseUrl + id + "&mode=medium";
}
// open the pixiv url in a new tab
function openPixivUrl(url) {
GM_openInTab(url, {active: true, insert: true});
}
// handle the mouseup event
function handleMouseUp(event) {
var text = getSelectionText(); // get the selected text
if (isValidPixivId(text)) { // check if it is a valid pixiv id
var url = getPixivUrl(text); // generate the pixiv url
var confirm = window.confirm("Do you want to open this pixiv work: " + url + "?"); // ask for confirmation
if (confirm) { // if confirmed
openPixivUrl(url); // open the url in a new tab
}
}
}
// add the event listener to the document
document.addEventListener("mouseup", handleMouseUp, false);
})();