// ==UserScript== // @name PixivPreviewer // @namespace // @version 1.20 // @description 显示大图预览,按热门度排序(pixiv_sk),批量下载。View Preview, Sort by favorite numbers, Bulk download.(仅搜索排行页生效, Only available in search and rank page) // @author Ocrosoft // @match https://www.pixiv.net/search.php* // @match https://www.pixiv.net/member_illust.php?mode=medium* // @match https://www.pixiv.net/member_illust.php?mode=ugoira_view* // @match https://www.pixiv.net/ranking.php* // @grant none // @require http://code.jquery.com/jquery-2.1.4.min.js // @namespace // @downloadURL none // ==/UserScript== function log(text) { console.log(text); } /** * ---------------------------------------- 以下为 全局 部分 ---------------------------------------- */ /** * 节点结构 * -div, [picList] * --div * ---figure * ----div, [picDiv] * -----a, [picHref] * ------img(图片节点), [picNode] * -----div(菜单) */ var dataDiv, picList, picDiv = [], picHref = [], picNode = []; // 相关元素,含义见上 var dataStr; // 更新后图片信息使用 json 保存在了 dataDiv 的 data-items 属性中 var imgData; // 保存解析后的 json var mousePos; // 鼠标位置 // 获取相关的元素 function getImageElements() { $('.popular-introduction').remove(); dataDiv = $('#js-mount-point-search-result-list'); dataStr = dataDiv.attr('data-items'); imgData = eval(dataStr); picList = dataDiv.children()[0]; var pics = $(picList).children(); picDiv = [], picHref = [], picNode = []; for (var i = 0; i < pics.length; i++) { picDiv.push(pics[i].childNodes[0].childNodes[0]); $(picDiv[i]).attr('data-index', i); picHref.push(picDiv[i].childNodes[0]); $(picHref[i]).attr('data-index', i); picNode.push($(picHref[i]).children('img')[0]); $(picNode[i]).attr('data-index', i); $(picNode[i]).attr('data-id', imgData[i].illustId); } } // 动图预览在相关页面调用的函数(自动执行,非动图页面无操作)[属于预览功能] (function animePreview() { // 普通查看转换为全屏查看 if (location.href.indexOf('medium') != -1 && location.href.indexOf('animePreview') != -1) { location.href = location.href.replace('medium', 'ugoira_view'); return; } // 全图预览调节并返回 canvas 大小 if (location.href.indexOf('ugoira_view') != -1 && location.href.indexOf('animePreview') != -1) { var height = parseInt($('canvas').css('height').split('px')); var width = parseInt($('canvas').css('width').split('px')); var newHeight = 580 / width * height; $('canvas').css({ 'height': newHeight + 'px', 'width': 580 + 'px' }); window.parent.iframeLoaded(newHeight, 580); return; } })(); // iframe 加载完成时调用(动图预览)[属于预览功能] // arg: canvas 元素高,canvas 元素宽 function iframeLoaded(height, width) { $('.pixivPreview').children('iframe').css({ 'width': width + 20 + 'px', 'height': height + 20 + 'px' }); // 调整位置 var divX = mousePos.x, divY = mousePos.y; var screenWidth = document.documentElement.clientWidth; var screenHeight = document.documentElement.clientHeight; if (mousePos.x > screenWidth / 2) { divX -= width; } if ((mousePos.y - document.body.scrollTop) > screenHeight / 2) { divY -= height; } $('.pixivPreview').css({ 'left': divX + 'px', 'top': divY + 'px' }); $('.pixivPreview').children('iframe').css('display', ''); $('.pixivPreview').children('img').remove(); } /** * ---------------------------------------- 以下为 预览功能 部分 ---------------------------------------- */ function pixivPreviewer() { // 开启预览功能 function activePreview() { // 鼠标移动到图片上显示预览图 $(picHref).mouseover(function (e) { // 按住 Ctrl键 不显示预览图 if (e.ctrlKey) { return; } // 从预览图移动到图片上,不应再次显示 try { if ($(e.relatedTarget.parentNode).hasClass('pixivPreview')) { return; } } catch (e) { } // 图片索引 var dataIndex = $(this).attr('data-index'); // 图片节点 var imgNode = picNode[dataIndex]; // 鼠标位置 mousePos = { x: e.pageX, y: e.pageY }; // 预览 Div var previewDiv = document.createElement('div'); $(previewDiv).css({ 'position': 'absolute', 'z-index': '999999' }); $(previewDiv).addClass('pixivPreview'); $(previewDiv).attr('data-index', dataIndex); // 添加 Div 到 body $('.pixivPreview').remove(); $('body')[0].appendChild(previewDiv); // 加载中图片节点 var loadingImg = new Image(); loadingImg.src = 'https://raw.githubusercontent.com/shikato/pixiv_sk/master/loading.gif'; $(loadingImg).css('position', 'absolute'); $(loadingImg).attr('data-index', dataIndex); previewDiv.appendChild(loadingImg); // 要显示的预览图节点 var loadImg = new Image(); $(loadImg).attr('data-index', dataIndex); previewDiv.appendChild(loadImg); // 表示显示的是原图的图标 var originIcon = new Image(); originIcon.src = 'https://source.pixiv.net/www/images/pixivcomic-favorite.png'; $(originIcon).css({ 'position': 'absolute', 'bottom': '0px', 'right': '0px', 'display': 'none' }); $(originIcon).attr('data-index', dataIndex); previewDiv.appendChild(originIcon); // 点击图标新网页打开原图 $(originIcon).click(function () { window.open($(previewDiv).children('img')[1].src); }); $(previewDiv).css({ 'left': mousePos.x + 'px', 'top': mousePos.y + 'px' }); // 显示预览图 // args: 图片地址数组,下标,原图地址数组 function viewImages(imgs, index, imgsOrigin) { if (!imgs || imgs.length == 0) return; if (index < 0) return; if (!imgsOrigin || imgsOrigin.length == 0 || imgs.length != imgsOrigin.length) return; if (!index) index = 0; // 绑定点击事件,Ctrl+左键 单击切换原图 if ($(previewDiv).children('script').length == 0) { loadImg.addEventListener('click', function (ev) { // 按住 Ctrl 来回切换原图 if (ev.ctrlKey) { if (loadImg.src.indexOf('origin') == -1) { viewImages(allImgsOrigin, parseInt($($('.pixivPreview').children('img')[1]).attr('img-index')), allImgs); } else { viewImages(allImgs, parseInt($($('.pixivPreview').children('img')[1]).attr('img-index')), allImgsOrigin); } } // 按住 Shift 点击图片新标签页打开原图 else if (ev.shiftKey) { window.open(allImgsOrigin[parseInt($($('.pixivPreview').children('img')[1]).attr('img-index'))]); } }); } // 多图时绑定点击事件,点击图片切换到下一张 if (index == 0 && imgs.length != 1 && $(previewDiv).children('._work').length == 0) { loadImg.addEventListener('click', function (e) { if (e.ctrlKey || e.shiftKey) return; var newIndex = parseInt($($('.pixivPreview').children('img')[1]).attr('img-index')) + 1; if (newIndex == allImgs.length) newIndex = 0; $('.pixivPreview').children('div').children('div').children('span')[0].innerHTML = (newIndex + 1) + '/' + allImgs.length; if (loadImg.src.indexOf('origin') == -1) { viewImages(allImgs, newIndex, allImgsOrigin); } else { viewImages(allImgsOrigin, newIndex, allImgs); } }); } // 右上角张数标记 if (imgs.length != 1 && index == 0 && $(previewDiv).children('._work').length == 0) { var iconDiv = document.createElement('div'); iconDiv.innerHTML = '
处理中
1/1