A Universal Script to Re-Enable the Selection and Copying

Enables select, right-click, copy and drag on pages that disable them. Enhanced Feature: Alt Key HyperLink Text Selection

当前为 2021-06-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name A Universal Script to Re-Enable the Selection and Copying
  3. // @name:zh-TW A Universal Script to Re-Enable the Selection and Copying
  4. // @version 1.7.9.6
  5. // @icon https://image.flaticon.com/icons/png/128/3848/3848147.png
  6. // @description Enables select, right-click, copy and drag on pages that disable them. Enhanced Feature: Alt Key HyperLink Text Selection
  7. // @description:zh-TW 解除禁止復制、剪切、選擇文本、右鍵菜單的限制。破解鎖右鍵、文字複製、文字選取。增強功能:Alt鍵超連結文字選取。
  8. // @include /^https?\:\/\//
  9. // @grant none
  10. // @run-at document-start
  11. // @namespace https://greasyfork.org/users/371179
  12. // ==/UserScript==
  13. (function $$() {
  14. 'use strict';
  15. if (document == null || !document.documentElement) return window.requestAnimationFrame($$); // this is tampermonkey bug?? not sure
  16. //console.log('script at', location)
  17.  
  18. function $nil() {}
  19.  
  20. function isSupportAdvancedEventListener() {
  21. if ('_b1750' in $) return $._b1750
  22. var prop = 0;
  23. document.createAttribute('z').addEventListener('nil', $nil, {
  24. get passive() {
  25. prop++;
  26. },
  27. get once() {
  28. prop++;
  29. }
  30. });
  31. return ($._b1750 = prop == 2);
  32. }
  33.  
  34. function isSupportPassiveEventListener() {
  35. if ('_b1650' in $) return $._b1650
  36. var prop = 0;
  37. document.createAttribute('z').addEventListener('nil', $nil, {
  38. get passive() {
  39. prop++;
  40. }
  41. });
  42. return ($._b1650 = prop == 1);
  43. }
  44.  
  45. var getSelection = window.getSelection || Error()(),
  46. requestAnimationFrame = window.requestAnimationFrame || Error()(),
  47. getComputedStyle = window.getComputedStyle || Error()();
  48.  
  49. const $ = {
  50. utSelectionColorHack: 'msmtwejkzrqa',
  51. utTapHighlight: 'xfcklblvkjsj',
  52. utLpSelection: 'gykqyzwufxpz',
  53. utLpSelectionKeyDown:'sdaolbulluuw',
  54. utHoverBlock: 'meefgeibrtqx', //scc_emptyblock
  55. utNonEmptyElm: 'ilkpvtsnwmjb',
  56. utNonEmptyElmPrevElm:'jttkfplemwzo',
  57. ksFuncReplacerNonFalse: '___dqzadwpujtct___',
  58. ksEventReturnValue: ' ___ndjfujndrlsx___',
  59. ksSetData: '___rgqclrdllmhr___',
  60.  
  61. eh_capture_passive: () => isSupportPassiveEventListener() ? ($._eh_capture_passive = ($._eh_capture_passive || {
  62. capture: true,
  63. passive: true
  64. })) : true,
  65.  
  66. mAlert_DOWN: function() {}, //dummy function in case alert replacement is not valid
  67. mAlert_UP: function() {}, //dummy function in case alert replacement is not valid
  68.  
  69. isAnySelection: function() {
  70. var sel = getSelection();
  71. return !sel ? null : (typeof sel.isCollapsed == 'boolean') ? !sel.isCollapsed : (sel.toString().length > 0);
  72. },
  73.  
  74. createCSSElement: function(cssStyle, container) {
  75. var css = document.createElement('style'); //slope: DOM throughout
  76. css.type = 'text/css';
  77. css.innerHTML = cssStyle;
  78. if (container) container.appendChild(css);
  79. return css;
  80. },
  81.  
  82. createFakeAlert: function(_alert) {
  83. if (typeof _alert != 'function') return null;
  84.  
  85. function alert(msg) {
  86. alert.__isDisabled__() ? console.log("alert msg disabled: ", msg) : _alert.apply(this, arguments);
  87. };
  88. alert.toString = () => "function alert() { [native code] }";
  89. return alert;
  90. },
  91.  
  92. createFuncReplacer: function(originalFunc, pName, resFX) {
  93. resFX = function(ev) {
  94. var res = originalFunc.apply(this, arguments);
  95. if (!this || this[pName] != resFX) return res; // if this is null or undefined, or this.onXXX is not this function
  96. if (res === false) return; // return undefined when "return false;"
  97. originalFunc[$.ksFuncReplacerNonFalse] = true;
  98. this[pName] = originalFunc; // restore original
  99. return res;
  100. }
  101. resFX.toString = () => originalFunc.toString();
  102. return resFX;
  103. },
  104.  
  105. listenerDisableAll: function(evt) {
  106. var elmNode = evt.target;
  107. var pName = 'on' + evt.type;
  108. evt = null;
  109. Promise.resolve().then(() => {
  110. while (elmNode && elmNode.nodeType > 0) { //i.e. HTMLDocument or HTMLElement
  111. var f = elmNode[pName];
  112. if (typeof f == 'function' && f[$.ksFuncReplacerNonFalse] !== true) {
  113. var nf = $.createFuncReplacer(f, pName);
  114. nf[$.ksFuncReplacerNonFalse] = true;
  115. elmNode[pName] = nf;
  116. }
  117. elmNode = elmNode.parentNode;
  118. }
  119. })
  120. },
  121.  
  122. onceCssHighlightSelection: () => {
  123. if (document.documentElement.hasAttribute($.utLpSelection)) return;
  124. $.onceCssHighlightSelection = null
  125. Promise.resolve().then(() => {
  126. var s = [...document.querySelectorAll('a,p,div,span,b,i,strong,li')].filter(elm => elm.childElementCount === 0); // randomly pick an element containing text only to avoid css style bug
  127. var elm = !s.length ? document.body : s[s.length >> 1];
  128. return elm
  129. }).then(elm => {
  130. var selectionStyle = getComputedStyle(elm, ':selection');
  131. if (/^rgba\(\d+,\s*\d+,\s*\d+,\s*0\)$/.test(selectionStyle.getPropertyValue('background-color'))) document.documentElement.setAttribute($.utSelectionColorHack, "");
  132. return elm;
  133. }).then(elm => {
  134. var elmStyle = getComputedStyle(elm)
  135. if (/^rgba\(\d+,\s*\d+,\s*\d+,\s*0\)$/.test(elmStyle.getPropertyValue('-webkit-tap-highlight-color'))) document.documentElement.setAttribute($.utTapHighlight, "");
  136. })
  137. },
  138.  
  139. isCurrentClipboardDataReplaced: function(clipboardData) {
  140. var items = clipboardData ? clipboardData.items : null;
  141. if (items && items.length > 0) {
  142. for (var i = 0, l = items.length; i < l; i++) {
  143. if (items[i].type == 'text/plain') return true;
  144. }
  145. }
  146. return false;
  147. },
  148.  
  149. replacementSetData: function(_setData, evt) {
  150. if (typeof _setData != 'function') return;
  151.  
  152. function setData() {
  153. var res = _setData.apply(this, arguments);
  154. try {
  155. if (evt.clipboardData === this && this.setData === setData && evt.cancelable && evt.defaultPrevented === false) {
  156. if ($.isCurrentClipboardDataReplaced(evt.clipboardData)) {
  157. evt.preventDefault();
  158. if (evt.defaultPrevented === true) {
  159. this.setData = _setData;
  160. delete this[$.ksSetData];
  161. }
  162. }
  163. }
  164. } catch (e) {}
  165. return res;
  166. }
  167. setData.toString = () => _setData.toString();
  168. evt.clipboardData.setData = setData;
  169. evt.clipboardData[$.ksSetData] = _setData;
  170. },
  171.  
  172.  
  173. enableSelectClickCopy: function() {
  174. $.eyEvts = ['keydown', 'keyup', 'copy', 'contextmenu', 'select', 'selectstart', 'dragstart', 'beforecopy']; //slope: throughout
  175.  
  176. function isDeactivePreventDefault(evt) {
  177. if ($.bypass) return false;
  178. var j = $.eyEvts.indexOf(evt.type);
  179. switch (j) {
  180. case 6:
  181. if ($.enableDragging) return false;
  182. if (evt.target.hasAttribute('draggable')) {
  183. $.enableDragging = true;
  184. return false;
  185. }
  186. //if(evt.target.hasAttribute('draggable')&&evt.target!=window.getSelection().anchorNode)return false;
  187. return true;
  188. case 3:
  189. if (evt.target instanceof Element && (evt.target.textContent || "").trim().length === 0) return false; //exclude elements like video
  190. return true;
  191. case -1:
  192. return false;
  193. case 0:
  194. case 1:
  195. return (evt.keyCode == 67 && (evt.ctrlKey || evt.metaKey) && !evt.altKey && !evt.shiftKey && $.isAnySelection() === true);
  196. case 2:
  197.  
  198. if (!('clipboardData' in evt && 'setData' in DataTransfer.prototype)) return true; // Event oncopy not supporting clipboardData
  199. // see the richtext hack in https://www.cleancss.com/css-beautify/
  200. // see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/copy_event
  201. // see https://w3c.github.io/clipboard-apis/#widl-ClipboardEvent-clipboardData
  202.  
  203. if ($.isCurrentClipboardDataReplaced(evt.clipboardData) == false) { //no replacement data
  204. if (!evt.clipboardData[$.ksSetData] && evt.cancelable && evt.defaultPrevented === false) $.replacementSetData(evt.clipboardData.setData, evt);
  205. return true;
  206. }
  207. var trimedSelectionText = getSelection().toString().trim()
  208. if (trimedSelectionText) {
  209. //there is replacement data and the selection is not empty
  210. console.log("copy event - clipboardData replacement is allowed and the selection is not empty", trimedSelectionText)
  211. return false;
  212. } else {
  213. //there is replacement data and the selection is empty
  214. return false;
  215. }
  216. break; // for js formatting only
  217.  
  218. default:
  219. return true;
  220. }
  221. }
  222.  
  223. Event.prototype.preventDefault = (function(f) {
  224. function preventDefault() {
  225. if (!isDeactivePreventDefault(this)) f.call(this);
  226. }
  227. preventDefault.toString = () => f.toString();
  228. return preventDefault;
  229. })(Event.prototype.preventDefault);
  230.  
  231. Object.defineProperty(Event.prototype, "returnValue", {
  232. get() {
  233. return $.ksEventReturnValue in this ? this[$.ksEventReturnValue] : true;
  234. },
  235. set(newValue) {
  236. if (newValue === false && !isDeactivePreventDefault(this)) this.preventDefault();
  237. this[$.ksEventReturnValue] = newValue;
  238. },
  239. enumerable: true,
  240. configurable: true
  241. });
  242.  
  243. for (var i = 2, eventsCount = $.eyEvts.length; i < eventsCount; i++) {
  244. document.addEventListener($.eyEvts[i], $.listenerDisableAll, true); // Capture Event; passive:false; expected occurrence COMPLETELY before Target Capture and Target Bubble
  245. }
  246.  
  247. var _alert = window.alert; //slope: temporary
  248. if (typeof _alert == 'function') {
  249. var _mAlert = $.createFakeAlert(_alert);
  250. if (_mAlert) {
  251. var clickBlockingTo = 0;
  252. _mAlert.__isDisabled__ = () => clickBlockingTo > +new Date;
  253. $.mAlert_DOWN = () => (clickBlockingTo = +new Date + 50);
  254. $.mAlert_UP = () => (clickBlockingTo = +new Date + 20);
  255. window.alert = _mAlert
  256. }
  257. }
  258.  
  259. },
  260.  
  261. lpCheckPointer: function(targetElm) {
  262. if (targetElm && targetElm.nodeType == 1 && targetElm.matches('*:hover')) {
  263. if (getComputedStyle(targetElm).getPropertyValue('cursor') == 'pointer' && targetElm.textContent) return true;
  264. }
  265. return false;
  266. },
  267.  
  268. lpFullCancel: function(evt, toPreventDefault) {
  269. $.bypass = true;
  270. !toPreventDefault || evt.preventDefault()
  271. evt.stopPropagation();
  272. evt.stopImmediatePropagation();
  273. $.bypass = false;
  274. },
  275.  
  276. lpKeyPressing: false,
  277.  
  278. lpKeyDown: function(evt) {
  279.  
  280. if(evt.key=="Alt" && $.lpKeyPressing==false && evt.altKey && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && evt.target){
  281. $.lpKeyPressing=true;
  282. const rootNode=$.rootHTML(evt.target);
  283. if(rootNode && !rootNode.hasAttribute($.utLpSelectionKeyDown)){
  284.  
  285. for(const elm of rootNode.querySelectorAll(`[${$.utNonEmptyElmPrevElm}], [${$.utNonEmptyElm}]`)){
  286. elm.removeAttribute($.utNonEmptyElmPrevElm)
  287. elm.removeAttribute($.utNonEmptyElm)
  288. }
  289.  
  290. const s=[...rootNode.querySelectorAll('*:not(button, textarea, input, script, noscript, style, link, img, br)')].filter((elm)=>elm.childElementCount===0 && (elm.textContent||'').trim().length>0)
  291.  
  292.  
  293. for(const elm of s){
  294. elm.setAttribute($.utNonEmptyElm,1)
  295. }
  296.  
  297.  
  298. for(const elm of s){
  299.  
  300. let qElm=elm;
  301. let qi=0;
  302.  
  303. while(qElm){
  304. qi++;
  305.  
  306. if(qi>1){
  307. if(qElm.hasAttribute($.utNonEmptyElm))break;
  308. qElm.setAttribute($.utNonEmptyElm,qi)
  309. }
  310.  
  311. let pElm=qElm.previousElementSibling;
  312. while(pElm){
  313. if(pElm.matches(`[${$.utNonEmptyElm}]`) || pElm.querySelector(`[${$.utNonEmptyElm}]`)){
  314. break;
  315. }else if(!pElm.matches(`button, textarea, input, script, noscript, style, link, img, br`)&& (pElm.textContent||'').length===0 && pElm.clientWidth * pElm.clientHeight>0){
  316. pElm.setAttribute($.utNonEmptyElmPrevElm,'')
  317. }
  318. pElm=pElm.previousElementSibling;
  319. }
  320.  
  321. qElm=qElm.parentNode;
  322. if(qElm === rootNode)break;
  323.  
  324. }
  325.  
  326.  
  327.  
  328. }
  329.  
  330. }
  331.  
  332. if(rootNode && !rootNode.hasAttribute($.utLpSelectionKeyDown))rootNode.setAttribute($.utLpSelectionKeyDown, '')
  333.  
  334. }
  335.  
  336. },
  337. lpKeyUp: function(evt) {
  338.  
  339. if(evt.key=="Alt" && $.lpKeyPressing==true && !evt.altKey && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && evt.target){
  340. $.lpKeyPressing=false;
  341.  
  342. const rootNode=$.rootHTML(evt.target);
  343. if(rootNode && rootNode.hasAttribute($.utLpSelectionKeyDown))rootNode.removeAttribute($.utLpSelectionKeyDown)
  344.  
  345.  
  346. for(const elm of rootNode.querySelectorAll(`[${$.utNonEmptyElmPrevElm}], [${$.utNonEmptyElm}]`)){
  347. elm.removeAttribute($.utNonEmptyElmPrevElm)
  348. elm.removeAttribute($.utNonEmptyElm)
  349. }
  350.  
  351.  
  352. }
  353.  
  354. },
  355.  
  356. lpMouseDown: function(evt) {
  357. $.lpMouseActive = 0;
  358. if (evt.altKey && !evt.ctrlKey && !evt.metaKey && !evt.shiftKey && evt.button === 0) {
  359. $.lpMouseActive = 1;
  360. $.lpFullCancel(evt, false);
  361. const rootNode=$.rootHTML(evt.target);
  362. rootNode.setAttribute($.utLpSelection, '');
  363. }
  364. },
  365.  
  366. lpMouseUp: function(evt) {
  367. if ($.lpMouseActive == 1) {
  368. $.lpMouseActive = 2;
  369. const rootNode=$.rootHTML(evt.target);
  370. rootNode.removeAttribute($.utLpSelection);
  371. $.lpFullCancel(evt, false);
  372. if ($.onceCssHighlightSelection) window.requestAnimationFrame($.onceCssHighlightSelection);
  373. }
  374. },
  375.  
  376. lpClick: function(evt) {
  377. if ($.lpMouseActive == 2) {
  378. $.lpFullCancel(evt, false);
  379. }
  380. },
  381.  
  382. lpEnable: function() { // this is an optional feature for modern browser
  383. // the built-in browser feature has already disabled the default event behavior, the coding is just to ensure no "tailor-made behavior" occuring.
  384. document.addEventListener('keydown', $.lpKeyDown, {
  385. capture: true,
  386. passive: true
  387. })
  388. document.addEventListener('keyup', $.lpKeyUp, {
  389. capture: true,
  390. passive: true
  391. })
  392. document.addEventListener('mousedown', $.lpMouseDown, {
  393. capture: true,
  394. passive: true
  395. })
  396. document.addEventListener('mouseup', $.lpMouseUp, {
  397. capture: true,
  398. passive: true
  399. })
  400. document.addEventListener('click', $.lpClick, {
  401. capture: true,
  402. passive: true
  403. })
  404. },
  405.  
  406. rootHTML:(node)=>{
  407.  
  408. if(!node || !(node.nodeType>0))return null;
  409. if(!node.ownerDocument) return node;
  410. let rootNode=node.getRootNode?node.getRootNode():null
  411. if(!rootNode) {
  412. let pElm=node;
  413. while(pElm){
  414.  
  415. if(pElm.parentNode) pElm=pElm.parentNode;
  416. else break;
  417.  
  418. }
  419. rootNode=pElm;
  420. }
  421.  
  422.  
  423. rootNode=rootNode.querySelector('html')||node.ownerDocument.documentElement||null;
  424. return rootNode
  425.  
  426. },
  427.  
  428. mainEnableScript: () => {
  429. var cssStyleOnReady = `
  430. html, html *,
  431. html *::before, html *::after,
  432. html *:hover, html *:link, html *:visited, html *:active,
  433. html *[style], html *[class]{
  434. -khtml-user-select: auto !important; -moz-user-select: auto !important; -ms-user-select: auto !important;
  435. -webkit-touch-callout: default !important; -webkit-user-select: auto !important; user-select: auto !important;
  436. }
  437. *:hover>img[src]{pointer-events:auto !important;}
  438.  
  439. [${$.utSelectionColorHack}] :not(input):not(textarea)::selection{ background-color: Highlight !important; color: HighlightText !important;}
  440. [${$.utSelectionColorHack}] :not(input):not(textarea)::-moz-selection{ background-color: Highlight !important; color: HighlightText !important;}
  441. [${$.utTapHighlight}] *{ -webkit-tap-highlight-color: rgba(0, 0, 0, 0.18) !important;}
  442.  
  443. [${$.utLpSelectionKeyDown}] [${$.utNonEmptyElmPrevElm}]{pointer-events:none !important;}
  444. [${$.utLpSelectionKeyDown}] [${$.utNonEmptyElmPrevElm}]~[${$.utNonEmptyElm}], [${$.utLpSelectionKeyDown}] [${$.utNonEmptyElmPrevElm}]~[${$.utNonEmptyElm}] [${$.utNonEmptyElm}]{z-index:inherit !important;}
  445.  
  446. html[${$.utLpSelection}] *:hover, html[${$.utLpSelection}] *:hover * { cursor:text !important;}
  447. html[${$.utLpSelection}] :not(input):not(textarea)::selection {background-color: rgba(255, 156, 179,0.5) !important;}
  448. html[${$.utLpSelection}] :not(input):not(textarea)::-moz-selection {background-color: rgba(255, 156, 179,0.5) !important;}
  449.  
  450. [${$.utHoverBlock}="2"]{pointer-events:none !important;user-select:none !important;}
  451. img[${$.utHoverBlock}="4"]{display:none !important;}
  452. [${$.utHoverBlock}="7"]{padding:0 !important;}
  453. [${$.utHoverBlock}="7"]>img[${$.utHoverBlock}="4"]:first-child{
  454. display:inline-block !important;
  455. opacity: 0 !important;
  456. padding: 0 !important;
  457. margin: 0 !important;
  458. position: relative !important;
  459. z-index:1 !important;
  460. width: 100% !important;
  461. height: 100% !important;
  462. left: 0 !important;
  463. top: 0 !important;
  464. outline: 0 !important;
  465. border: 0 !important;
  466. box-sizing: border-box !important;
  467. transform: initial !important;
  468. float: left !important;
  469. pointer-events:inherit !important; user-select:none !important;cursor:inherit !important;
  470. }
  471.  
  472. `.trim();
  473.  
  474. $.enableSelectClickCopy()
  475. $.createCSSElement(cssStyleOnReady, document.documentElement);
  476.  
  477. },
  478.  
  479. mainEvents: (listenerPress, listenerRelease) => {
  480. document.addEventListener("mousedown", listenerPress, true); // Capture Event; (desktop)
  481. document.addEventListener("contextmenu", listenerPress, true); // Capture Event; (desktop&mobile)
  482. document.addEventListener("mouseup", listenerRelease, false); // Bubble Event;
  483. },
  484.  
  485. disableHoverBlock: () => {
  486.  
  487. var nMap = new WeakMap()
  488.  
  489. function elmParam(elm) {
  490.  
  491. var mElm = nMap.get(elm);
  492. if (!mElm) nMap.set(elm, mElm = {});
  493. return mElm;
  494. }
  495.  
  496. function overlapArea(rect1, rect2) {
  497.  
  498. let l1 = {
  499. x: rect1.left,
  500. y: rect1.top
  501. }
  502.  
  503. let r1 = {
  504. x: rect1.right,
  505. y: rect1.bottom
  506. }
  507. let l2 = {
  508. x: rect2.left,
  509. y: rect2.top
  510. }
  511.  
  512. let r2 = {
  513. x: rect2.right,
  514. y: rect2.bottom
  515. }
  516.  
  517. // Area of 1st Rectangle
  518. let area1 = Math.abs(l1.x - r1.x) * Math.abs(l1.y - r1.y);
  519.  
  520. // Area of 2nd Rectangle
  521. let area2 = Math.abs(l2.x - r2.x) * Math.abs(l2.y - r2.y);
  522.  
  523. // Length of intersecting part i.e
  524. // start from max(l1.x, l2.x) of
  525. // x-coordinate and end at min(r1.x,
  526. // r2.x) x-coordinate by subtracting
  527. // start from end we get required
  528. // lengths
  529. let x_dist = Math.min(r1.x, r2.x) - Math.max(l1.x, l2.x);
  530. let y_dist = (Math.min(r1.y, r2.y) - Math.max(l1.y, l2.y));
  531. let areaI = 0;
  532. if (x_dist > 0 && y_dist > 0) {
  533. areaI = x_dist * y_dist;
  534. }
  535.  
  536. return {
  537. area1,
  538. area2,
  539. areaI
  540. };
  541.  
  542.  
  543. }
  544.  
  545. function redirectEvent(event, toElement) {
  546.  
  547. toElement.dispatchEvent(new event.constructor(event.type, event));
  548. if (event.type != 'wheel') event.preventDefault();
  549. event.stopPropagation();
  550. }
  551.  
  552. const floatingBlockHover = new WeakMap();
  553.  
  554. let _nImgs = [];
  555.  
  556. function init_nImg(resObj){
  557. const nImg = resObj.elm
  558.  
  559. if(!resObj.cid_fade && 0){
  560. resObj.cid_fade = setInterval(()=>{
  561. if(!nImg || !nImg.parentNode) return (resObj.cid_fade=clearInterval(resObj.cid_fade))
  562. if(getComputedStyle(nImg.parentNode).getPropertyValue('pointer-events')=='none'){
  563. nImg.parentNode.removeChild(nImg)
  564. return (resObj.cid_fade=clearInterval(resObj.cid_fade))
  565. }
  566. },100)
  567. }
  568.  
  569. }
  570.  
  571. function nImgFunc() {
  572.  
  573. for (const s of _nImgs) {
  574. if (s.lastTime + 800 < +new Date) {
  575. s.lastTime = +new Date;
  576. init_nImg(s)
  577. return s.elm
  578. }
  579. }
  580.  
  581. let nImg = document.createElement('img');
  582. nImg.setAttribute('title',' ');
  583. nImg.setAttribute('alt',' ');
  584. nImg.onerror = function() {
  585. if (this.parentNode != null) this.parentNode.removeChild(this)
  586. }
  587. nImg.setAttribute($.utHoverBlock, '4');
  588. const handle = function(event) {
  589. if (this === event.target) {
  590. if (event.button != 2) redirectEvent(event, this.parentNode)
  591. Promise.resolve().then(() => {
  592. for (const s of _nImgs) {
  593. if (s.elm === this) {
  594. s.lastTime = +new Date
  595. }
  596. }
  597. })
  598. }
  599. }
  600. nImg.addEventListener('click', handle, true);
  601. nImg.addEventListener('mousedown', handle, true);
  602. nImg.addEventListener('mouseup', handle, true);
  603. nImg.addEventListener('mousemove', handle, true);
  604. nImg.addEventListener('mouseover', handle, true);
  605. nImg.addEventListener('mouseout', handle, true);
  606. nImg.addEventListener('mouseenter', handle, true);
  607. nImg.addEventListener('mouseleave', handle, true);
  608. //nImg.addEventListener('wheel', handle, $.eh_capture_passive());
  609. let resObj={
  610. elm: nImg,
  611. lastTime: +new Date,
  612. cid_fade:0
  613. }
  614. _nImgs.push(resObj)
  615. init_nImg(resObj)
  616.  
  617. return nImg;
  618.  
  619. }
  620.  
  621. const wmHoverUrl = new WeakMap();
  622. let lastMouseEnterElm = null;
  623. let lastMouseEnterAt = 0;
  624. let lastMouseEnterCid = 0;
  625.  
  626. function mouseEnter() {
  627. lastMouseEnterCid = 0;
  628.  
  629. if (+new Date - lastMouseEnterAt < 30) {
  630. lastMouseEnterCid = setTimeout(mouseEnter, 82)
  631. return;
  632. }
  633.  
  634. //if($.lpKeyPressing)return;
  635.  
  636. const targetElm = lastMouseEnterElm
  637.  
  638. Promise.resolve()
  639. .then(() => {
  640. if (targetElm && targetElm.parentNode) {} else {
  641. return;
  642. }
  643. if (floatingBlockHover.get(targetElm)) {
  644. let url = null
  645. if (targetElm.getAttribute($.utHoverBlock) == '7' && (url = wmHoverUrl.get(targetElm)) && targetElm.querySelector(`[${$.utHoverBlock}]`) == null) {
  646. let _nImg = nImgFunc();
  647. if (_nImg.parentNode !== targetElm) {
  648. _nImg.setAttribute('src', url);
  649. targetElm.insertBefore(_nImg, targetElm.firstChild);
  650. }
  651. }
  652. return;
  653. }
  654. floatingBlockHover.set(targetElm, 1);
  655. return 1;
  656. }).then((ayRes) => {
  657. if (!ayRes) return;
  658.  
  659. if (targetElm.nodeType != 1) return;
  660. if ("|SVG|IMG|HTML|BODY|VIDEO|AUDIO|BR|HEAD|NOSCRIPT|SCRIPT|STYLE|TEXTAREA|AREA|INPUT|FORM|BUTTON|".indexOf(`|${targetElm.nodeName}|`) >= 0) return;
  661.  
  662. const targetArea = targetElm.clientWidth * targetElm.clientHeight
  663.  
  664. if (targetArea > 0) {} else {
  665. return;
  666. }
  667.  
  668. const targetCSS = getComputedStyle(targetElm)
  669. const targetBgImage = targetCSS.getPropertyValue('background-image');
  670. let exec1 = null
  671.  
  672. if (targetBgImage != 'none' && (exec1 = /^\s*url\s*\("?([^"\)]+\b(\.gif|\.png|\.jpeg|\.jpg|\.webp)\b[^"\)]*)"?\)\s*$/i.exec(targetBgImage))) {
  673. if ((targetElm.textContent || "").trim().length > 0) return;
  674. const url = exec1[1];
  675. return url
  676.  
  677. // console.log(targetBgImage,[...exec1])
  678. }
  679.  
  680.  
  681.  
  682. if (targetCSS.getPropertyValue('position') == 'absolute' && +targetCSS.getPropertyValue('z-index') > 0) {} else {
  683. return;
  684. }
  685. if ((targetElm.textContent || "").trim().length > 0) return;
  686.  
  687. let possibleResults = [];
  688.  
  689. for (const imgElm of document.querySelectorAll('img[src]')) {
  690. const param = elmParam(imgElm)
  691. if (!param.area) {
  692. const area = imgElm.clientWidth * imgElm.clientHeight
  693. if (area > 0) param.area = area;
  694. }
  695. if (param.area > 0) {
  696. if (targetArea > param.area * 0.9) possibleResults.push(imgElm)
  697. }
  698. }
  699.  
  700. let i = 0;
  701. let j = 0;
  702. for (const imgElm of possibleResults) {
  703.  
  704. const cmpVal = targetElm.compareDocumentPosition(imgElm)
  705.  
  706. /*
  707.  
  708.  
  709. 1: The two nodes do not belong to the same document.
  710. 2: p1 is positioned after p2.
  711. 4: p1 is positioned before p2.
  712. 8: p1 is positioned inside p2.
  713. 16: p2 is positioned inside p1.
  714. 32: The two nodes has no relationship, or they are two attributes on the same element.
  715.  
  716. */
  717.  
  718. if (cmpVal & 8 || cmpVal & 16) return;
  719. if (cmpVal & 2) j++; // I<p
  720. else if (cmpVal & 4) break; // I>p
  721.  
  722.  
  723. i++;
  724.  
  725. }
  726.  
  727. // before: j-1 after: j
  728.  
  729. let indexBefore = j - 1;
  730. let indexAfter = j;
  731. if (indexBefore < 0) indexBefore = 0;
  732. if (indexAfter > possibleResults.length - 1) indexAfter = possibleResults.length - 1;
  733.  
  734. // setTimeout(function(){
  735. for (let i = indexBefore; i <= indexAfter; i++) {
  736. const s = possibleResults[i];
  737. const {
  738. area1,
  739. area2,
  740. areaI
  741. } = overlapArea(targetElm.getBoundingClientRect(), s.getBoundingClientRect())
  742. const criteria = area1 * 0.7
  743. if (areaI > 0.9 * area2) {
  744.  
  745.  
  746. return s.getAttribute('src')
  747.  
  748.  
  749. }
  750. }
  751. // },1000);
  752.  
  753. }).then((sUrl) => {
  754.  
  755. if (typeof sUrl != 'string') return;
  756.  
  757. //console.log(targetElm, targetElm.querySelectorAll('img').length)
  758.  
  759. // console.log(313, evt.target, s)
  760. let _nImg = nImgFunc();
  761.  
  762.  
  763. if (_nImg.parentNode !== targetElm) {
  764. _nImg.setAttribute('src', sUrl);
  765. targetElm.insertBefore(_nImg, targetElm.firstChild);
  766. wmHoverUrl.set(targetElm, sUrl);
  767. targetElm.setAttribute($.utHoverBlock, '7');
  768. }
  769.  
  770.  
  771.  
  772. })
  773.  
  774. }
  775.  
  776. document.addEventListener('mouseenter', function(evt) {
  777. lastMouseEnterElm = evt.target
  778. lastMouseEnterAt = +new Date;
  779. if (!lastMouseEnterCid) lastMouseEnterCid = setTimeout(mouseEnter, 82)
  780. }, $.eh_capture_passive())
  781.  
  782.  
  783.  
  784. }
  785.  
  786. }
  787.  
  788. // $.holdingElm=null;
  789.  
  790. $.mainEnableScript();
  791.  
  792. if (isSupportAdvancedEventListener()) $.lpEnable(); // top capture event for alt-click
  793.  
  794. $.mainEvents(
  795. function(evt) {
  796. // $.holdingElm=evt.target;
  797. // console.log('down',evt.target)
  798. if ($.onceCssHighlightSelection) window.requestAnimationFrame($.onceCssHighlightSelection);
  799. if (evt.button == 2 || evt.type == "contextmenu") $.mAlert_DOWN();
  800. },
  801. function(evt) {
  802. // $.holdingElm=null;
  803. // console.log('up',evt.target)
  804. if (evt.button == 2) $.mAlert_UP();
  805. if ($.enableDragging) {
  806. $.enableDragging = false;
  807. }
  808. }
  809. );
  810.  
  811. $.disableHoverBlock();
  812.  
  813. console.log('userscript running - To Re-Enable Selection & Copying');
  814.  
  815.  
  816.  
  817.  
  818. })();