pixiv_sk

pixivの検索結果をソートしたりフィルタリングしたり1ページに表示する数を増やしたりできます。

目前為 2015-05-10 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name           pixiv_sk
// @namespace      http://alexam.hateblo.jp/
// @description    pixivの検索結果をソートしたりフィルタリングしたり1ページに表示する数を増やしたりできます。
// @version        1.0.0
// @include        http://www.pixiv.net/search.php*
// @include        http://www.pixiv.net/tags.php*
// ==/UserScript==

(function (doc, func) {

  var head = doc.getElementsByTagName('head')[0]; 

  var jquery = doc.createElement('script'); 
  jquery.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js');
  jquery.addEventListener('load', function() {
    var myScript = doc.createElement('script');
    myScript.textContent = 'jQuery.noConflict();(' + func.toString() + ')(jQuery);';
    head.appendChild(myScript);
  }, false); 

  head.appendChild(jquery);
})(document, function ($) {

  /** 編集可能 **/
  
  // 1ページに作品を通常の何倍の数表示するか
  // ex) 1なら通常通り
  //     2にすると2ページ分になる(表示にかかる時間は+1秒)
  var PAGE_MULTIPLE = 3;
  // 作品のブックマーク数が以下の値未満の場合は表示しない
  var FAV_FILTER = 3;
  // リンクを別のタブで開くかどうか true or false
  var IS_LINK_BLANK = true; 

  /** 編集可能 **/ 

  if (PAGE_MULTIPLE < 1 || FAV_FILTER < 0) {
    return;
  } 

  var mCurrentGotPage = null;
  var mCurrentUrl = null;
  var mCurrentPage = null;
  var mGetPageLimit = null; 

  var getNextPage = function () { 
    var url = mCurrentUrl;

    if (mCurrentPage === 1) {
      mCurrentPage++;    
      url += ('&p='+mCurrentPage); 
    } else {
      mCurrentPage++;    
      url = mCurrentUrl.replace(/p=\d+/, 'p='+mCurrentPage); 
    }

    if (mCurrentPage > mGetPageLimit) {
      return;
    }

    mCurrentUrl = url; 

    var req = new XMLHttpRequest();
    req.open('GET', mCurrentUrl, true);
    req.onload = function (event) { 
      $(req.responseText).find('.column-search-result').children('._image-items').children('.image-item').each(function() {
        $('.column-search-result').children('ul').append($(this));
      });
      mCurrentGotPage++;
      // PAGE_MULTIPLEで指定した分だけ取得したらソートして表示
      if (mCurrentGotPage === (PAGE_MULTIPLE-1)) {
        $('#loading').remove();
        var sortedImages = filterAndSort();
        $('.column-search-result').children('ul').empty().append(sortedImages).show(); 
      }
      req = null;
    };
    req.onerror = function (event) {
      alert('画像の取得に失敗しました。');
      req = null;
    };

    req.send(null);

    // 1秒後にリクエスト
    setTimeout(function () {
      getNextPage(mCurrentPage);
    }, 1000);
  };
     
  var filterAndSort = function () {
    // FAV_FILTER未満の作品をremove
    $('.column-search-result').children('._image-items').children('.image-item').each(function() {
      var fav = $(this).children('ul').children('li:first').children('a').text();
      if (fav < FAV_FILTER) {
        $(this).remove();
      } else {
        // blank onの場合 target属性追加
        if (!IS_LINK_BLANK) {
          return;
        }
        $(this).children('a').attr('target', 'blank');
      }
    });
    var images = $('.column-search-result').children('._image-items').children('.image-item').map(function() {
      return $(this);
    });

    // ソート
    images.sort(function (a, b) {
      var favA = $(a).children('ul').children('li:first').children('a').text();
      var favB = $(b).children('ul').children('li:first').children('a').text();
      if (favA === '') {
        favA = 0; 
      } else {
        favA = parseInt(favA); 
      }
      if (favB === '') {
        favB = 0; 
      } else {
        favB = parseInt(favB); 
      }
      if (favA > favB) {
        return -1;
      }
      if (favA < favB) {
        return 1;
      }
      return 0;
    }); 

    var results = '';
    for (var i = 0; i < images.length; i++) { 
      results += $('<div>').append(images[i]).html();                    
    }
    return results;
  } 


  mCurrentGotPage = 0;
  mCurrentUrl = location.href;
  mCurrentPage = mCurrentUrl.match(/p=(\d+)/);

  if (mCurrentPage !== null) {
    mCurrentPage = parseInt(mCurrentPage[1]);
    mGetPageLimit = (PAGE_MULTIPLE - 1) + mCurrentPage;
  } else {
    mCurrentPage = 1;
    mGetPageLimit = PAGE_MULTIPLE;
  }

  if (PAGE_MULTIPLE > 1) {
    // load時のスピナー表示
    $('.column-search-result').children('ul').hide();
    $('.column-search-result').prepend(
      '<div id="loading" style="width:50px;margin-left:auto;margin-right:auto;">'+
      '<img src="http://semifo.pa.land.to/loading.gif" /></div>'
    );

    // pixiv_sk用のページネーションリンク表示
    if (mCurrentPage === 1) {
      $('.pager-container').empty().append(
        '<a href="'+mCurrentUrl+'" style="margin-right:15px;">&lt;&lt;</a>'+
        '<a href="'+mCurrentUrl+'&p='+(mCurrentPage+PAGE_MULTIPLE)+'">&gt;</a>'
      ); 
    } else {
      $('.pager-container').empty().append(
        '<a href="'+mCurrentUrl.replace(/&p=\d+/, '')+'" style="margin-right:15px;">&lt;&lt;</a>'+
        '<a href="'+mCurrentUrl.replace(/p=\d+/, 'p='+(mCurrentPage-PAGE_MULTIPLE))+'" style="margin-right:10px;">&lt;</a>'+
        '<a href="'+mCurrentUrl.replace(/p=\d+/, 'p='+(mCurrentPage+PAGE_MULTIPLE))+'" style="margin-right:10px;">&gt;</a>'
      ); 
    }
    // PAGE_MULTIPLEで指定したページ分取得する
    getNextPage();
  } else {
    var sortedImages = filterAndSort();
    $('.column-search-result').children('ul').empty().append(sortedImages);
  }

  // 被さって表示されてしまう要素をremove
  $('.popular-introduction').remove();
});