awn-git / Abrowzer

A Google Chrome extension for good loitering time in open2ch.
MIT License
0 stars 0 forks source link

inspectwepage(content_scripts)の見直し #32

Open awn-git opened 7 years ago

awn-git commented 7 years ago

概要

現状、location.href.indexOfでページを判定した結果を文字列でbackgroundへ送信しているのみである。 しかしながら、取得できる情報はもっとある。 情報は多めに出そう。

メリット

bacgroundから個別に実行するinject何ちゃら関数でurlやbbsnameを取得しなくても良い

デメリット

全部書き換える必要がある:sob:

タスク一覧

その他

何かあればここに書く

awn-git commented 7 years ago

commitしてないけど、こんなかんじ。

////////////////////////////////////////////////////////////////////////////////
//                                                                               
//              ,,                                                               
//      db     *MM                                                               
//     ;MM:     MM                                                               
//    ,V^MM.    MM,dMMb.`7Mb,od8 ,pW"Wq.`7M'    ,A    `MF'M"""MMV .gP"Ya `7Mb,od8
//   ,M  `MM    MM    `Mb MM' "'6W'   `Wb VA   ,VAA   ,V  '  AMV ,M'   Yb  MM' "'
//   AbmmmqMA   MM     M8 MM    8M     M8  VA ,V  VA ,V     AMV  8M""""""  MM    
//  A'     VML  MM.   ,M9 MM    YA.   ,A9   VVV    VVV     AMV  ,YM.    ,  MM    
//.AMA.   .AMMA.P^YbmdP'.JMML.   `Ybmd9'     W      W     AMMmmmM `Mbmmd'.JMML.  
////////////////////////////////////////////////////////////////////////////////

