swdyh (owner)

Revisions

gist: 97052 Download_button fork
public
Public Clone URL: git://gist.github.com/97052.git
Embed All Files: show embed
url2img.user.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// ==UserScript==
// @name url2img
// @namespace http://relucks.org/
// @include http*
// ==/UserScript==
 
(function() {
    url2img(document)
    document.addEventListener("DOMNodeInserted", function(e){
        if (e.target.tagName) {
            setTimeout(function() { url2img(e.target) }, 100)
        }
    }, false)
 
 
    function url2img(doc) {
        var re = new RegExp('(^https?://.+\.(png|jpg|jpeg|gif)$)', 'i')
        $X('//a[@href]', doc).forEach(function(a) {
            if (re.test(a.href) && !hasImg(a)) {
                replace(a)
            }
        })
    }
 
    function hasImg(a) {
        var cs = a.childNodes
        for (var i = 0; i < cs.length; i++) {
            if (cs[i].tagName == 'IMG' && cs[i].src == a.href) {
                return true
            }
        }
        return false
    }
 
    function replace(a) {
        var img = document.createElement('img')
        img.src = a.href
        a.appendChild(img)
        a.style.color = '#fff'
        a.style.backgroundColor = '#0f0'
        img.style.border = '1px solid #0f0'
    }
 
    // http://gist.github.com/raw/3242/1a7950e033a207efcfc233ae8d9939b676bdbf46
    // simple version of $X
    // $X(exp);
    // $X(exp, context);
    function $X (exp, context) {
        context || (context = document);
        var expr = (context.ownerDocument || context).createExpression(exp, function (prefix) {
            return document.createNSResolver(context)(prefix) ||
                (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : "";
        });
 
        var result = expr.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;
    }
})()