Web选择

由于Web选择太好用,微软就把他砍掉了。本脚本实现了Web选择的部分功能。按下Alt+S即可选择文本。

目前为 2024-06-08 提交的版本。查看 最新版本

// ==UserScript==
// @name                Web选择
// @name:en             Web Select
// @name:zh-TW          Web捕獲
// @namespace           http://howardzhangdqs.eu.org/
// @source              https://github.com/Howardzhangdqs/web-select
// @version             0.2.2
// @description         由于Web选择太好用,微软就把他砍掉了。本脚本实现了Web选择的部分功能。按下Alt+S即可选择文本。
// @description:en      Due to the ease with which the "web select" was used, Microsoft cut it off. This script implements some functions of "web select". Press Alt+S to select text.
// @description:zh-TW   由於Web選擇功能非常實用,微軟就將它砍掉了。本腳本實現了Web選擇的部分功能。按下Alt+S即可選擇文本。
// @author              HowardZhangdqs
// @match               *://*/*
// @license             MIT
// @icon                data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABsUExURf///xYWFgMDAw4ODgICAh8fHw4ODQAAAA8PD9LS0gEBAR4eHgcHBwoKCgUFBQgICCAgICgoKCwsLB0dHSQkJCYmJvv7+vr6+ZiYlyMjIhwcHJaWlBsbGxgYGAYGBnV2dfz9/BAQEHR0cw0NDWBB0hYAAAFaSURBVFjD7VbZkoMgEARBwNvNsfed///HAJoUq3Ga1a0tH+ynQXragWmtYWzDelEopXzQmgu+/Jorxe9gOtceNjI6gF2X14hC0ic8MZYPBIJwGqonlSzIsHhkkQpu/xlVqKa3D3b7QJ+RLqHAlyRIisICaqkAXYE9YYKMAijYaTGU/0dWu+4+eL9DcjOm1J1DZZQP2jFF6qtARBvNiJK65FxK+TlTwOWnfXyaIZDZB9UvnHhT4HWpQLoJ/KGANcUH/h98EwJzvsSVCST6HiUMKT8EWtyFguyCWdrGxQJHLKCRkYDCzv2+yC644cCY7r78eCOEEd2aC+OHjh3lg2CIGI44TTh8TBupZx0nRxyBnFi4xGYw4rxc1uVKv4U9JGW0M96xeQiFHLoPnNLNtm+LrknTFWIBrgGhQm/oBoSUfAF9S1IjIKdUIF9iO5FF8ChL7it5EzVnG1aMM5cYFmmzQuXPAAAAAElFTkSuQmCC
// @grant               none
// ==/UserScript==

(() => {
    "use strict";
    var t = {
            border: "1px solid #fff",
            outline: "1px dashed #000",
            outlineOffset: "-1px",
            zIndex: "99999",
        },
        e = {
            border: "1px solid #fff",
            outline: "1px solid #000",
            outlineOffset: "0",
            zIndex: "99999",
        },
        n = { position: "fixed", outline: "3px dashed white", zIndex: "99999" },
        o = function (t, e) {
            for (var n in (t instanceof HTMLElement && (t = [t]), e))
                for (var o = 0, i = t; o < i.length; o++) i[o].style[n] = e[n];
        },
        i = function (e) {
            var n = e.top,
                i = e.left,
                r = e.width,
                c = e.height,
                l = e.text,
                a = e.src,
                u = document.createElement("div");
            return (
                (u.style.position = "fixed"),
                (u.style.top = "".concat(n, "px")),
                (u.style.left = "".concat(i, "px")),
                (u.style.width = "".concat(r, "px")),
                (u.style.height = "".concat(c, "px")),
                o(u, t),
                (u.style.zIndex = "99999"),
                document.body.appendChild(u),
                { top: n, left: i, width: r, height: c, text: l, el: u, src: a }
            );
        },
        r = function (i) {
            return new Promise(function (r, c) {
                var l = document.createElement("div");
                o(l, n), document.body.appendChild(l);
                var a = [],
                    u = {
                        _p1: [0, 0],
                        _p2: [0, 0],
                        set p1(t) {
                            (this._p1[0] = Math.floor(t[0])),
                                (this._p1[1] = Math.floor(t[1])),
                                this.runwatch();
                        },
                        set p2(t) {
                            (this._p2[0] = Math.floor(t[0])),
                                (this._p2[1] = Math.floor(t[1])),
                                this.runwatch();
                        },
                        get p1() {
                            return this._p1;
                        },
                        get p2() {
                            return this._p2;
                        },
                        get top() {
                            return Math.min(this._p1[0], this._p2[0]);
                        },
                        get left() {
                            return Math.min(this._p1[1], this._p2[1]);
                        },
                        get width() {
                            return Math.abs(this._p1[1] - this._p2[1]);
                        },
                        get height() {
                            return Math.abs(this._p1[0] - this._p2[0]);
                        },
                        watchfn: [],
                        watch: function (t) {
                            this.watchfn.push(t);
                        },
                        unwatch: function (t) {
                            this.watchfn = this.watchfn.filter(function (e) {
                                return e !== t;
                            });
                        },
                        runwatch: function () {
                            for (var t = 0, e = this.watchfn; t < e.length; t++)
                                (0, e[t])(this);
                        },
                    },
                    h = function (t) {
                        var e, n, i, r;
                        t.preventDefault(),
                            a.push.apply(
                                a,
                                ((e = document.createElement("div")),
                                (n = document.createElement("div")),
                                (i = document.createElement("div")),
                                (r = document.createElement("div")),
                                o([e, n, i, r], {
                                    position: "fixed",
                                    top: "0",
                                    left: "0",
                                    background: "#0007",
                                    zIndex: "99997",
                                }),
                                u.watch(function () {
                                    o(e, {
                                        top: "0",
                                        left: "0",
                                        width: "".concat(u.left, "px"),
                                        height: "100%",
                                    }),
                                        o(n, {
                                            top: "0",
                                            left: "".concat(u.left, "px"),
                                            width: "".concat(u.width, "px"),
                                            height: "".concat(u.top, "px"),
                                        }),
                                        o(i, {
                                            top: "".concat(
                                                u.top + u.height,
                                                "px",
                                            ),
                                            left: "".concat(u.left, "px"),
                                            width: "".concat(u.width, "px"),
                                            height: "calc(100% - ".concat(
                                                u.top + u.height,
                                                "px)",
                                            ),
                                        }),
                                        o(r, {
                                            top: "0px",
                                            left: "".concat(
                                                u.left + u.width,
                                                "px",
                                            ),
                                            width: "calc(100% - ".concat(
                                                u.left + u.width,
                                                "px)",
                                            ),
                                            height: "100vh",
                                        });
                                }),
                                document.body.appendChild(e),
                                document.body.appendChild(n),
                                document.body.appendChild(i),
                                document.body.appendChild(r),
                                [e, n, i, r]),
                            ),
                            (l.style.display = "block"),
                            (u.p1 = [t.clientY, t.clientX]),
                            (u.p2 = [t.clientY, t.clientX]),
                            (l.style.top = "".concat(u.top, "px")),
                            (l.style.left = "".concat(u.left, "px")),
                            (l.style.width = "0"),
                            (l.style.height = "0"),
                            document.addEventListener("mousemove", s),
                            document.addEventListener("mouseup", f);
                    },
                    p = function () {
                        return i.filter(function (t) {
                            return !(
                                t.left < u.left ||
                                t.top < u.top ||
                                t.left + t.width > u.left + u.width ||
                                t.top + t.height > u.top + u.height
                            );
                        });
                    },
                    s = function (n) {
                        n.preventDefault(),
                            (u.p2 = [n.clientY, n.clientX]),
                            (l.style.top = "".concat(u.top, "px")),
                            (l.style.left = "".concat(u.left, "px")),
                            (l.style.width = "".concat(u.width, "px")),
                            (l.style.height = "".concat(u.height, "px"));
                        for (var r = p(), c = 0, a = i; c < a.length; c++) {
                            var h = a[c];
                            r.includes(h) ? o(h.el, e) : o(h.el, t);
                        }
                        console.log(
                            r
                                .sort(function (t, e) {
                                    return e.left - t.left;
                                })
                                .sort(function (t, e) {
                                    return e.top - t.top;
                                }),
                        );
                    },
                    f = function (t) {
                        document.removeEventListener("mousemove", s),
                            document.removeEventListener("mouseup", f),
                            document.removeEventListener("mousedown", h),
                            l.remove();
                        for (var e = 0, n = a; e < n.length; e++) n[e].remove();
                        o(document.body, { pointerEvents: "" }),
                            r({
                                top: parseInt(l.style.top),
                                left: parseInt(l.style.left),
                                width: parseInt(l.style.width),
                                height: parseInt(l.style.height),
                                el: p(),
                            });
                    };
                document.addEventListener("mousedown", h),
                    document.addEventListener("mouseup", f),
                    o(document.body, { pointerEvents: "none" });
            });
        },
        c = function (t, e) {
            for (var n = e.parentNode; null !== n; ) {
                if (n === t) return !0;
                n = n.parentNode;
            }
            return !1;
        },
        l = function () {
            return (
                (t = void 0),
                (e = void 0),
                (l = function () {
                    var t, e, n, l, a, u, h, p, s, f;
                    return (function (t, e) {
                        var n,
                            o,
                            i,
                            r,
                            c = {
                                label: 0,
                                sent: function () {
                                    if (1 & i[0]) throw i[1];
                                    return i[1];
                                },
                                trys: [],
                                ops: [],
                            };
                        return (
                            (r = { next: l(0), throw: l(1), return: l(2) }),
                            "function" == typeof Symbol &&
                                (r[Symbol.iterator] = function () {
                                    return this;
                                }),
                            r
                        );
                        function l(l) {
                            return function (a) {
                                return (function (l) {
                                    if (n)
                                        throw new TypeError(
                                            "Generator is already executing.",
                                        );
                                    for (; r && ((r = 0), l[0] && (c = 0)), c; )
                                        try {
                                            if (
                                                ((n = 1),
                                                o &&
                                                    (i =
                                                        2 & l[0]
                                                            ? o.return
                                                            : l[0]
                                                              ? o.throw ||
                                                                ((i =
                                                                    o.return) &&
                                                                    i.call(o),
                                                                0)
                                                              : o.next) &&
                                                    !(i = i.call(o, l[1])).done)
                                            )
                                                return i;
                                            switch (
                                                ((o = 0),
                                                i && (l = [2 & l[0], i.value]),
                                                l[0])
                                            ) {
                                                case 0:
                                                case 1:
                                                    i = l;
                                                    break;
                                                case 4:
                                                    return (
                                                        c.label++,
                                                        {
                                                            value: l[1],
                                                            done: !1,
                                                        }
                                                    );
                                                case 5:
                                                    c.label++,
                                                        (o = l[1]),
                                                        (l = [0]);
                                                    continue;
                                                case 7:
                                                    (l = c.ops.pop()),
                                                        c.trys.pop();
                                                    continue;
                                                default:
                                                    if (
                                                        !(
                                                            (i =
                                                                (i = c.trys)
                                                                    .length >
                                                                    0 &&
                                                                i[
                                                                    i.length - 1
                                                                ]) ||
                                                            (6 !== l[0] &&
                                                                2 !== l[0])
                                                        )
                                                    ) {
                                                        c = 0;
                                                        continue;
                                                    }
                                                    if (
                                                        3 === l[0] &&
                                                        (!i ||
                                                            (l[1] > i[0] &&
                                                                l[1] < i[3]))
                                                    ) {
                                                        c.label = l[1];
                                                        break;
                                                    }
                                                    if (
                                                        6 === l[0] &&
                                                        c.label < i[1]
                                                    ) {
                                                        (c.label = i[1]),
                                                            (i = l);
                                                        break;
                                                    }
                                                    if (i && c.label < i[2]) {
                                                        (c.label = i[2]),
                                                            c.ops.push(l);
                                                        break;
                                                    }
                                                    i[2] && c.ops.pop(),
                                                        c.trys.pop();
                                                    continue;
                                            }
                                            l = e.call(t, c);
                                        } catch (t) {
                                            (l = [6, t]), (o = 0);
                                        } finally {
                                            n = i = 0;
                                        }
                                    if (5 & l[0]) throw l[1];
                                    return {
                                        value: l[0] ? l[1] : void 0,
                                        done: !0,
                                    };
                                })([l, a]);
                            };
                        }
                    })(this, function (d) {
                        switch (d.label) {
                            case 0:
                                for (
                                    t = document.createElement("div"),
                                        o(t, {
                                            position: "fixed",
                                            top: "0",
                                            left: "0",
                                            inlineSize: "100%",
                                            blockSize: "100%",
                                            zIndex: "99998",
                                            background: "#0007",
                                        }),
                                        v = [],
                                        document
                                            .querySelectorAll("*")
                                            .forEach(function (t) {
                                                var e =
                                                        t.getBoundingClientRect(),
                                                    n = e.top,
                                                    o = e.left,
                                                    i = e.width,
                                                    r = e.height;
                                                n &&
                                                    o &&
                                                    i &&
                                                    r &&
                                                    t.innerText &&
                                                    v.push({
                                                        top: n,
                                                        left: o,
                                                        width: i,
                                                        height: r,
                                                        text: t.innerText,
                                                        src: t,
                                                    });
                                            }),
                                        e = v,
                                        console.log(e),
                                        n = [],
                                        l = 0,
                                        a = e;
                                    l < a.length;
                                    l++
                                )
                                    (u = a[l]), n.push(i(u));
                                return [4, r(n)];
                            case 1:
                                for (
                                    h = d.sent(), p = 0, s = n;
                                    p < s.length;
                                    p++
                                )
                                    s[p].el.remove();
                                return (
                                    (f = h.el
                                        .filter(function (t, e, n) {
                                            console.log(t, e, n);
                                            for (
                                                var o = 0, i = n;
                                                o < i.length;
                                                o++
                                            ) {
                                                var r = i[o];
                                                if (c(r.src, t.src)) return !1;
                                            }
                                            return !0;
                                        })
                                        .sort(function (t, e) {
                                            return t.left - e.left;
                                        })
                                        .sort(function (t, e) {
                                            return t.top - e.top;
                                        })),
                                    console.log(
                                        f.map(function (t) {
                                            return t.text;
                                        }),
                                    ),
                                    navigator.clipboard.writeText(
                                        f
                                            .map(function (t) {
                                                return t.text;
                                            })
                                            .join("\n\n"),
                                    ),
                                    [2]
                                );
                        }
                        var v;
                    });
                }),
                new ((n = void 0) || (n = Promise))(function (o, i) {
                    function r(t) {
                        try {
                            a(l.next(t));
                        } catch (t) {
                            i(t);
                        }
                    }
                    function c(t) {
                        try {
                            a(l.throw(t));
                        } catch (t) {
                            i(t);
                        }
                    }
                    function a(t) {
                        var e;
                        t.done
                            ? o(t.value)
                            : ((e = t.value),
                              e instanceof n
                                  ? e
                                  : new n(function (t) {
                                        t(e);
                                    })).then(r, c);
                    }
                    a((l = l.apply(t, e || [])).next());
                })
            );
            var t, e, n, l;
        };
    window.addEventListener("keydown", function (t) {
        "s" !== t.key || !t.altKey || t.ctrlKey || t.shiftKey || l();
    });
})();