// ==UserScript== // @name Pixiv View Util // @namespace Pixiv View Util // @description 閲覧専用のユーティリティです。(1)各イラストの閲覧ページや作者ごとの画像一覧ページのレイアウトを変更します。(2)pixivサイト内でポップアップ機能を有効化します。this is some utility funcitions for pixiv.(1)change the layout of illust pages and auther's pages. (2)add popup tool. // @author sotoba // @match https://www.pixiv.net/bookmark_new_illust.php* // @match https://www.pixiv.net/discovery* // @match https://www.pixiv.net/bookmark_detail.php?illust_id=* // @match https://www.pixiv.net/bookmark_add.php?id=* // @match https://www.pixiv.net/member_illust.php* // @match https://www.pixiv.net/ranking.php?mode=* // @match https://www.pixiv.net/member.php?id=* // @match https://www.pixiv.net/bookmark.php* // @match https://www.pixiv.net/search.php* // @match https://www.pixiv.net* // @version 0.5.7-20181015 // @homepageURL https://github.com/SotobatoNihu/PixivViewUtil // @license MIT License // @require https://code.jquery.com/jquery-3.2.1.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js // @resource gearIcon https://svgsilh.com/svg/24277.svg // @grant GM.getValue // @grant GM.setValue // @grant GM.getResourceUrl // @grant GM_getResourceText // @downloadURL none // ==/UserScript== (() => { /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 9); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * 本プロジェクトで使用する各種列挙型 */ Object.defineProperty(exports, "__esModule", { value: true }); var prop; (function (prop) { prop[prop["changeIllustPageLayout"] = 0] = "changeIllustPageLayout"; prop[prop["changeMemberPageLayout"] = 1] = "changeMemberPageLayout"; prop[prop["popup_typeA"] = 2] = "popup_typeA"; prop[prop["popup_typeB"] = 3] = "popup_typeB"; prop[prop["openComment"] = 4] = "openComment"; })(prop = exports.prop || (exports.prop = {})); var uiComponent; (function (uiComponent) { uiComponent[uiComponent["image"] = 0] = "image"; uiComponent[uiComponent["manga"] = 1] = "manga"; uiComponent[uiComponent["ugoira"] = 2] = "ugoira"; uiComponent[uiComponent["caption"] = 3] = "caption"; })(uiComponent = exports.uiComponent || (exports.uiComponent = {})); var pagetype; (function (pagetype) { // my top page pagetype[pagetype["top"] = 0] = "top"; //Works from favourite artists pagetype[pagetype["bookmark_new_illust"] = 1] = "bookmark_new_illust"; //Discovery page pagetype[pagetype["discovery"] = 2] = "discovery"; //Artist works page pagetype[pagetype["member_illust"] = 3] = "member_illust"; //Artist's "top" page pagetype[pagetype["member"] = 4] = "member"; //Bookmark information pagetype[pagetype["bookmark_detail"] = 5] = "bookmark_detail"; //Added new bookmarks pagetype[pagetype["bookmark_add"] = 6] = "bookmark_add"; //Daily rankings pagetype[pagetype["ranking"] = 7] = "ranking"; //Someone's bookmarks page pagetype[pagetype["bookmark_id"] = 8] = "bookmark_id"; //Search page pagetype[pagetype["search"] = 9] = "search"; //Your bookmarks page pagetype[pagetype["bookmark"] = 10] = "bookmark"; pagetype[pagetype["other"] = 11] = "other"; })(pagetype = exports.pagetype || (exports.pagetype = {})); ; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const enum_1 = __webpack_require__(0); /** * 機能の有効/無効等の設定内容 * */ class Setting { constructor() { this.changeIllustPageLayout = true; this.changeMemberPageLayout = true; this.openComment = true; this.popup = true; this.popupScale = 0.7; } async init() { // @ts-ignore await GM.getValue("pixiv_viewutil_setting").then(jsonString => { if (jsonString !== undefined) { const jsonData = JSON.parse(jsonString); this.changeIllustPageLayout = (jsonData.changeIllustPageLayout == null) ? true : jsonData.changeIllustPageLayout; this.changeMemberPageLayout = (jsonData.changeMemberPageLayout == null) ? true : jsonData.changeMemberPageLayout; this.openComment = (jsonData.openComment == null) ? true : jsonData.openComment; this.popup = (jsonData.popup == null) ? true : jsonData.popup; this.popupScale = (jsonData.popupScale == null) ? 0.7 : jsonData.popupScale; } }); } set setStringData(jsonString) { const jsonData = JSON.parse(jsonString); this.changeIllustPageLayout = (jsonData.changeIllustPageLayout == null) ? true : jsonData.changeIllustPageLayout; this.changeMemberPageLayout = (jsonData.changeMemberPageLayout == null) ? true : jsonData.changeMemberPageLayout; this.openComment = (jsonData.openComment == null) ? true : jsonData.openComment; this.popup = (jsonData.popup == null) ? true : jsonData.popup; this.popupScale = (jsonData.popup == null) ? 0.7 : jsonData.popupScale; this.uiComponent = [enum_1.uiComponent.image, enum_1.uiComponent.manga, enum_1.uiComponent.ugoira, enum_1.uiComponent.caption]; } set setData(jsonData) { this.changeIllustPageLayout = (jsonData.changeIllustPageLayout == null) ? true : jsonData.changeIllustPageLayout; this.changeMemberPageLayout = (jsonData.changeMemberPageLayout == null) ? true : jsonData.changeMemberPageLayout; this.openComment = (jsonData.openComment == null) ? true : jsonData.openComment; this.popup = (jsonData.popup == null) ? true : jsonData.popup; this.popupScale = (jsonData.popup == null) ? 0.7 : jsonData.popupScale; } get getJsonString() { const obj = { changeIllustPageLayout: this.changeIllustPageLayout, changeMemberPageLayout: this.changeMemberPageLayout, openComment: this.openComment, popup: this.popup, popupScale: this.popupScale, }; return JSON.stringify(obj); } save() { // @ts-ignore GM.setValue("pixiv_viewutil_setting", this.getJsonString); } } exports.Setting = Setting; /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; /** * divやspan,imgなどのHTML要素を作るファクトリ */ Object.defineProperty(exports, "__esModule", { value: true }); class ContainerFactory { constructor() { this.id = ''; this.classname = ''; this.innerHtml = ''; this.innerText = ''; this.cssText = ''; } setId(idString) { this.id = idString; return this; } setClass(classname) { this.classname = classname; return this; } setLeft(x) { } setTop(y) { } setWitdh(w) { this.width = w; return this; } setInnerHtml(elem) { this.innerHtml = elem; return this; } setInnerText(elem) { this.innerText = elem; return this; } createDiv() { const elem = document.createElement('div'); elem.id = this.id; elem.className = this.classname; elem.innerHTML = this.innerHtml; elem.style.cssText = this.cssText; this.init(); return elem; } createImg() { const elem = document.createElement('img'); elem.id = this.id; elem.className = this.classname; elem.style.cssText = this.cssText; this.init(); return elem; } createSpan() { const elem = document.createElement('span'); elem.id = this.id; elem.className = this.classname; elem.innerHTML = this.innerHtml; elem.style.cssText = this.cssText; this.init(); return elem; } appendChild(infoElem) { this.appendChild(infoElem); return this; } setCSS(cssString) { this.cssText = cssString; return this; } init() { this.id = ''; this.classname = ''; this.innerHtml = ''; this.innerText = ''; this.cssText = ''; } } exports.ContainerFactory = ContainerFactory; /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const enum_1 = __webpack_require__(0); const popupUtil_1 = __webpack_require__(7); const setting_1 = __webpack_require__(1); const ContainerFactory_1 = __webpack_require__(2); const jsonInterface_1 = __webpack_require__(4); /*** * 各種ユーティリティ関数 * ポップアップ機能に関するユーティリティ関数軍が長くなったため * popupUtilと外出しにしている */ class Util { constructor() { this.innerContainerID = 'popup-inner-container'; this.outerContainerID = 'popup-outer-container'; this.captionContainerID = 'popup-caption-container'; this.popupClass = 'popup-util'; //あとで各要素やドキュメントに挿入するCSS文字列 this.outerContainerCSS = ` position:absolute; display:none; z-index:10000; background-color:#FFF; max-width:${window.innerWidth * 0.8}px; max-height:${window.innerHeight * 0.8}px; `; this.innerContainerCSS = ` border: 5px solid black; background-color:#111; position:relative; width:100%; height:100%; float:left; max-width:${window.innerWidth * 0.8}px; max-height:${window.innerHeight * 0.8}px; `; this.captionContainerCSS = ` white-space:pre-wrap; z-index:10001; position:relative; border: 1px solid black; width:auto; height:20%; max-width:${window.innerWidth * 0.8}px; background-color:white; word-wrap:break-word; word-break:break-all; `; this.gearCSS = `width: 25px; height: 25px; color:rgb(173, 173, 173); `; this.modalCSS = `.pixiv-modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index:10000; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content/Box */ #pixiv-modal-content { background-color: #fefefe; margin: 15% auto; /* 15% from the top and centered */ padding: 20px; z-index:10001; border: 1px solid #888; width: 80%; /* Could be more or less, depending on screen size */ } /* The Close Button */ #pixiv-modal-close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } #pixiv-modal-close :hover, #pixiv-modal-close :focus { color: black; text-decoration: none; cursor: pointer; } `; this.pixpediaItemCSS = `.popup-pixpedia-icon{ display: inline-block; margin-left: 2px; width: 15px; height: 14px; vertical-align: -2px; text-decoration: none; background: url(https://s.pximg.net/www/images/inline/pixpedia.png) no-repeat; } .popup-pixpedia-no-icon{ display: inline-block; margin-left: 2px; width: 15px; height: 14px; vertical-align: -2px; text-decoration: none; background: url(https://s.pximg.net/www/images/inline/pixpedia-no-item.png) no-repeat; } `; this.utilIcon = 'pixiv-view-util-gear'; } /** * とりあえずやっておくこと。 * 今いるページのタイプと、設定内容に応じて、レイアウトの修正やポップアップの用意を行う * トップ画面の場合は設定ダイアログも用意する * @param setting * @param page */ async initExecute(setting, page) { if (page.pagetype == enum_1.pagetype.top && document.getElementById(this.utilIcon) === null) { this.setConfigDialog(); } if (setting.popup && (page.isEnable(enum_1.prop.popup_typeA)) || page.isEnable(enum_1.prop.popup_typeB)) { this.setPopup(page, setting); console.log("set popup"); } } /** * 画面読み込み完了時に行うこと。initExecuteと同様 * @param setting * @param page */ onloadExecute(setting, page) { if (page.pagetype === enum_1.pagetype.top && document.getElementById('pixiv-view-util-gear') === null) { this.setConfigDialog(); } if (setting.changeIllustPageLayout && page.isEnable(enum_1.prop.changeIllustPageLayout)) { Util.changeIllustPageLayout(); console.log("layout chainged"); } if (setting.openComment && page.isEnable(enum_1.prop.openComment)) { this.openComment(page); console.log("comment opend"); } if (setting.changeMemberPageLayout && (page.pagetype === enum_1.pagetype.member || page.pagetype === enum_1.pagetype.member_illust)) { this.changeMemberPageLayout(); //読み込みに時間がかかるようなので時差を付ける for (let i = 0; i < 5; i++) { setTimeout(this.changeMemberPageLayout(), 1000 * i); } } } /** * URLに応じてpagetypeを返す * @param url */ static checkPageType(url) { if (url.match('https://www.pixiv.net/bookmark_new_illust.php?')) return enum_1.pagetype.bookmark_new_illust; if (url.match('https://www.pixiv.net/discovery?')) return enum_1.pagetype.discovery; if (url.match('https://www.pixiv.net/member_illust.php?')) return enum_1.pagetype.member_illust; if (url.match('https://www.pixiv.net/member.php?')) return enum_1.pagetype.member; if (url.match('https://www.pixiv.net/bookmark_detail.php?')) return enum_1.pagetype.bookmark_detail; if (url.match('https://www.pixiv.net/bookmark_add.php?')) return enum_1.pagetype.bookmark_add; if (url.match('https://www.pixiv.net/ranking.php?')) return enum_1.pagetype.ranking; if (url.match(/https:\/\/www\.pixiv\.net\/bookmark\.php\?id/)) return enum_1.pagetype.bookmark_id; if (url.match('https://www.pixiv.net/search.php')) return enum_1.pagetype.search; if (url.match('https://www.pixiv.net/bookmark.php?')) return enum_1.pagetype.bookmark; if (url.match('https://www.pixiv.net/')) return enum_1.pagetype.top; else return enum_1.pagetype.other; } ; /** * イラスト閲覧ページのレイアウトを修正する */ static changeIllustPageLayout() { $('figure').before($('figcaption')); } /** * そのページで可能な、本スクリプトが対象とする操作を返す * HTML要素に埋め込まれたURLの構造にはページに応じて2パターンあり、かつてはポップアップ機能はパターンごとに * 区別し実行していたためその名残でタイプA/Bが残っている * @param type */ static getAllowedFuncList(type) { switch (type) { case enum_1.pagetype.top: return [enum_1.prop.popup_typeB]; case enum_1.pagetype.bookmark_new_illust: return [enum_1.prop.popup_typeA]; case enum_1.pagetype.discovery: return [enum_1.prop.popup_typeA]; case enum_1.pagetype.member_illust: return [enum_1.prop.popup_typeB, enum_1.prop.changeIllustPageLayout, enum_1.prop.openComment]; case enum_1.pagetype.member: return [enum_1.prop.popup_typeB, enum_1.prop.changeMemberPageLayout]; case enum_1.pagetype.bookmark_detail: return [enum_1.prop.popup_typeB]; case enum_1.pagetype.bookmark_add: return [enum_1.prop.popup_typeB]; case enum_1.pagetype.bookmark_id: return [enum_1.prop.popup_typeB]; case enum_1.pagetype.search: return [enum_1.prop.popup_typeA]; case enum_1.pagetype.ranking: return [enum_1.prop.popup_typeB]; case enum_1.pagetype.bookmark: return [enum_1.prop.popup_typeB]; default: return []; } } /** * コメントを開く * @param page */ openComment(page) { if (page.getURL.indexOf('mode=medium') > 0) { let elem = $("article"); elem.find("[aria-expanded='false']").click(); var observer = new MutationObserver(function (MutationRecords, MutationObserver) { elem.find("[aria-expanded='false']").click(); }); observer.observe(document, { childList: true, subtree: true, }); } } /** * ポップアップ機能の用意を行う * @param page * @param setting */ setPopup(page, setting) { const popupUtil = new popupUtil_1.PopupUtil(); //IDやCSSなどをセットしたHTML要素を作成 const factory = new ContainerFactory_1.ContainerFactory(); //ポップアップの外枠となるouterContainer const outerContainer = factory .setId(this.outerContainerID) .setClass(this.popupClass) .setCSS(this.outerContainerCSS) .createDiv(); const innerContainer = factory .setId(this.innerContainerID) .setClass(this.popupClass) .setCSS(this.innerContainerCSS) .createDiv(); const captionContainer = factory .setId(this.captionContainerID) .setClass(this.popupClass) .setCSS(this.captionContainerCSS) .createDiv(); outerContainer.appendChild(captionContainer); outerContainer.appendChild(innerContainer); document.body.appendChild(outerContainer); //ドキュメントにCSSを登録 const style = document.createElement('style'); style.textContent = this.pixpediaItemCSS; document.getElementsByTagName('head')[0].appendChild(style); const classname = this.popupClass; // イラスト&漫画のクリックイベントを登録する $('body').on('mouseenter', 'a[href*="member_illust.php?mode=medium&illust_id="]', function () { //クリック対象とhrefがある要素の入れ子関係は2パターン以上あるため注意 const thumb = $(this).find('.non-trim-thumb'); const clickElem = thumb.length > 0 ? thumb[0] : this; //イラストの本来のクリックによる遷移を抑制 $(this).attr('onclick', 'console.log();return false;'); //漫画の本来のクリックによる遷移を抑制 $(this).find('.non-trim-thumb').attr('onclick', 'console.log();return false;'); const hrefElem = this; const url = this.getAttribute("href"); //イラストIDを取得 const matches = url.match(/(.)+illust_id=([0-9]+)(&.+)?/); const illustID = Number(matches[2]); //イラストIDを元にJSONを入手 fetch(`https://www.pixiv.net/ajax/illust/${illustID}`, { method: 'GET', mode: 'cors', keepalive: true }).then(function (response) { if (response.ok) { return response.json(); } }).then(async function (json) { //pixivJsonオブジェクトに格納 const pixivJson = new jsonInterface_1.PixivJson(json); // const mouseX=e.pageX //const mouseY=e.pageY //漫画のポップアップを実行 if (popupUtil.isManga(pixivJson)) { const pageNum = Util.getPageNum(pixivJson); popupUtil.preloadManga(outerContainer, hrefElem, pixivJson, Number(pageNum)); } else if (popupUtil.isIllust(pixivJson)) { //イラストのポップアップを実行 popupUtil.preloadImg(page, outerContainer, hrefElem, pixivJson); } else { //うごイラのポップアップを実行 //うごイラは通常のイラストのポップアップ手順でも正常動作する popupUtil.preloadImg(page, outerContainer, hrefElem, pixivJson); //うごイラのメタ情報のJSONを入手 await fetch(`https://www.pixiv.net/ajax/illust/${pixivJson.body.illustId}/ugoira_meta`) .then(function (response) { return response.json(); }).then(json => { popupUtil.preloadUgoira(outerContainer, hrefElem, pixivJson, new jsonInterface_1.PixivJson(json)).then(result => console.log(result)); }); } //キャプションのポップアップを実行 popupUtil.preloadCaption(outerContainer, pixivJson); }); //ポップアップを実行 $(clickElem).on('click', () => { const elems = document.getElementsByClassName(classname); for (let elem of elems) { // @ts-ignore elem.style.display = 'block'; } const offset = popupUtil.getOffset(outerContainer); outerContainer.style.left = `${offset.left}px`; outerContainer.style.top = `${offset.top}px`; captionContainer.style.width = `${Number(outerContainer.style.width) + 10}px`; popupUtil.adjustContainer(); //captionContainer.style.top=`${-100}px` }); }); this.addMouseMove(outerContainer); } cleanContainer(outerContainer) { const innerContainer = document.getElementById(this.innerContainerID); const captionContainer = document.getElementById(this.captionContainerID); innerContainer.innerText = ''; captionContainer.innerText = ''; outerContainer.style.display = 'none'; /* const childContainer=outerContainer.childNodes for(let container of childContainer){ container.innerText='' } document.getElementById(this.innerContainerID).innerText='' document.getElementById(this.captionContainerID).innerText='' document.getElementById(this.outerContainerID).style.display = 'none'; */ } static getUserID(json) { return json.body.tags.authorId; } static getPageNum(json) { return Number(json.body.pageCount); } /** * 設定ダイアログを用意する */ async setConfigDialog() { const iconID = this.utilIcon; const iconElem = document.getElementById(this.utilIcon); if (iconElem === null) { await this.setIconElem(iconID); await this.setModal(iconID); } } /** * 設定アイコンをトップページにセットする * @param iconID */ async setIconElem(iconID) { // @ts-ignore const gearIcon = await GM_getResourceText('gearIcon'); const factory = new ContainerFactory_1.ContainerFactory(); const gearElem = factory .setId(iconID) .setCSS(this.gearCSS) .setInnerHtml(gearIcon) .createDiv(); gearElem.className = 'trigger'; const svgTag = gearElem.getElementsByTagName('svg')[0]; const gTag = gearElem.getElementsByTagName('g')[0]; svgTag.setAttribute('width', '25'); svgTag.setAttribute('height', '25'); gTag.setAttribute('fill', '#BECAD8'); const liElem = document.createElement('li'); liElem.className = 'viewUtil'; liElem.appendChild(gearElem); document.body.getElementsByClassName('notifications')[0].appendChild(liElem); } /** * ダイアログ(モーダル)をセットする * @param iconID */ setModal(iconID) { const setting = new setting_1.Setting; const iconElem = document.getElementById(iconID); const modal1 = document.createElement('div'); modal1.innerHTML = `
Setting: