Pixiv AI Tag

对Pixiv中的AI生成图像添加一个标注

  1. // ==UserScript==
  2. // @license MIT
  3. // @name Pixiv AI Tag
  4. // @description 对Pixiv中的AI生成图像添加一个标注
  5. // @author BAKAOLC
  6. // @version 0.3
  7. // @icon http://www.pixiv.net/favicon.ico
  8. // @match *://www.pixiv.net/*
  9. // @namespace none
  10. // @grant none
  11. // @supportURL https://github.com/BAKAOLC/Tampermonkey-Script
  12. // @homepageURL https://github.com/BAKAOLC/Tampermonkey-Script
  13. // @noframes
  14. // ==/UserScript==
  15.  
  16. let config = {
  17. //查询间隔,时间单位为毫秒,0代表无延时
  18. query_delay: 0,
  19. /*是否移除AI作品的预览图
  20. * 0 不移除
  21. * 1 仅屏蔽图像显示
  22. * 2 从网页中移除
  23. */
  24. remove_image: 0,
  25. };
  26.  
  27. const common_selector = "div.sc-k3uf3r-8>div>div>div>div>a";
  28. const selector = [
  29. {
  30. url: /pixiv.net\/(cate_r18|manga|en\/$|$)/,
  31. sel: "ul div>div>div:nth-of-type(1)>a",
  32. parent: 4,
  33. },
  34. {
  35. url: /pixiv.net\/(en\/)?artworks/,
  36. sel: "ul div>div>div>a, main nav div>div>div>a",
  37. parent: 4,
  38. },
  39. {
  40. url: "pixiv.net/bookmark_new_illust",
  41. sel: "ul div>div>div>a",
  42. parent: 4,
  43. },
  44. {
  45. url: "pixiv.net/contest",
  46. sel: ".thumbnail-container>a",
  47. parent: 0,
  48. },
  49. {
  50. url: "pixiv.net/discovery",
  51. sel: "ul div>div>div:nth-of-type(1)>a",
  52. parent: 4,
  53. },
  54. {
  55. url: "pixiv.net/new_illust",
  56. sel: "ul div>div:nth-of-type(1)>div>a",
  57. parent: 4,
  58. },
  59. {
  60. url: "pixiv.net/ranking",
  61. sel: ".ranking-image-item>a",
  62. parent: 0,
  63. },
  64. {
  65. url: /pixiv.net\/request($|\/(complete|creators)\/(illust|manga|ugoira))/,
  66. sel: "ul div>div:nth-of-type(1)>a",
  67. parent: 4,
  68. },
  69. {
  70. url: /pixiv.net\/(en\/)?tags/,
  71. sel: "ul div>div>div>a",
  72. parent: 4,
  73. },
  74. {
  75. url: /pixiv.net\/(en\/)?users/,
  76. sel: "ul div>div:nth-of-type(1)>div:nth-of-type(1)>a, ul div>div div>div:nth-of-type(1)>a:nth-child(1)",
  77. parent: 4,
  78. },
  79. {
  80. url: /pixiv.net\/user\/\d+\/series\/\d+/,
  81. sel: "ul div>div>div>a",
  82. parent: 4,
  83. },
  84. ];
  85. const parent_list = ["sc-7uv8pt-1, sc-l7cibp-2, sc-xsxgxe-0"]
  86.  
  87. let query_array = [];
  88. let query_record = {};
  89. (function () {
  90. add_style();
  91. selector.map(
  92. (rule) =>
  93. (rule.sel = (rule.sel + ", " + common_selector)
  94. .split(",")
  95. .map((n) => n + `[href*="/artworks/"]:not(.add_ai_tag)`)
  96. .join(","))
  97. );
  98. start_interval();
  99. new MutationObserver(function () {
  100. let rule = selector.find((s) => location.href.match(s.url));
  101. let illusts = rule ? document.querySelectorAll(rule.sel) : [];
  102. if (illusts.length) add_ai_tag_delay(illusts, rule.parent);
  103. }).observe(document.body, { childList: true, subtree: true });
  104. })();
  105.  
  106. function add_style() {
  107. document.head.insertAdjacentHTML("beforeend", `
  108. <style id="css_add_ai_tag">
  109. .add_ai_tag_view {
  110. padding: 0px 6px;
  111. border-radius: 3px;
  112. color: rgb(255, 255, 255);
  113. background: rgb(96, 64, 255);
  114. font-weight: bold;
  115. font-size: 10px;
  116. line-height: 16px;
  117. user-select: none;
  118. }
  119. </style>
  120. `);
  121. }
  122.  
  123. function start_interval() {
  124. if (config.query_delay > 0) {
  125. setInterval(async function () {
  126. if (query_array.length > 0) {
  127. let data = query_array.shift();
  128. query_illust(data.id, data.node, data.depth);
  129. }
  130. }, config.query_delay);
  131. }
  132. else {
  133. setInterval(async function () {
  134. while (query_array.length > 0) {
  135. let data = query_array.shift();
  136. query_illust(data.id, data.node, data.depth);
  137. }
  138. }, 100);
  139. }
  140. }
  141.  
  142. async function add_ai_tag_delay(illusts, parent_depth) {
  143. illusts.forEach(async (a) => {
  144. a.classList.add("add_ai_tag");
  145. let id = a.href.split("/artworks/").pop();
  146. query_array.push({
  147. id: id,
  148. node: a,
  149. depth: parent_depth,
  150. });
  151. });
  152. }
  153.  
  154. async function query_illust(id, node, depth) {
  155. if (!query_record[id]) {
  156. let json = await (
  157. await fetch(
  158. "https://www.pixiv.net/ajax/illust/" + id,
  159. { credentials: "omit" }
  160. )
  161. ).json();
  162. query_record[id] = json.body.aiType == 2;
  163. }
  164. if (query_record[id]) {
  165. add_ai_tag_view(node, depth);
  166. }
  167. }
  168.  
  169. function getParentNodeWithDepth(node, depth) {
  170. while (depth > 0) {
  171. if (node.parentNode)
  172. node = node.parentNode;
  173. else
  174. return null;
  175. depth--;
  176. }
  177. return node;
  178. }
  179.  
  180. function add_ai_tag_view(node, depth) {
  181. try {
  182. switch (config.remove_image) {
  183. case 1:
  184. node.querySelector("div>img").outerHTML = "<h5>AI Artwork</h5>";
  185. break;
  186. case 2:
  187. var parent = getParentNodeWithDepth(node, depth ?? 0);
  188. if (parent) {
  189. var parent2 = parent.parentNode;
  190. if (parent2) {
  191. parent2.removeChild(parent);
  192. }
  193. }
  194. return;
  195. }
  196. } catch { };
  197. let div12 = node.querySelector("div.sc-rp5asc-12");
  198. if (div12) {
  199. let div13 = div12.querySelector("div.sc-rp5asc-13");
  200. if (!div13) {
  201. div12.insertAdjacentHTML("afterbegin", `<div class="sc-rp5asc-13 liXhix"></div>`);
  202. div13 = div12.querySelector("div.sc-rp5asc-13");
  203. }
  204. div13.insertAdjacentHTML("afterbegin", `<div class="sc-1ovn4zb-0 add_ai_tag_view">AI</div>`);
  205. }
  206. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址