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

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

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


(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 οnclick="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 οnclick="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 + "\" οnclick=\"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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值