(function() {
    //private member
    var _info = {};

    //initializer
    _init();

    //private method
    function _init() {
        _info = _getPageInfo();
        _sendMessageToBackground(_info);
        return;
    }

    function _sendMessageToBackground(message) {
        if (message.bbsname) {
            var url = "http://" + message.server + ".open2ch.net/" + message.bbsname + "/SETTING.TXT";
            _execXHR(url, _getBbsnameJandSendMessage, message);
        } else {
            chrome.runtime.sendMessage(message);
        }
        return;
    }

    function _getPageInfo() {
        var obj = {};
        var basic = {};
        basic = _getBasicInfo();
        obj = _detectPageType(basic);
        return obj;
    }

    function _getBasicInfo() {
        var obj = {
            hostname: location.hostname,
            href: location.href,
            origin: location.origin,
            pathname: location.pathname,
            title: document.title,
            body: document.body
        };
        return obj;
    }

    function _detectPageType(ba) {
        var obj = {};
        obj.url = ba.href;
        obj.title = ba.title;
        obj.pagetype = null;
        obj.bbsname = null;
        obj.bbsnameJ = null;

        switch (true) {
            //note: 以下はbbsnameが取得できないページ
            case ba.href.indexOf("/dev/") > -1:
                /*
                //note: "/dev/"は以下が該当する
                    http://open2ch.net/dev/ranking.cgi
                    http://open2ch.net/dev/antena.cgi
                    http://open2ch.net/dev/toukei.cgi
                    http://open2ch.net/dev/banner.cgi
                    http://open2ch.net/dev/kome.cgi
                */
                obj.pagetype = ba.href.match(/.*open2ch\.net\/dev\/(.*)\.cgi/)[1];
                break;
            case ba.href.indexOf("headline.cgi") > -1:
                obj.pagetype = "headline";
                break;
            case ba.href.indexOf("/plus/") > -1:
                obj.pagetype = "plus";
                break;
            case ba.href.indexOf("senbura.html") > -1:
                obj.pagetype = "senbura";
                break;
            case ba.href.indexOf("p2.open2ch.net") > -1:
                obj.pagetype = "p2";
                break;
            case ba.href.indexOf("find.open2ch.net") > -1:
                obj.pagetype = "find";
                break;
            case ba.href.indexOf("bbsmenu.html") > -1:
                obj.pagetype = "bbsmenu";
                break;
            case ba.href.indexOf("wiki.open2ch.net") > -1:
                obj.pagetype = "wiki";
                break;
            case ba.href.indexOf("mente.open2ch.net") > -1:
                obj.pagetype = "mente";
                break;
            case ba.href.indexOf("history.cgi") > -1:
                obj.pagetype = "history";
                break;
            case ba.href === "http://open2ch.net/menu/":
                obj.pagetype = "open2ch_menu";
                break;
            case ba.href === "http://open2ch.net/":
                obj.pagetype = "open2ch_entrance";
                break;
            case ba.href.indexOf("bbs.cgi") > -1:
                obj.pagetype = "bbs.cgi";
                obj.result = _getBBSCGIResult(ba.body, ba.title);
                break;

                //note: 以下はbbsnameが取得できるページ
            case ba.href.indexOf("subback.html") > -1:
                obj.pagetype = "subback";
                obj.server = ba.hostname.split(".")[0];
                obj.bbsname = ba.pathname.match(/^\/(.*?)\//)[1];
                break;
            case ba.href.indexOf("gomi.html") > -1:
                obj.pagetype = "gomi";
                obj.server = ba.hostname.split(".")[0];
                obj.bbsname = ba.pathname.match(/^\/(.*?)\//)[1];
                break;
            case ba.href.indexOf("kako") > -1:
                obj.pagetype = "kako";
                obj.server = ba.hostname.split(".")[0];
                obj.bbsname = ba.pathname.match(/^\/(.*?)\//)[1];
                break;
            case ba.href.indexOf("subject.txt") > -1:
                obj.pagetype = "subject";
                obj.server = ba.hostname.split(".")[0];
                obj.bbsname = ba.pathname.match(/^\/(.*?)\//)[1];
                break;
            case ba.href.indexOf("SETTING.TXT") > -1:
                obj.pagetype = "SETTING";
                obj.server = ba.hostname.split(".")[0];
                obj.bbsname = ba.pathname.match(/^\/(.*?)\//)[1];
                break;

                //note: readcgi, imagecgi, datは個別にobjを設定する
            case ba.href.indexOf("read.cgi") > -1:
                obj = _analyzeREADCGI(ba);
                break;
            case ba.href.indexOf("image.cgi") > -1:
                obj = _analyzeIMAGECGI(ba);
                break;
            case ba.href.indexOf("/dat/") > -1:
                obj = _analyzeDAT(ba);
                break;

                //note: defaultケースは板TOPになる"はず"である
            default:
                obj = _analyzeITATOP(ba);
                break;
        }

        return obj;
    }

    function _analyzeITATOP(ba) {
        var obj = {};
        obj.pagetype = "itatop";
        obj.url = ba.href;
        obj.title = ba.title;
        obj.bbsnameJ = null;
        var matches;

        var regexp_itatop = new RegExp(/^http:\/\/(.*?)\.open2ch\.net\/(.*?)\//);
        matches = ba.href.match(regexp_itatop);
        obj.server = matches[1];
        obj.bbsname = matches[2];

        return obj;
    }

    function _analyzeREADCGI(ba) {
        var obj = {};
        var matches = [];
        obj.pagetype = "read.cgi";
        obj.url = ba.href;
        obj.title = ba.title;
        obj.bbsnameJ = null;

        var regexp_readcgi = new RegExp(/^http:\/\/(.*?)\.open2ch\.net\/test\/read\.cgi\/(.*?)\/([0-9]{10})\//);
        matches = ba.href.match(regexp_readcgi);
        obj.server = matches[1];
        obj.bbsname = matches[2];
        obj.key = matches[3] - 0;

        return obj;
    }

    function _analyzeIMAGECGI(ba) {
        var obj = {};
        var matches = [];
        obj.pagetype = "image.cgi";
        obj.url = ba.href;
        obj.title = ba.title;
        obj.bbsnameJ = null;
        obj.hasImage = "yes";

        var regexp_imagecgi = new RegExp(/^http:\/\/(.*?)\.open2ch\.net\/test\/image\.cgi\/(.*?)\/([0-9]{10})\//);
        matches = ba.href.match(regexp_imagecgi);
        obj.server = matches[1];
        obj.bbsname = matches[2];
        obj.key = matches[3] - 0;
        if (ba.body.innerText.match("このスレには画像は貼られていないようだ。。")) {
            obj.hasImage = "no";
        }

        return obj;

    }

    function _analyzeDAT(ba) {
        var obj = {};
        var matches = [];
        obj.pagetype = "dat";
        obj.url = ba.href;
        obj.bbsnameJ = null;

        var regexp_dat = new RegExp(/^http:\/\/(.*?)\.open2ch\.net\/(.*?)\/dat\/([0-9]{10})\.dat/);
        matches = ba.href.match(regexp_dat);
        obj.server = matches[1];
        obj.bbsname = matches[2];
        obj.key = matches[3] - 0;

        var arr = ba.body.getElementsByTagName("pre")[0].innerText.split("\n");
        obj.title = arr[0].match(/^(.*?)<>(.*?)<>(.*?) ID:(.*?)<> (.*?) <>(.*?)$/)[6];

        return obj;

    }

    function _getBBSCGIResult(body, title) {
        var str = "ERROR";
        if (title.match("書きこみました。") || body.innerText.match("書きこみが終わりました。")) {
            str = "SUCCESS";
        }
        return str;
    }

    function _execXHR(url, callback, message) {
        var xhr = new XMLHttpRequest();
        var text;
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                text = xhr.responseText;
                callback(text, message);
            }
        };
        xhr.open('GET', url, true);
        xhr.send();
        return;
    }

    function _getBbsnameJandSendMessage(parm, message) {
        var regexp = /BBS_TITLE=(.*)/;
        var bbsnameJ = parm.match(regexp)[1];
        message.bbsnameJ = bbsnameJ;
        chrome.runtime.sendMessage(message);
        return;
    }

    //public api
    return;
})();