JavaScript 的 getSelection (获得页面的选中文字)

来源:互联网 发布:新sat 知乎 编辑:程序博客网 时间:2024/04/27 16:10

来自新浪前端的一段代码,研究了一些,写的有水平。读起来有点费劲,代码已经被压缩处理过了。

但是核心的部分还在,写的非常的好。


(function() {    function B() {        f.addEntry({            summary: ""        });        m = f.entries.length - 1;        for (var a = 0,        c = "",        b = '<div class="bslist">',        d = 0; d < r.length; d++) {            var e = r[d];            e == v ? (c += "#" + i + " .bslist .icon-" + e + '{background: url("' + w + 'more.gif") center center no-repeat;}', b += '<div class="listitem"><span onclick="javascript:bShare.more(event, ' + m + ');return false;" title="' + n.morePlatsShort + '" class="bsicon icon-' + e + '"></span>', b += "</div>", a++) : g.isUndefined(f.pnMap[e]) || (c += "#" + i + " .bslist .icon-" + e + '{background: url("' + w + e + '.gif") center center no-repeat;}', b += '<div class="listitem"><span onclick="javascript:document.getElementById(\'' + i + "').style.display='none';bShare.share(event,'" + e + "'," + m + ');return false;" title="' + n.shareText + " " + f.pnMap[e][0] + '" class="bsicon icon-' + e + '"></span>', b += "</div>", a++)        }        d = a * 36 + (g.isIe6 ? 2 : 0);        e = 15 * (o.length + 1) + 16;        a = "#" + i + " {position:absolute;background-color:#fff;*display:inline;zoom:1;display:inline-block;}";        a += ".b-drag {padding:2px 10px;border:1px solid #ff5c00;border-radius:8px;overflow:hidden;width:" + (d > e ? d: e) + "px;}";        a += ".b-drag-arrow {position:absolute;margin:-4px 40px;width:10px;height:5px;background:url(" + C + "arrow-orange-hollow.gif) no-repeat;}";        a += ".b-drag .bstitle {display:block;height:15px;*height:18px;font-size:12px;padding:2px;color:#333;background:#fff;border-bottom:1px solid #e9e9e9;overflow:hidden;}";        a += ".b-drag .bstitle span {float:left;}";        a += ".b-drag .bstitle .bsclose {cursor:pointer;float:right;font-family:Arial,\u5b8b\u4f53,sans-serif;font-weight:bold;width:15px;height:15px;text-align:center;}";        a += ".b-drag .bslist {height:40px;margin-top:5px;}";        a += ".b-drag .bslist .listitem {float:left;height:30px;width:34px;margin:0 1px;padding:5px 0 0;cursor:pointer;text-align:center;list-style:none;zoom:1;vertical-align:top;}";        a += ".b-drag .bslist .listitem:hover {border-radius:3px;background-color:#e9e9e9;}";        a += ".b-drag .bslist .bsicon {width:24px;height:24px;margin:0 auto;display:block;}";        a += c;        c = '<span class="b-drag-arrow BSHARE_IMAGE_NO"></span><div class="b-drag"><div class="bstitle"><span>' + o + '...</span><a class="bsclose" title="' + n.closeHint + "\" onclick=\"var date = new Date(); date.setTime(date.getTime() + 1800000); document.cookie='bshare_off=true; expires='+date.toGMTString()+'; path=/'; document.getElementById('" + i + "').style.display='none'; return false;\">X</a></div>";        c += b;        c += "</div></div>";        g.loadStyle(a);        s && g.loadScript(s);        var j = h.getElementById(i);        j || (j = g.createElement("div", i, "", "display:none;", c), x.appendChild(j));        var l = function(a) {            a.onmousedown = function(a) {                p = t(a || k.event)            };            a.onmouseup = function(a) {                if (!D()) {                    a = a || k.event;                    q = t(a);                    if (!p || !q || p.left != q.left || p.top != q.top) u = !0;                    var b;                    if (k.getSelection) b = k.getSelection().toString();                    else if (h.getSelection) b = h.getSelection();                    else if (h.selection) try {                        b = h.selection.createRange().text                    } catch(c) {}                    if (b.length != 0 && u) {                        var y = t(a);                        setTimeout(function() {                            f.entries[m].summary = b;                            f.curb = m;                            j.style.top = y.top + 15 + "px";                            j.style.left = y.left - 40 + "px";                            j.style.display = "inline-block"                        },                        200);                        u = !1;                        g.stopProp(a)                    }                }            }        };        g.getElem(h, "div", z,        function(a) {            l(a)        });        g.getElem(h, "p", z,        function(a) {            l(a)        });        x.onmouseup = function(a) {            a = a || k.event;            for (a = a.srcElement || a.target; a;) {                if (a.id == i || a.id == E) return;                a = a.parentNode            }            j.style.display = "none";            f.entries[m].summary = ""        }    }    function D() {        try {            var a = h.cookie.split(";"),            c,            b,            d = "false";            for (c = 0; c < a.length; c++) {                for (b = a[c]; b.charAt(0) == " ";) b = b.slice(1, b.length);                b.indexOf("bshare_off=") == 0 && (d = b.slice(11, b.length))            }            if (d == "true") return ! 0        } catch(e) {            return ! 0        }        return ! 1    }    function t(a) {        return {            left: a.pageX || a.clientX + l.scrollLeft - l.clientLeft,            top: a.pageY || a.clientY + l.scrollTop - l.clientTop        }    }    var A = !1,    k = window,    h = document,    l = h.documentElement,    x = h.body,    g = k.bShareUtil,    f = k.bShare,    n = f.iL8n || {},    v = "more",    C = f.shost + "/images/",    w = f.imageBasePath + "logos/m2/",    z = "BSHARE_POP",    i = "b-drag-div",    E = "bsMorePanelHolder",    r = ["sinaminiblog", "qqmb", "neteasemb", "sohuminiblog", "renren", "kaixin001", v],    s = null,    u = !1,    q,    p,    m,    o;    g.ready(function() {        var a = function() {            if (f.completed) {                n = f.iL8n;                o = n.shareText;                var c = g.getScript(/((bshare|static).*bshareDrag?(Org)?\.js)/)[0],                b = {};                if (!g.isUndefined(window.bShareOpt)) b = window.bShareOpt;                if (c) {                    var c = g.parseOptions(c.src),                    d;                    for (d in c) b[d] = c[d]                }                if (b.text) o = b.text;                b.bp && (r = b.bp.split(","));                if (b.popjs) s = b.popjs;                B()            } else setTimeout(a, 50)        };        A || (A = !0, a())    })})();




0 0