Pixiv 增强

专注沉浸式体验, 1. 屏蔽广告, 直接访问热门图片 2. 使用users入り的方式进行搜索 3. 搜索pid和uid 4. 下载原图|gif图|gif帧zip|多图zip 5. 显示画师id、画师背景图 6. 自动加载评论 7. 对动态标记作品类型 8. 去除重定向 github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。

当前为 2018-08-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pixiv Plus
  3. // @name:zh-CN Pixiv 增强
  4. // @name:zh-TW Pixiv 增強
  5. // @namespace https://github.com/Ahaochan/Tampermonkey
  6. // @version 0.3.2
  7. // @icon http://www.pixiv.net/favicon.ico
  8. // @description Focus on immersive experience, 1. Block ads, directly access popular images 2. Search using users to search for 3. Search pid and uid 4. Download original image | gif map | gif frame zip | multi-image zip 5. Display artist id , Artist background Figure 6. Automatic loading comments 7. Dynamically mark the work type 8. Remove the redirect github: https://github.com/Ahaochan/Tampermonkey, welcome star and fork.
  9. // @description:zh-CN 专注沉浸式体验, 1. 屏蔽广告, 直接访问热门图片 2. 使用users入り的方式进行搜索 3. 搜索pid和uid 4. 下载原图|gif图|gif帧zip|多图zip 5. 显示画师id、画师背景图 6. 自动加载评论 7. 对动态标记作品类型 8. 去除重定向 github:https://github.com/Ahaochan/Tampermonkey,欢迎star和fork。
  10. // @description:zh-TW 專注沉浸式體驗, 1. 屏蔽廣告, 直接訪問熱門圖片2. 使用users入り的方式進行搜索3. 搜索pid和uid 4. 下載原圖|gif圖|gif幀zip|多圖zip 5. 顯示畫師id 、畫師背景圖6. 自動加載評論7. 對動態標記作品類型8. 去除重定向github:https://github.com/Ahaochan/Tampermonkey,歡迎star和fork。
  11. // @author Ahaochan
  12. // @include http*://www.pixiv.net*
  13. // @match http://www.pixiv.net/
  14. // @connect i.pximg.net
  15. // @license GPL-3.0
  16. // @supportURL https://github.com/Ahaochan/Tampermonkey
  17. // @grant unsafeWindow
  18. // @grant GM.xmlHttpRequest
  19. // @grant GM.setClipboard
  20. // @require https://code.jquery.com/jquery-2.2.4.min.js
  21. // @require https://cdn.bootcss.com/jszip/3.1.4/jszip.min.js
  22. // @require https://cdn.bootcss.com/FileSaver.js/1.3.2/FileSaver.min.js
  23. // @require https://greasyfork.org/scripts/2963-gif-js/code/gifjs.js?version=8596
  24. // @run-at document-end
  25. // @noframes
  26. // ==/UserScript==
  27.  
  28. jQuery(function ($) {
  29. 'use strict';
  30. // 加载依赖
  31.  
  32. // ============================ 全局参数 ====================================
  33. let lang = document.documentElement.getAttribute('lang') || 'en',
  34. globalInitData = unsafeWindow.globalInitData,
  35. illustJson = {};
  36. let illust = function () {
  37. // 1. 判断是否已有作品id(兼容按左右方向键翻页的情况)
  38. let preIllustId = $('body').attr('ahao_illust_id');
  39. let urlIllustId = new URL(location.href).searchParams.get("illust_id");
  40. // 2. 如果illust_id没变, 则不更新json
  41. if (parseInt(preIllustId) === parseInt(urlIllustId)) {
  42. return illustJson;
  43. }
  44. // 3. 如果illust_id变化, 则持久化illust_id, 且同步更新json
  45. $('body').attr('ahao_illust_id', urlIllustId);
  46. $.ajax({url: '/ajax/illust/' + urlIllustId, dataType: 'json', async: false, success: response => illustJson = response.body});
  47. return illustJson;
  48. };
  49. let uid = illust().userId || (pixiv && pixiv.context.userId) || (globalInitData && Object.keys(globalInitData.preload.user)[0]) || 'unknown';
  50. let mimeType = suffix => {
  51. let lib = {png: "image/png", jpg: "image/jpeg", gif: "image/gif"};
  52. return lib[suffix] || 'mimeType[' + suffix + '] not found';
  53. };
  54. let executeMutationObserver = function (option) {
  55. var options = $.extend({
  56. type: 'childList',
  57. attributeName: [],
  58. isValid: () => false,
  59. execute: addedNodes => {}
  60. }, option);
  61.  
  62. // 2.1. MutationObserver 处理
  63. var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  64. var mutationObserver = new MutationObserver(function (mutations) {
  65. mutations.forEach(function (mutation) {
  66. if (options.type !== mutation.type) {
  67. return;
  68. }
  69. if (options.type === 'attributes' && options.attributeName && options.attributeName.indexOf(mutation.attributeName) < 0) {
  70. return;
  71. }
  72. if (mutation.target && typeof mutation.target !== 'object') {
  73. return;
  74. }
  75. var $parent = $(mutation.target).parent();
  76. if (!options.isValid($parent)) {
  77. return;
  78. }
  79. options.execute($parent);
  80.  
  81. });
  82. });
  83. mutationObserver.observe(document.getElementsByTagName('body')[0], {
  84. childList: true,
  85. subtree: true,
  86. attributes: true
  87. });
  88.  
  89. // 2.2. 定时处理, 避免 MutationObserver 来不及加载的情况, 10秒后交由 MutationObserver 接管
  90. var intervalId = setInterval(function () {
  91. var $body = $('body');
  92. if (!options.isValid($body)) {
  93. return;
  94. }
  95. options.execute($body);
  96. }, 1000);
  97. setTimeout(function () {
  98. window.clearInterval(intervalId);
  99. }, 10000);
  100. };
  101.  
  102. // ============================ i18n 国际化 ===============================
  103. let i18nLib = {
  104. ja: {
  105. favorites: 'users入り',
  106. },
  107. en: {
  108. favorites: 'favorites',
  109. illegal: 'illegal',
  110. download: 'download',
  111. download_wait: 'please wait download completed',
  112. copy_to_clipboard: 'copy to Clipboard',
  113. background: 'background',
  114. background_not_found: 'no-background',
  115. loginWarning: 'Pixiv Plus Script Warning! Please login to Pixiv for a better experience! Failure to login may result in unpredictable bugs!',
  116. illust_type_single: '[single pic]',
  117. illust_type_multiple: '[multiple pic]',
  118. illust_type_gif: '[gif pic]',
  119. },
  120. ko: {},
  121. zh: {
  122. favorites: '收藏人数',
  123. illegal: '不合法',
  124. download: '下载',
  125. download_wait: '请等待下载完成',
  126. copy_to_clipboard: '已复制到剪贴板',
  127. background: '背景图',
  128. background_not_found: '无背景图',
  129. loginWarning: 'Pixiv增强 脚本警告! 请登录Pixiv获得更好的体验! 未登录可能产生不可预料的bug!',
  130. illust_type_single: '[单图]',
  131. illust_type_multiple: '[多图]',
  132. illust_type_gif: '[gif图]',
  133. },
  134. 'zh-CN': {},
  135. 'zh-tw': {
  136. favorites: '收藏人數',
  137. illegal: '不合法',
  138. download: '下載',
  139. download_wait: '請等待下載完成',
  140. copy_to_clipboard: '已復製到剪貼板',
  141. background: '背景圖',
  142. background_not_found: '無背景圖',
  143. loginWarning: 'Pixiv增強 腳本警告! 請登錄Pixiv獲得更好的體驗! 未登錄可能產生不可預料的bug!',
  144. illust_type_single: '[單圖]',
  145. illust_type_multiple: '[多圖]',
  146. illust_type_gif: '[gif圖]',
  147. }
  148. };
  149. i18nLib['zh-CN'] = $.extend({}, i18nLib.zh);
  150. // TODO 待翻译
  151. i18nLib.ja = $.extend({}, i18nLib.en, i18nLib.ja);
  152. i18nLib.ko = $.extend({}, i18nLib.en, i18nLib.ko);
  153. let i18n = key => i18nLib[lang][key] || 'i18n[' + lang + '][' + key + '] not found';
  154.  
  155. // ============================ url 页面判断 ==============================
  156. let isArtworkPage = /.+member_illust\.php\?.*illust_id=\d+.*/.test(location.href);
  157.  
  158. let isMemberIndexPage = /.+member.php.*id=\d+.*/.test(location.href);
  159. let isMemberIllustPage = /.+\/member_illust\.php\?id=\d+/.test(location.href);
  160. let isMemberBookmarkPage = /.+\/bookmark\.php\?id=\d+/.test(location.href);
  161. let isMemberFriendPage = /.+\/mypixiv_all\.php\?id=\d+/.test(location.href);
  162. let isMemberDynamicPage = /.+\/stacc\/.+/.test(location.href);
  163. let isMemberPage = isMemberIndexPage || isMemberIllustPage || isMemberBookmarkPage || isMemberFriendPage || isMemberDynamicPage;
  164.  
  165. // ============================ 反混淆 ====================================
  166. let confusedLib = {};
  167. setInterval(function () {
  168. let webpackJsonp = unsafeWindow.webpackJsonp;
  169. let filter = webpackJsonp.map(value => value[1]).filter(value => value && !Array.isArray(value) && typeof value === 'object');
  170. $.each(filter, (index, obj) => {
  171. for (let key in obj) {
  172. if (!obj.hasOwnProperty(key)) {
  173. continue;
  174. }
  175. let tmp = {};
  176. try { obj[key](tmp); } catch(err) { continue; };
  177. if(tmp.hasOwnProperty('exports')) {
  178. confusedLib = $.extend(confusedLib, tmp.exports);
  179. }
  180. }
  181. });
  182. }, 1000);
  183. var confused = function (key) {
  184. return confusedLib[key] || 'confused[' + key + '] not found';
  185. };
  186.  
  187. // 判断是否登录
  188. if (dataLayer[0].login === 'no') {
  189. alert(i18n('loginWarning'));
  190. }
  191.  
  192. // 1. 屏蔽广告, 隐藏搜索页的热门图片遮罩层, 直接访问热门图片
  193. (function () {
  194. // 1. 删除静态添加的广告
  195. $('._premium-lead-tag-search-bar').hide();
  196. $('.popular-introduction-overlay').hide();// 移除热门图片遮罩层
  197.  
  198. // 2. 删除动态添加的广告
  199. executeMutationObserver({
  200. type: 'childList',
  201. isValid: () => true,
  202. execute: function ($parent) {
  203. // 2.1. 隐藏广告
  204. (function () {
  205. var adSelector = ['iframe', '._premium-lead-promotion-banner', '.' + confused('alertContainer')];
  206. var $ad = $parent.find(adSelector.join(','));
  207. if (!$ad.length) {
  208. return;
  209. }
  210. $ad.hide();
  211. })();
  212.  
  213. // 2.2. 移除class
  214. (function () {
  215. var $figure = $parent.find('figure');
  216. if (!$figure.length) {
  217. return false;
  218. }
  219. var classSelector = ['.' + confused('blur')];
  220. var $class = $parent.find(classSelector.join(','));
  221. if (!$class.length) {
  222. return;
  223. }
  224. $class.removeClass(classSelector.map(function (value) { return value.replace('.', ''); }).join(' '));
  225. })();
  226. }
  227. });
  228. })();
  229.  
  230. // 2. 使用users入り的方式进行搜索, 优先显示高质量作品
  231. (function () {
  232. var label = i18n('favorites'); // users入り
  233.  
  234. // 1. 初始化通用页面UI
  235. (function () {
  236. if (isArtworkPage) {
  237. return;
  238. }
  239. console.log("初始化通用页面 按收藏数搜索");
  240. var icon = $('._discovery-icon').attr('src');
  241. $('.navigation-menu-right').append(
  242. '<div class="menu-group">' +
  243. ' <a class="menu-item js-click-trackable-later">' +
  244. ' <img class="_howto-icon" src="' + icon + '">' +
  245. ' <span class="label">' + label + ':</span>' +
  246. ' <select id="select-ahao-favorites">' +
  247. ' <option value=""></option>' +
  248. ' <option value="10000users入り">10000users入り</option>' +
  249. ' <option value="5000users入り" > 5000users入り</option>' +
  250. ' <option value="1000users入り" > 1000users入り</option>' +
  251. ' <option value="500users入り" > 500users入り</option>' +
  252. ' <option value="300users入り" > 300users入り</option>' +
  253. ' <option value="100users入り" > 100users入り</option>' +
  254. ' <option value="50users入り" > 50users入り</option>' +
  255. ' </select>' +
  256. ' </a>' +
  257. '</div>');
  258. })();
  259.  
  260. // 2. 初始化作品页面UI
  261. (function () {
  262. if (!isArtworkPage) {
  263. return;
  264. }
  265. console.log("初始化作品页面 按收藏数搜索");
  266. executeMutationObserver({
  267. type: 'childList',
  268. isValid: function ($parent) {
  269. // 1. 判断是否添加完毕
  270. if (!!$parent.find('#select-ahao-favorites').length) {
  271. return false;
  272. }
  273.  
  274. // 2. 判断[发现]节点是否加入dom
  275. var $discovery = $parent.find('a[href="/discovery"]');
  276. if (!$discovery.length) {
  277. return false;
  278. }
  279. return true;
  280. },
  281. execute: function ($parent) {
  282. var $discovery = $parent.find('a[href="/discovery"]');
  283. // 3. clone [发现]节点, 移除href属性, 避免死循环
  284. var $tabGroup = $discovery.closest('div');
  285. var $tab = $discovery.closest('ul').clone();
  286. $tab.find('a[href="/discovery"]').attr('href', 'javascript:void(0)');
  287.  
  288. // 4. 加入dom中
  289. $tabGroup.prepend($tab);
  290. $tab.find('a').contents().last()[0].textContent = label;
  291. $tab.find('a').after('' +
  292. '<select id="select-ahao-favorites">' +
  293. ' <option value=""></option>' +
  294. ' <option value="10000users入り">10000users入り</option>' +
  295. ' <option value="5000users入り" > 5000users入り</option>' +
  296. ' <option value="1000users入り" > 1000users入り</option>' +
  297. ' <option value="500users入り" > 500users入り</option>' +
  298. ' <option value="300users入り" > 300users入り</option>' +
  299. ' <option value="100users入り" > 100users入り</option>' +
  300. ' <option value="50users入り" > 50users入り</option>' +
  301. '</select>');
  302. }
  303. });
  304. })();
  305.  
  306. // 3. 如果已经有搜索字符串, 就在改变选项时直接搜索
  307. $('body').on('change', '#select-ahao-favorites', function () {
  308. if (!!$('input[name="word"]').val()) {
  309. $('form[action="/search.php"]').submit();
  310. }
  311. });
  312.  
  313. // 4. 在提交搜索前处理搜索关键字
  314. $('form[action="/search.php"]').submit(function () {
  315. var $text = $(this).find('input[name="word"]');
  316. var $favorites = $('#select-ahao-favorites');
  317. // 3.1. 去除旧的搜索选项
  318. $text.val((index, val) => val.replace(/\d*users入り/g, ''));
  319. // 3.2. 去除多余空格
  320. $text.val((index, val) => val.replace(/\s\s+/g, ' '));
  321. // 3.3. 添加新的搜索选项
  322. $text.val((index, val) => val + ' ' + $favorites.val());
  323. });
  324. })();
  325.  
  326. // 3. 追加搜索pid和uid功能
  327. (function () {
  328. // 1. 初始化通用页面UI
  329. (function () {
  330. if (isArtworkPage) {
  331. return;
  332. }
  333. console.log("初始化通用页面 搜索UID和PID");
  334. var initSearch = function (option) {
  335. var options = $.extend({right: '0px', placeholder: '', url: ''}, option);
  336.  
  337. // 1. 初始化表单UI
  338. var $form = $('<form class="ui-search" ' +
  339. ' style="position: static;width: 100px;">' +
  340. '<div class="container" style="width:80%;">' +
  341. ' <input class="ahao-input" placeholder="' + options.placeholder + '" style="width:80%;"/>' +
  342. '</div>' +
  343. '<input type="submit" class="submit sprites-search-old" value="">' +
  344. '</form>');
  345. var $div = $('<div class="ahao-search"></div>').css('position', 'absolute')
  346. .css('bottom', '44px')
  347. .css('height', '30px')
  348. .css('right', options.right);
  349. $div.append($form);
  350. $('#suggest-container').before($div);
  351.  
  352. // 2. 绑定submit事件
  353. $form.submit(function (e) {
  354. e.preventDefault();
  355.  
  356. var $input = $(this).find('.ahao-input');
  357. var id = $input.val();
  358. // 2.1. ID 必须为纯数字
  359. if (!/^[0-9]+$/.test(id)) {
  360. var label = options.placeholder + i18n('illegal');
  361. alert(label);
  362. return;
  363. }
  364. // 2.2. 新窗口打开url
  365. var url = option.url + id;
  366. window.open(url);
  367. // 2.3. 清空input等待下次输入
  368. $input.val('');
  369. });
  370. };
  371. // 1. UID搜索
  372. initSearch({right: '235px', placeholder: 'UID', url: 'https://www.pixiv.net/member.php?id='});
  373. // 2. PID搜索
  374. initSearch({right: '345px', placeholder: 'PID', url: 'https://www.pixiv.net/member_illust.php?mode=medium&illust_id='});
  375. })();
  376.  
  377. // 2. 初始化作品页面UI
  378. (function () {
  379. if (!isArtworkPage) {
  380. return;
  381. }
  382. console.log("初始化作品页面 搜索UID和PID");
  383. executeMutationObserver({
  384. type: 'childList',
  385. isValid: function ($parent) {
  386. // 1. 判断是否添加完毕
  387. if (!!$parent.find('.ahao-search').length) {
  388. return false;
  389. }
  390.  
  391. // 2. 判断 form 节点是否加入dom
  392. var $form = $parent.find('form[action="/search.php"]');
  393. if (!$form.length) {
  394. return false;
  395. }
  396. return true;
  397. },
  398. execute: function ($parent) {
  399. var $form = $parent.find('form[action="/search.php"]');
  400. // 3. 使用flex布局包裹
  401. var $flexBox = $('<div></div>').css('display', 'flex');
  402. $form.wrap($flexBox);
  403. $flexBox = $form.closest('div');
  404.  
  405. var initSearch = function (option) {
  406. var options = $.extend({
  407. placeholder: '',
  408. url: ''
  409. }, option);
  410.  
  411. // 4. clone form表单
  412. var $cloneForm = $form.clone();
  413. $cloneForm.attr('action', '').addClass('ahao-search').css('margin-right', '15px').css('width', '126px');
  414. $cloneForm.find('input[name="s_mode"]').remove(); // 只保留一个input
  415. $cloneForm.find('input:first').attr('placeholder', options.placeholder).attr('name', options.placeholder).css('width', '64px');
  416. $flexBox.prepend($cloneForm);
  417.  
  418. // 5. 绑定submit事件
  419. $cloneForm.submit(function (e) {
  420. e.preventDefault();
  421.  
  422. var $input = $(this).find('input[name="' + options.placeholder + '"]');
  423. var id = $input.val();
  424. // ID 必须为纯数字
  425. if (!/^[0-9]+$/.test(id)) {
  426. var label = options.placeholder + i18n('illegal');
  427. alert(label);
  428. return;
  429. }
  430. // 新窗口打开url
  431. var url = option.url + id;
  432. window.open(url);
  433. // 清空input等待下次输入
  434. $input.val('');
  435. });
  436. };
  437. // 1. UID搜索
  438. initSearch({placeholder: 'UID', url: 'https://www.pixiv.net/member.php?id='});
  439. // 2. PID搜索
  440. initSearch({
  441. placeholder: 'PID',
  442. url: 'https://www.pixiv.net/member_illust.php?mode=medium&illust_id='
  443. });
  444. }
  445. });
  446. })();
  447. })();
  448.  
  449. // 4. 单张图片替换为原图格式. 追加下载按钮, 下载gif图、gif的帧压缩包、多图
  450. (function () {
  451. if (!isArtworkPage) {
  452. return;
  453. }
  454. // 1. 初始化 下载按钮, 复制分享按钮并旋转180度
  455. let initDownloadBtn = function (option) {
  456. let options = $.extend({$parent: undefined, id: '', text: '', clickFun: ()=>{}}, option);
  457. let $shareButtonContainer = options.$parent.find('.' + confused('shareButtonContainer'));
  458. let $downloadButtonContainer = $shareButtonContainer.clone();
  459. $downloadButtonContainer.addClass('ahao-download-btn')
  460. .attr('id', options.id)
  461. .removeClass(confused('shareButtonContainer'))
  462. .css('margin-right', '10px')
  463. .css('position', 'relative')
  464. .append('<p>'+options.text+'</p>');
  465. $downloadButtonContainer.find('button').css('transform', 'rotate(180deg)')
  466. .on('click', options.clickFun);
  467. $shareButtonContainer.after($downloadButtonContainer);
  468. };
  469.  
  470. executeMutationObserver({
  471. type: 'attributes',
  472. attributeName: ['src', 'srcset'],
  473. isValid: function ($parent) {
  474. // 1. 判断 执行完毕
  475. var $img = $parent.find('img.' + confused('illust'));
  476. let isImgExist = !!$img.length;
  477. let isImgSrcOriginal = /.+original.+/.test($img.attr('src'));
  478. let isImgSrcsetEmpty = $img.attr('srcset');
  479. if (!isImgExist || (isImgSrcOriginal && !isImgSrcsetEmpty)) {
  480. return false;
  481. }
  482. return true;
  483. },
  484. execute: function ($parent) {
  485. // 1. 单图、多图、gif图三种模式
  486. var moreMode = !!$('a.' + confused('mangaViewLink')).length;
  487. var gifMode = !!$('button.' + confused('play')).length;
  488. var singleMode = !moreMode && !gifMode;
  489. if (!singleMode) {
  490. return;
  491. }
  492. console.log('下载单图');
  493.  
  494. // 2. 替换大图
  495. var $img = $parent.find('img.' + confused('illust'));
  496. let url = illust().urls.original;
  497. $img.attr('src', url).attr('srcset', '');
  498.  
  499. }
  500. });
  501. executeMutationObserver({
  502. type: 'childList',
  503. isValid: function ($parent) {
  504. // 1. 判断 figure 节点是否加入dom
  505. var $figure = $parent.find('figure');
  506. if (!$figure.length) {
  507. return false;
  508. }
  509.  
  510. // 2. 判断 执行完毕
  511. var $sharBtn = $parent.find('.' + confused('shareButtonContainer'));
  512. if (!$sharBtn.length || !!$parent.find('.ahao-download-btn').length) {
  513. return false;
  514. }
  515. return true;
  516. },
  517. execute: function ($parent) {
  518. // 1. 单图、多图、gif图三种模式
  519. var moreMode = !!$('a.' + confused('mangaViewLink')).length;
  520. var gifMode = !!$('button.' + confused('play')).length;
  521. var singleMode = !moreMode && !gifMode;
  522. if (!gifMode) {
  523. return;
  524. }
  525. console.log('下载gif图');
  526.  
  527. // 2. 从 pixiv 官方 api 获取 gif 的数据, 要求 async:false, 否则页面混乱
  528. $.ajax({url: '/ajax/illust/' + illust().illustId + '/ugoira_meta',
  529. dataType: 'json', async: false,
  530. success: response => {
  531. // 2.1. 初始化 zip 下载按钮
  532. initDownloadBtn({
  533. $parent: $parent,
  534. id: 'ahao-download-zip',
  535. text: 'zip',
  536. clickFun: () => window.open(response.body.originalSrc)
  537. });
  538.  
  539. // 2.2. 初始化 gif 下载按钮
  540. // GIF_worker_URL 来自 https://greasyfork.org/scripts/2963-gif-js/code/gifjs.js?version=8596
  541. let gifUrl;
  542. let gif = new GIF({workers: 2, quality: 10, workerScript: GIF_worker_URL});
  543. let gifFrames = [];
  544. $.each(response.body.frames, function (index, frame) {
  545. let url = illust().urls.original.replace('ugoira0.', 'ugoira'+index+'.');
  546. GM.xmlHttpRequest({
  547. method: 'GET', url: url,
  548. headers: {referer: 'https://www.pixiv.net/'},
  549. overrideMimeType: 'text/plain; charset=x-user-defined',
  550. onload: function (xhr) {
  551. // 2.1. 转为blob类型
  552. let r = xhr.responseText, data = new Uint8Array(r.length), i = 0;
  553. while (i < r.length) {
  554. data[i] = r.charCodeAt(i);
  555. i++;
  556. }
  557. let suffix = url.split('.').splice(-1);
  558. let blob = new Blob([data], {type: mimeType(suffix)});
  559.  
  560. // 2.2. 压入gifFrames数组中, 手动同步sync
  561. let img = document.createElement('img');
  562. img.src = URL.createObjectURL(blob);
  563. img.width = illust().width;
  564. img.height = illust().height;
  565. gifFrames[index] = {frame: img, option: {delay: frame.delay}};
  566.  
  567. let loadFramesLength = Object.keys(gifFrames).length;
  568. // 2.3. 若xhr执行完毕, 则加载gif
  569. if(loadFramesLength >= response.body.frames.length){
  570. $.each(gifFrames, function (index, frame) {
  571. gif.addFrame(frame.frame, frame.option);
  572. });
  573. gif.render();
  574. }
  575. }
  576. });
  577. });
  578. gif.on('progress', function (pct) {
  579. $('#ahao-download-gif').find('p').text('gif '+parseInt(pct*100)+'%');
  580. });
  581. gif.on('finished', function(blob) {
  582. gifUrl = URL.createObjectURL(blob);
  583.  
  584. let $a = $('<a id="ahao-download-gif" ></a>')
  585. .attr('href', gifUrl)
  586. .attr('download', illust().illustId+'.gif');
  587. $('#ahao-download-gif').find('button').wrap($a);
  588. });
  589.  
  590. initDownloadBtn({
  591. $parent: $parent,
  592. id: 'ahao-download-gif',
  593. text: 'gif 0%',
  594. clickFun: function () {
  595. if (!gifUrl) {
  596. alert('Gif未加载完毕, 请稍等片刻!');
  597. return;
  598. }
  599. // Adblock 禁止直接打开 blob url, https://github.com/jnordberg/gif.js/issues/71#issuecomment-367260284
  600. // window.open(gifUrl);
  601. }
  602. });
  603. }
  604. });
  605.  
  606.  
  607. }
  608. });
  609. executeMutationObserver({
  610. type: 'childList',
  611. isValid: function ($parent) {
  612. // 1. 判断 figure 节点是否加入dom
  613. var $figure = $parent.find('figure');
  614. if (!$figure.length) {
  615. return false;
  616. }
  617.  
  618. // 2. 判断 执行完毕
  619. var $sharBtn = $parent.find('.' + confused('shareButtonContainer'));
  620. if (!$sharBtn.length || !!$parent.find('.ahao-download-btn').length) {
  621. return false;
  622. }
  623. return true;
  624. },
  625. execute: function ($parent) {
  626. // 1. 单图、多图、gif图三种模式
  627. var moreMode = !!$('a.' + confused('mangaViewLink')).length;
  628. var gifMode = !!$('button.' + confused('play')).length;
  629. var singleMode = !moreMode && !gifMode;
  630. if (!moreMode) {
  631. return;
  632. }
  633. console.log('下载多图');
  634.  
  635. // 2. 初始化 图片数量, 图片url
  636. let zip = new JSZip();
  637. let downloaded = 0; // 下载完成数量
  638. let num = illust().pageCount; // 下载目标数量
  639. let url = illust().urls.original;
  640. let imgUrls = Array(parseInt(num)).fill()
  641. .map((value, index) => url.replace(/_p\d\./, '_p' + index + '.'));
  642.  
  643. // 3. 初始化 下载按钮, 复制分享按钮并旋转180度
  644. initDownloadBtn({
  645. $parent: $parent,
  646. id: 'ahao-download-gif',
  647. text: i18n('download') + '0/' + num,
  648. clickFun: function () {
  649. // 3.1. 手动sync, 避免下载不完全
  650. if (downloaded < num) {
  651. alert(i18n('download_wait'));
  652. return;
  653. }
  654. // 3.2. 使用jszip.js和FileSaver.js压缩并下载图片
  655. zip.generateAsync({type: 'blob', base64: true})
  656. .then(content => saveAs(content, illust().illustId + '.zip'));
  657. }
  658. });
  659.  
  660. // 4. 下载图片, https://wiki.greasespot.net/GM.xmlHttpRequest
  661. $.each(imgUrls, function (index, url) {
  662. GM.xmlHttpRequest({
  663. method: 'GET', url: url,
  664. headers: {referer: 'https://www.pixiv.net/'},
  665. overrideMimeType: 'text/plain; charset=x-user-defined',
  666. onload: function (xhr) {
  667. // 4.1. 转为blob类型
  668. let r = xhr.responseText, data = new Uint8Array(r.length), i = 0;
  669. while (i < r.length) {
  670. data[i] = r.charCodeAt(i);
  671. i++;
  672. }
  673. let suffix = url.split('.').splice(-1);
  674. let blob = new Blob([data], {type: mimeType(suffix)});
  675.  
  676. // 4.2. 压缩图片
  677. zip.file(illust().illustId + '_' + index + '.' + suffix, blob, {binary: true});
  678.  
  679. // 4.3. 手动sync, 避免下载不完全的情况
  680. downloaded++;
  681. $('.ahao-download-btn').find('p').html(i18n('download') + '' + downloaded + '/' + num);
  682. }
  683. });
  684. });
  685. }
  686. });
  687. })();
  688.  
  689. // 5. 在画师页面和作品页面显示画师id、画师背景图
  690. (function () {
  691. // 画师页面UI
  692. (function () {
  693. if (!isMemberPage) {
  694. return;
  695. }
  696.  
  697. // 1. 获取用户名的元素
  698. var $username = $('a.user-name');
  699.  
  700. // 2. 显示画师背景图
  701. var url = $('body').css('background-image').replace('url(', '').replace(')', '').replace(/"/gi, "");
  702. var $div = $('<div style="text-align: center"></div>');
  703. if (!!url && url !== 'none') {
  704. $div.append('<img src="' + url + '" width="10%">' +
  705. '<a target="_blank" href="' + url + ' ">' + i18n('background') + '</a>');
  706. } else {
  707. $div.append('<span>' + i18n('background_not_found') + '</span>');
  708. }
  709. $username.after($div);
  710.  
  711. // 3. 显示画师id, 点击自动复制到剪贴板
  712. var $uid = $('<span>UID: ' + uid + '</span>')
  713. .on('click', function () {
  714. var $this = $(this);
  715. $this.text('UID' + i18n('copy_to_clipboard'));
  716. GM.setClipboard(uid);
  717. setTimeout(function () {
  718. $this.text('UID: ' + uid);
  719. }, 2000);
  720. });
  721. $username.after($uid);
  722. })();
  723. // 作品页面UI
  724. (function () {
  725. if (!isArtworkPage) {
  726. return;
  727. }
  728.  
  729. executeMutationObserver({
  730. type: 'childList',
  731. isValid: function ($parent) {
  732. // 1. 判断 画师名称 节点是否加入dom
  733. var $authorName = $parent.find('.' + confused('authorTopRow'));
  734. if (!$authorName.length) {
  735. return false;
  736. }
  737.  
  738. // 2. 判断 执行完毕
  739. var $uid = $parent.find('#ahao-uid'), $background = $parent.find('#ahao-background');
  740. if (!!$uid.length || !!$background.length) {
  741. return false;
  742. }
  743. return true;
  744. },
  745. execute: function ($parent) {
  746. console.log("显示画师id和背景图");
  747. let $authorTopRow = $parent.find('.' + confused('authorTopRow'));
  748. let $authorMeta = $authorTopRow.children('div.' + confused('authorMeta'));
  749.  
  750. // 1. 显示画师背景图
  751. var background = globalInitData.preload.user[uid].background;
  752. var url = (background && background.url) || '';
  753. var $authorSecondRow = $authorTopRow.clone().attr('id', 'ahao-background');
  754. $authorSecondRow.children('a').remove();
  755. $authorSecondRow.prepend('<img src="' + url + '" width="10%"/>');
  756. $authorSecondRow.find('div a').attr('href', !!url ? url : 'javascript:void(0)').attr('target', '_blank')
  757. .text(!!url ? i18n('background') : i18n('background_not_found'));
  758. $authorTopRow.after($authorSecondRow);
  759.  
  760. // 2. 显示画师id, 点击自动复制到剪贴板
  761. var $uid = $authorMeta.clone();
  762. $uid.find('a').attr('href', 'javascript:void(0)').attr('id', 'ahao-uid').text('UID: ' + uid);
  763. $uid.on('click', function () {
  764. var $this = $(this);
  765. $this.find('a').text('UID' + i18n('copy_to_clipboard'));
  766. GM.setClipboard(uid);
  767. setTimeout(function () {
  768. $this.find('a').text('UID: ' + uid);
  769. }, 2000);
  770. });
  771. $authorMeta.after($uid);
  772. }
  773. });
  774. })();
  775. })();
  776.  
  777. // 6. 自动加载评论
  778. (function () {
  779. if (!isArtworkPage) {
  780. return;
  781. }
  782. executeMutationObserver({
  783. type: 'childList',
  784. isValid: function ($parent) {
  785. // 1. 判断 查看更多评论 节点是否加入dom
  786. var $showMoreButton = $parent.find('.' + confused('showMoreButton'));
  787. if (!$showMoreButton.length) {
  788. return false;
  789. }
  790. return true;
  791. },
  792. execute: function ($parent) {
  793. $parent.find('.' + confused('showMoreButton')).click();
  794. }
  795. });
  796. })();
  797.  
  798. // 7. 对画师页动态中的图片标记作品类型
  799. (function () {
  800. if(!isMemberDynamicPage) {
  801. return;
  802. }
  803.  
  804. executeMutationObserver({
  805. type: 'childList',
  806. isValid: function ($parent) {
  807. // 1. 判断 figure 节点是否加入dom
  808. let $title = $parent.find('.stacc_ref_illust_title');
  809. if (!$title.length) {
  810. return false;
  811. }
  812. return true;
  813. },
  814. execute: function ($parent) {
  815. $parent.find('.stacc_ref_illust_title').each(function () {
  816. let $a = $(this).find('a');
  817. // 1. 已经添加过标记的就不再添加
  818. if(!!$a.attr('ahao-illust-id')){
  819. return;
  820. }
  821. // 2. 获取pid, 设置标记避免二次生成
  822. let illustId = new URL(location.origin + '/' + $a.attr('href')).searchParams.get('illust_id');
  823. $a.attr('ahao-illust-id', illustId);
  824. // 3. 调用官方api, 判断作品类型
  825. $.ajax({
  826. url: '/ajax/illust/' + illustId, dataType: 'json',
  827. success: response => {
  828. let illustType = parseInt(response.body.illustType);
  829. let isMultiPic = parseInt(response.body.pageCount) > 1;
  830. switch (illustType) {
  831. case 0:
  832. case 1:$a.after('<p>' + (isMultiPic ? i18n('illust_type_multiple') : i18n('illust_type_single')) + '</p>');break;
  833. case 2:$a.after('<p>'+i18n('illust_type_gif')+'</p>');break;
  834. }
  835. }
  836. });
  837. })
  838.  
  839. }
  840. });
  841. })();
  842.  
  843. // 8. 对jump.php取消重定向
  844. $('a[href*="jump.php"]').each(function () {
  845. let $this = $(this);
  846. let href = $this.attr('href').replace(/\/?jump.php\?/, '');
  847. $this.attr('href', decodeURIComponent(href));
  848. });
  849. });