// ==UserScript== // @name PixivPreviewer // @namespace // @version 1.15 // @description 在搜索页显示较大的预览图(请注意阅读详细信息)。Show preview of pictures in serach 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 // @namespace // @downloadURL none // ==/UserScript== var mousePos; // 鼠标位置 /* * 节点结构 * -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 // 获取相关的元素 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 log(text) { console.log(text); } function activePreview() { $(picHref).mouseover(function (e) { 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 $('.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' }); 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