法令データ年号変換

    @@ -4,9 +4,27 @@ * @include http://law.e-gov.go.jp/* */ -var a=$X('//div[contains(text(),"最終改正:")]')[0]; -var b=$X('translate(./text(),"〇一二三四五六七八九","0123456789")', a).replace('平成','H').replace('昭和','S').replace('年','.').replace('月','.').replace('日',' ').replace('令',''); -a.innerHTML=b; +var a, b, c, d, n, s; +a = $X('//div[contains(text(),"最終改正:")]')[0]; +b = $X('translate(./text(),"〇一二三四五六七八九","0123456789")', a); +c = b.replace('最終改正:', '').replace(/(\D+)/, '$1.').replace('年', '.').replace('月', '.').replace('日', '.'); +d = c.split('.'); +n = d[0], year = parseInt(d[1]), month = parseInt(d[2]), day = parseInt(d[3]), low = d[4]; + +//via http://www.openreference.jp/?aid=190 +if ((n == "平成") && (year > 0)) { + s = (year + 1988); +} else if ((n == "昭和") && (year > 0) && (year <= 64)) { + s = (year + 1925); +} else if ((n == "大正") && (year > 0) && (year <= 15)) { + s = (year + 1911); +} else if ((n == "明治") && (year > 0) && (year <= 45)) { + s = (y + 1867); +} else {} + +a.innerHTML = '最終改正: ' + s + '.' + month + '.' + day + ' ' + low; + + // cho45 / dollarX.js https://gist.github.com/cho45/3238 // extend version of $X @@ -14,41 +32,50 @@ // $X(exp, context); // $X(exp, type); // $X(exp, context, type); -function $X (exp, context, type /* want type */) { - if (typeof context == "function") { - type = context; - context = null; - } - if (!context) context = document; - exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { - var o = document.createNSResolver(context)(prefix); - if (o) return o; - return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : ""; - }); - - switch (type) { - case String: return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue; - case Number: return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue; - case Boolean: return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue; - case Array: - var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); - for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) { - ret.push(result.snapshotItem(i)); - } - return ret; - case undefined: - var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); - switch (result.resultType) { - case XPathResult.STRING_TYPE : return result.stringValue; - case XPathResult.NUMBER_TYPE : return result.numberValue; - case XPathResult.BOOLEAN_TYPE: return result.booleanValue; - case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: - // not ensure the order. - var ret = [], i = null; - while ((i = result.iterateNext())) ret.push(i); - return ret; - } - return null; - default: throw(TypeError("$X: specified type is not valid type.")); - } + +function $X(exp, context, type /* want type */ ) { + if (typeof context == "function") { + type = context; + context = null; + } + if (!context) context = document; + exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { + var o = document.createNSResolver(context)(prefix); + if (o) return o; + return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : ""; + }); + + switch (type) { + case String: + return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue; + case Number: + return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue; + case Boolean: + return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue; + case Array: + var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) { + ret.push(result.snapshotItem(i)); + } + return ret; + case undefined: + var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); + switch (result.resultType) { + case XPathResult.STRING_TYPE: + return result.stringValue; + case XPathResult.NUMBER_TYPE: + return result.numberValue; + case XPathResult.BOOLEAN_TYPE: + return result.booleanValue; + case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: + // not ensure the order. + var ret = [], + i = null; + while ((i = result.iterateNext())) ret.push(i); + return ret; + } + return null; + default: + throw (TypeError("$X: specified type is not valid type.")); + } }
  • /*
     * @title 法令データ年号変換
     * @description 全角年号→半角英数年号
     * @include http://law.e-gov.go.jp/*
     */
    
    var a, b, c, d, n, s;
    a = $X('//div[contains(text(),"最終改正:")]')[0];
    b = $X('translate(./text(),"〇一二三四五六七八九","0123456789")', a);
    c = b.replace('最終改正:', '').replace(/(\D+)/, '$1.').replace('年', '.').replace('月', '.').replace('日', '.');
    d = c.split('.');
    n = d[0], year = parseInt(d[1]), month = parseInt(d[2]), day = parseInt(d[3]), low = d[4];
    
    //via http://www.openreference.jp/?aid=190
    if ((n == "平成") && (year > 0)) {
        s = (year + 1988);
    } else if ((n == "昭和") && (year > 0) && (year <= 64)) {
        s = (year + 1925);
    } else if ((n == "大正") && (year > 0) && (year <= 15)) {
        s = (year + 1911);
    } else if ((n == "明治") && (year > 0) && (year <= 45)) {
        s = (y + 1867);
    } else {}
    
    a.innerHTML = '最終改正: ' + s + '.' + month + '.' + day + ' ' + low;
    
    
    
    // cho45 / dollarX.js https://gist.github.com/cho45/3238
    // extend version of $X
    // $X(exp);
    // $X(exp, context);
    // $X(exp, type);
    // $X(exp, context, type);
    
    function $X(exp, context, type /* want type */ ) {
        if (typeof context == "function") {
            type = context;
            context = null;
        }
        if (!context) context = document;
        exp = (context.ownerDocument || context).createExpression(exp, function (prefix) {
            var o = document.createNSResolver(context)(prefix);
            if (o) return o;
            return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : "";
        });
    
        switch (type) {
        case String:
            return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue;
        case Number:
            return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue;
        case Boolean:
            return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue;
        case Array:
            var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
            for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) {
                ret.push(result.snapshotItem(i));
            }
            return ret;
        case undefined:
            var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
            switch (result.resultType) {
            case XPathResult.STRING_TYPE:
                return result.stringValue;
            case XPathResult.NUMBER_TYPE:
                return result.numberValue;
            case XPathResult.BOOLEAN_TYPE:
                return result.booleanValue;
            case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
                // not ensure the order.
                var ret = [],
                    i = null;
                while ((i = result.iterateNext())) ret.push(i);
                return ret;
            }
            return null;
        default:
            throw (TypeError("$X: specified type is not valid type."));
        }
    }
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2014/06/19 17:25:21 - 2014-06-19
  2. 2014/06/19 17:21:25 - 2014-06-19
  3. 2013/08/01 14:13:54 - 2013-08-01
  4. 2013/08/01 14:11:45 - 2013-08-01
  5. 2013/08/01 14:10:12 - 2013-08-01
  6. 2013/08/01 14:06:37 - 2013-08-01
  7. 2013/08/01 13:25:47 - 2013-08-01
  8. 2013/08/01 09:59:27 - 2013-08-01
  9. 2013/08/01 09:52:46 - 2013-08-01