Advertisement
Awn_pastebin

old_Op2ools.js

Oct 20th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. //@title おーぷん2ちゃんねるツール
  4. //@include http://*.open2ch.net
  5. //@private
  6. //
  7. //作った人: Awn(@Awn_tw)
  8. //
  9. //改定履歴
  10. //-20161021(ver 1.0.0): とりあえず作ってみた。
  11. //-20161024(ver 1.1.0): isSurenusiを追加。
  12. //-20161025(ver 1.2.0): _getNameを追加。モジュールパターンっぽくしてみた。isSurenusi -> _isSurenusiに変更。
  13. //-20161025(ver 1.3.0): _getName -> _getResNameに変更。_getResRawと_getResTimeと_getResIDを追加。
  14. //-20161025(ver 1.4.0): _isBrokenと_getValidNumを追加。
  15. //-20161025(ver 1.4.1): モジュールパターンではあるが、より正確に言えばリビーリングモジュールパターンっぽい。
  16. //-20161025(ver 1.4.2): URLの末尾に半角ハイフンを含む場合も"[全部]のURLに移動します。"のケースに含める。
  17. //-20161027(ver 1.4.3): (面倒なので)canonicalなURLで無い時はcanonicalなURLに飛ばす。kari_compの取得方法を変更(splitを減らし正規表現で取得)
  18. //
  19. //諸注意
  20. //-ご利用は自己責任でお願いします。
  21. //-スクリプトは予告なく修正または廃止されることがあります。
  22. //-コンソールでしか動かない、と思います。
  23. //-悪用は厳禁です。
  24. //-改造改良改悪はご自由にどうぞ。
  25. //
  26. //////////////////////////////////////////////////////////////////////////
  27.  
  28. var Op2ools = (function(){
  29.  
  30. /* private member */
  31.     var _VERSION = "1.4.3";
  32.     var _BBSNAME = "";
  33.     var _SURETAI = "";
  34.     var _isInitSuccess = false;
  35.  
  36.  
  37. /* private initializer */
  38.     _init();
  39.  
  40.  
  41. /* private method */
  42.     function _init(){
  43.         var _hostname = location.hostname;
  44.         var _url = location.href;
  45.  
  46.         //init failure
  47.         if( _hostname.indexOf("open2ch") === -1 ){
  48.             _failureMessage("このツールはおーぷん2ちゃんねる(http://*.open2ch.net)での動作しか保証しておりません。");
  49.             return _isInitSuccess;
  50.         }
  51.  
  52.         if( _url.indexOf("read.cgi") === -1 ){
  53.             _failureMessage("特定のスレを開いてください。");
  54.             return _isInitSuccess;
  55.         }
  56.  
  57.         if( _url !== $("link[rel='canonical']").attr("href") ){
  58.             _failureMessage("[全部]のURLに移動します。\n再度、Op2oolsを実行してください。");
  59.             location.href = $("link[rel='canonical']").attr("href");
  60.             return _isInitSuccess;
  61.         }
  62.  
  63.         //init success
  64.         _BBSNAME = _url.split("/")[5];
  65.         _SURETAI = document.title;
  66.         _isInitSuccess = true;
  67.  
  68.         console.log("Op2ools: init success!");
  69.         if( _isBroken() ){
  70.             console.warn("Op2ools: but this thread is broken..."); }
  71.         else{
  72.             console.log("Op2ools: and this thread may be not broken!");
  73.         };
  74.         return _isInitSuccess;
  75.     }
  76.  
  77.     function _failureMessage(mes){
  78.         console.warn(mes);
  79.         alert(mes);
  80.         console.log("Op2ools: init failure!");
  81.     }
  82.  
  83.     function _getLastResNum(){
  84.         //return ( $("dl.hide:last > dd > ares").attr("num") || $("dl:last > dd > ares").attr("num") ) - 0;
  85.         return ( $("dl:last > dd > ares").attr("num") ) - 0;
  86.     }
  87.  
  88.     function _getRes(num){
  89.         num = _getValidNum(num);
  90.         var rtn = $("dl:eq(" + (num-1) + ") > dd").text().replace(/\n/g,"").replace(/ [0-9]*/g,"").replace(/>>[0-9]*/g,"");
  91.         if( rtn !== "" ){ return rtn; }else{ return undefined; }
  92.     }
  93.  
  94.     function _getResRaw(num){
  95.         num = _getValidNum(num);
  96.         var rtn = $("dl:eq(" + (num-1) + ") > dd").text().replace(/ [0-9]*/g,"");
  97.         if( rtn !== ""){ return rtn; }else{ return undefined; }
  98.     }
  99.  
  100.     function _getResName(num){
  101.         num = _getValidNum(num);
  102.         if( num === undefined ){
  103.             return undefined;
  104.         }
  105.         var rtn = ($("dl:eq(" + (num-1) + ")> dt").text().split(":"))[1];
  106.         if( rtn !== "" && rtn !== undefined ){ return rtn.substr(rtn.length - 1) === " " ? rtn.substr(0,rtn.length - 1) : rtn ; }else{ return undefined; }
  107.     }
  108.  
  109.     function _getResTime(num){
  110.         num = _getValidNum(num);
  111.         if( num === undefined ){
  112.             return undefined;
  113.         }
  114.         var rtn = $("dl:eq(" + (num-1) + ")> dt");
  115.         rtn.length > 0 ? rtn = rtn.text().split(":")[2].split(" ")[0] : undefined
  116.         if( rtn !== "" && rtn !== undefined){return rtn; }else{ return undefined; }
  117.     }
  118.  
  119.     function _getResID(num){
  120.         num = _getValidNum(num);
  121.         if( num === undefined ){
  122.             return undefined;
  123.         }
  124.         var rtn = $("dl:eq(" + (num-1) + ")> dt");
  125.         rtn.length > 0 ? rtn = rtn.text().split(":")[2].split(" ")[1].split(":")[1] : undefined
  126.         if( rtn !== "" && rtn !== undefined){return rtn; }else{ return undefined; }
  127.     }
  128.  
  129.     function _isSurenusi(num){
  130.         num = _getValidNum(num);
  131.         if( $("dl:eq(" + (num-1) + ") > dt > font > font").attr("color") === "red" || $("dl:eq(" + (num-1) + ") > dt > span > font > font").attr("color") === "red" ){ return true }else{ return false };
  132.     }
  133.  
  134.     function _isBroken(){
  135.         var lastresnum = _getLastResNum();
  136.         if( lastresnum === $("dl").length ){ return false; }else{ return true;}
  137.     }
  138.  
  139.     function _getValidNum(kari){
  140.         if( typeof(kari) !== "number" ){
  141.             return undefined;
  142.         }
  143.         if( kari > 1006 || kari < 0){
  144.             return undefined;
  145.         }
  146.         var kari_comp = 1;
  147.         var kariBreakFlag = true;
  148.         var inc = 1;
  149.         var inc_computed = 0;
  150.  
  151.         //try{ kari_comp = ($("dl:eq(" + (kari - 1) + ")> dt").text().split(":"))[0].split("\n")[1].split(" ")[0] - 0 }catch(e){
  152.         try{ kari_comp = $("dl:eq(" + (kari - 1) + ")> dt").text().split(":")[0].match(/[0-9]+/)[0] - 0 }catch(e){
  153.             kari_comp = 1;
  154.         }
  155.         if( kari ===  kari_comp){
  156.             return kari;
  157.         }else{
  158.             while(kariBreakFlag){
  159.                 if( inc % 2 === 0 ){ inc_computed = (-1) * (inc/2); }else{ inc_computed = (inc+1)/2; };
  160.  
  161.                 //try{ kari_comp = ($("dl:eq(" + (kari - 1 + inc_computed ) + ")> dt").text().split(":"))[0].split("\n")[1].split(" ")[0] - 0}catch(e){
  162.                 try{ kari_comp = $("dl:eq(" + (kari - 1 + inc_computed ) + ")> dt").text().split(":")[0].match(/[0-9]+/)[0] - 0 }catch(e){
  163.                     kari_comp = 1005;
  164.                 }
  165.  
  166.                 if( kari === kari_comp ){
  167.                     kari = kari + inc_computed;
  168.                     kariBreakFlag = false;
  169.                 }
  170.  
  171.                 if( inc > 501 ){ kariBreakFlag = false; kari = undefined; return kari; };
  172.                 inc++;
  173.             }
  174.         return kari;
  175.         }
  176.     }
  177.  
  178.  
  179. /* public API */
  180.     if(_isInitSuccess){
  181.         return{
  182.             VERSION : _VERSION,
  183.             BBSNAME : _BBSNAME,
  184.             SURETAI : _SURETAI,
  185.  
  186.             getLastResNum : _getLastResNum,
  187.             getRes : _getRes,
  188.             getResRaw : _getResRaw,
  189.             getResName : _getResName,
  190.             getResTime : _getResTime,
  191.             getResID : _getResID,
  192.  
  193.             isSurenusi : _isSurenusi,
  194.             isBroken : _isBroken,
  195.  
  196.             getValidNum : _getValidNum
  197.         }
  198.     }else{
  199.         return undefined;
  200.     }
  201. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement