Web选择

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

当前为 2023-10-04 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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