2014

the whole world is peaceful.

記録

var getCacheCallback = function(res, url) {
    if (res.status != 200) {
        return getCacheErrorCallback(url)
    }

    var info
    try {
        info = eval(res.responseText).map(function(i) { return i.data })
    }
    catch(e) {
        info = []
        var matched = false
        var hdoc = createHTMLDocumentByString(res.responseText)
        var textareas = getElementsByXPath(
            '//*[@class="autopagerize_data"]', hdoc)
        textareas.forEach(function(textarea) {
            var d = parseInfo(textarea.value)
            if (d) {
                info.push(d)
                if (!matched && location.href.match(d.url)) {
                    matched = d
                }
            }
        })
    }
    if (info.length > 0) {
        info = info.filter(function(i) { return ('url' in i) })
        info.sort(function(a, b) { return (b.url.length - a.url.length) })

        var r_keys = ['url', 'nextLink', 'insertBefore', 'pageElement']
        info = info.map(function(i) {
            var item = {}
            r_keys.forEach(function(key) {
                if (i[key]) {
                    item[key] = i[key]
                }
            })
            return item
        })

        cacheInfo[url] = {
            url: url,
            expire: new Date(new Date().getTime() + CACHE_EXPIRE),
            info: info
        }
        GM_setValue('cacheInfo', cacheInfo.toSource())
        launchAutoPager(info)
    }
    else {
        getCacheErrorCallback(url)
    }
}
var parseInfo = function(str) {
    var lines = str.split(/\r\n|\r|\n/)
    var re = /(^[^:]*?):(.*)$/
    var strip = function(str) {
        return str.replace(/^\s*/, '').replace(/\s*$/, '')
    }
    var info = {}
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].match(re)) {
            info[RegExp.$1] = strip(RegExp.$2)
        }
    }
    var isValid = function(info) {
        var infoProp = ['url', 'nextLink', 'pageElement']
        for (var i = 0; i < infoProp.length; i++) {
            if (!info[infoProp[i]]) {
                return false
            }
        }
        return true
    }
    return isValid(info) ? info : null
}

powered by hatena blog.
the nikki system for lifelogging junkies.

all posts © their original owners.
writing is reusable solely under the BY Creative Commons License.