通用脚本重开启选取复制

破解锁右键,解除禁止复制、剪切、选择文本、右键菜单、文字复制、文字选取、图片右键等限制。增强功能:Alt键超连结文字选取。

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

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