From 373a297e86af9622c1ac1a80e69e7a548abb1471 Mon Sep 17 00:00:00 2001 From: lee Date: Fri, 7 Aug 2020 14:56:37 +0800 Subject: [PATCH 1/4] clean code. --- core/js/marker.js | 64 ++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/core/js/marker.js b/core/js/marker.js index 15a073c..21857d6 100644 --- a/core/js/marker.js +++ b/core/js/marker.js @@ -1,13 +1,38 @@ (function(_) { - var self = {} + let self; _.Marker = self = {}; + + function getVisibleElements(filter) { + let all = Array.from(document.documentElement.getElementsByTagName("*")); + let visibleElements = []; + for (let i = 0; i < all.length; i++) { + let e = all[i]; + // include elements in a shadowRoot. + if (e.shadowRoot) { + let cc = e.shadowRoot.querySelectorAll('*'); + for (let j = 0; j < cc.length; j++) { + all.push(cc[j]); + } + } + let rect = e.getBoundingClientRect(); + if ( (rect.top <= window.innerHeight) && (rect.bottom >= 0) + && (rect.left <= window.innerWidth) && (rect.right >= 0) + && rect.height > 0 + && getComputedStyle(e).visibility !== 'hidden' + ) { + filter(e, visibleElements); + } + } + return visibleElements; + } + function moveCursorToEnd(el) { if (typeof el.selectionStart == "number") { el.selectionStart = el.selectionEnd = el.value.length; } else if (typeof el.createTextRange != "undefined") { el.focus(); - var range = el.createTextRange(); + let range = el.createTextRange(); range.collapse(false); range.select(); } @@ -62,31 +87,6 @@ } } - function getVisibleElements(filter) { - var all = Array.from(document.documentElement.getElementsByTagName("*")); - var visibleElements = []; - for (var i = 0; i < all.length; i++) { - var e = all[i]; - // include elements in a shadowRoot. - if (e.shadowRoot) { - var cc = e.shadowRoot.querySelectorAll('*'); - for (var j = 0; j < cc.length; j++) { - all.push(cc[j]); - } - } - var rect = e.getBoundingClientRect(); - if ( (rect.top <= window.innerHeight) && (rect.bottom >= 0) - && (rect.left <= window.innerWidth) && (rect.right >= 0) - && rect.height > 0 - && getComputedStyle(e).visibility !== 'hidden' - ) { - filter(e, visibleElements); - } - } - return visibleElements; - } - - function cAdd1(keyCounter, index, maxDigit){ if(keyCounter[index] + 1 == maxDigit){ keyCounter[index] = 0; @@ -118,6 +118,7 @@ } } + self.generateMarker = (selectors) => { let style = document.createElement('style'); @@ -179,9 +180,9 @@ z-index: 100000;\ let validRects = []; if (typeof(selectors)=="function"){ - addElementToRects(validRects, selectors.call()); // collect links + addElementToRects(validRects, selectors()); }else if (typeof(selectors) == "string"){ - selectors = selectors.split(",") + selectors = selectors.split(","); selectors.forEach((s)=>addElementToRects(validRects, document.querySelectorAll(s.trim()))); } @@ -212,7 +213,7 @@ z-index: 100000;\ if (match !== undefined && callback !== undefined){ let selectors = match.getAttribute('pointed-link'); let node = document.querySelector(selectors); - return callback(node) + return callback(node); } else return ""; } @@ -249,9 +250,10 @@ z-index: 100000;\ } else if(node.nodeName.toLowerCase() === 'a') { node.click(); // most general a tag without href } - return "" + return ""; } + self.generateTextNodeMarker = () => { var elements = getVisibleElements(function(e, v) { var aa = e.childNodes; From faef79284a457184c488559cf9b7b03112935846 Mon Sep 17 00:00:00 2001 From: lee Date: Fri, 7 Aug 2020 15:05:36 +0800 Subject: [PATCH 2/4] Remove unused js file. --- core/browser.py | 4 - core/js/get_codes.js | 156 -------------------------------- core/js/get_markers.js | 199 ----------------------------------------- core/js/goto_code.js | 31 ------- core/js/goto_marker.js | 59 ------------ 5 files changed, 449 deletions(-) delete mode 100644 core/js/get_codes.js delete mode 100644 core/js/get_markers.js delete mode 100644 core/js/goto_code.js delete mode 100644 core/js/goto_marker.js diff --git a/core/browser.py b/core/browser.py index db702f9..8405e5e 100644 --- a/core/browser.py +++ b/core/browser.py @@ -68,11 +68,7 @@ class BrowserView(QWebEngineView): self.search_term = "" - self.get_markers_raw = self.read_js_content("get_markers.js") - self.goto_marker_raw = self.read_js_content("goto_marker.js") self.marker_js = self.read_js_content("marker.js") - self.get_codes_raw = self.read_js_content("get_codes.js") - self.goto_code_raw = self.read_js_content("goto_code.js") self.get_focus_text_js = self.read_js_content("get_focus_text.js") self.set_focus_text_raw = self.read_js_content("set_focus_text.js") self.clear_focus_js = self.read_js_content("clear_focus.js") diff --git a/core/js/get_codes.js b/core/js/get_codes.js deleted file mode 100644 index 292620a..0000000 --- a/core/js/get_codes.js +++ /dev/null @@ -1,156 +0,0 @@ -(function() { - function cssSelector(el) { - let path = [], parent; - while (parent = el.parentNode) { - path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el)+1})`); - el = parent; - } - return `${path.join(' > ')}`.toLowerCase(); - } - - function getCoords(link){ - let rect = link.getBoundingClientRect(); - return [ rect.top, rect.left, rect.right, rect.bottom, cssSelector(link) ]; - } - - function isElementOnScreen(rect){ - let clientHeight = document.documentElement.clientHeight; - let clientWidth = document.documentElement.clientWidth; - return (rect[0] >= 0 && rect[0] <= clientHeight && - rect[1] >= 0 && rect[1] <= clientWidth && - rect[2] != 0 && rect[3] != 0); - } - - function isElementOnTop(element, rect){ - let topElement = document.elementFromPoint((rect[1] + rect[2])/2, (rect[0] + rect[3])/2); - return topElement != undefined && (element.isSameNode(topElement) || element.contains(topElement) || topElement.contains(element)); - } - - function hasCopy(validRects, rect){ - for(let i = 0; i < validRects.length; i++) { - let each = validRects[i]; - if(each[0] === rect[0] && each[1] === rect[1]){ - return true; - } - } - return false; - } - - function addElementToRects(validRects, elements){ - let rect; - for(let i = 0; i < elements.length; i++) { - rect = getCoords(elements[i]); - if(!hasCopy(validRects, rect) && - isElementOnScreen(rect) && - isElementOnTop(elements[i], rect)){ - validRects.push(rect); - } - } - } - - function cAdd1(keyCounter, index, maxDigit){ - if(keyCounter[index] + 1 == maxDigit){ - keyCounter[index] = 0; - cAdd1(keyCounter, index + 1, maxDigit); - } else { - keyCounter[index]++; - } - } - - function generateKeys(markerContainer) { - let lettersString = "%1"; - let letters = lettersString.split(""); - let linkNum = markerContainer.children.length; - let keyLen = linkNum == 1 ? 1 : Math.ceil(Math.log(linkNum)/Math.log(letters.length)); - let keyCounter = []; - for(let i = 0; i < keyLen; i++) keyCounter[i] = 0; - for(let l = 0; l < linkNum; l++) { - let keyStr = ''; - for(let k = 0; k < keyLen; k++) { - let mark = document.createElement('span'); - mark.setAttribute('class', 'eaf-mark'); - let key = letters[keyCounter[k]]; - mark.textContent = key; - markerContainer.children[l].appendChild(mark); - keyStr += key; - cAdd1(keyCounter, 0, letters.length); - } - markerContainer.children[l].id = keyStr; - } - } - - let style = document.createElement('style'); - document.head.appendChild(style); - style.type = 'text/css'; - style.setAttribute('class', 'eaf-style'); - style.appendChild(document.createTextNode('\ -.eaf-mark {\ -background: none;\ -border: none;\ -bottom: auto;\ -box-shadow: none;\ -color: black !important;\ -cursor: auto;\ -display: inline;\ -float: none;\ -font-size: inherit;\ -font-variant: normal;\ -font-weight: bold;\ -height: auto;\ -left: auto;\ -letter-spacing: 0;\ -line-height: 100%;\ -margin: 0;\ -max-height: none;\ -max-width: none;\ -min-height: 0;\ -min-width: 0;\ -opacity: 1;\ -padding: 0;\ -position: static;\ -right: auto;\ -text-align: left;\ -text-decoration: none;\ -text-indent: 0;\ -text-shadow: none;\ -text-transform: none;\ -top: auto;\ -vertical-align: baseline;\ -white-space: normal;\ -width: auto;\ -z-index: 100000;\ -}')); - - style.appendChild(document.createTextNode('\ -.eaf-marker {\ -position: fixed;\ -display: block;\ -white-space: nowrap;\ -overflow: hidden;\ -font-size: 11.5px;\ -background: linear-gradient(to bottom, #ffdd6e 0%, #deb050 100%);\ -padding-left: 3px;\ -padding-right: 3px;\ -border: 1px solid #c38a22;\ -border-radius: 3px;\ -box-shadow: 0px 3px 7px 0px rgba(0, 0, 0, 0.3);\ -z-index: 100000;\ -}')); - - let validRects = []; - addElementToRects(validRects, document.querySelectorAll('pre')); - - let body = document.querySelector('body'); - let markerContainer = document.createElement('div'); - markerContainer.setAttribute('class', 'eaf-marker-container'); - body.insertAdjacentElement('afterend', markerContainer); - for(let i = 0; i < validRects.length; i++) { - let marker = document.createElement('div'); - marker.setAttribute('class', 'eaf-marker'); - marker.setAttribute('style', 'left: ' + validRects[i][1] + 'px; top: ' + validRects[i][0] + 'px;'); - marker.setAttribute('pointed-link', validRects[i][4]); - - markerContainer.appendChild(marker); - } - generateKeys(markerContainer); -})(); diff --git a/core/js/get_markers.js b/core/js/get_markers.js deleted file mode 100644 index f921a3e..0000000 --- a/core/js/get_markers.js +++ /dev/null @@ -1,199 +0,0 @@ -(function() { - function cssSelector(el) { - let path = [], parent; - while (parent = el.parentNode) { - path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el)+1})`); - el = parent; - } - return `${path.join(' > ')}`.toLowerCase(); - } - - function getCoords(node){ - let rect = node.getBoundingClientRect(); - return [ rect.top, rect.left, rect.right, rect.bottom, cssSelector(node) ]; - } - - function isElementOnScreen(rect){ - let clientHeight = document.documentElement.clientHeight; - let clientWidth = document.documentElement.clientWidth; - return (rect[0] >= 0 && rect[0] <= clientHeight && - rect[1] >= 0 && rect[1] <= clientWidth && - rect[2] != 0 && rect[3] != 0); - } - - function isElementOnTop(element, rect){ - let topElement = document.elementFromPoint((rect[1] + rect[2])/2, (rect[0] + rect[3])/2); - return topElement != undefined && (element.isSameNode(topElement) || element.contains(topElement) || topElement.contains(element)); - } - - function hasCopy(validRects, rect){ - for(let i = 0; i < validRects.length; i++) { - let each = validRects[i]; - if(each[0] === rect[0] && each[1] === rect[1]){ - return true; - } - } - return false; - } - - function addElementToRects(validRects, elements){ - let rect; - for(let i = 0; i < elements.length; i++) { - rect = getCoords(elements[i]); - if(!hasCopy(validRects, rect) && - isElementOnScreen(rect) && - isElementOnTop(elements[i], rect)){ - validRects.push(rect); - } - } - } - - function cAdd1(keyCounter, index, maxDigit){ - if(keyCounter[index] + 1 == maxDigit){ - keyCounter[index] = 0; - cAdd1(keyCounter, index + 1, maxDigit); - } else { - keyCounter[index]++; - } - } - - function generateKeys(markerContainer) { - let lettersString = "%1"; - let letters = lettersString.split(""); - let nodeNum = markerContainer.children.length; - let keyLen = nodeNum == 1 ? 1 : Math.ceil(Math.log(nodeNum)/Math.log(letters.length)); - let keyCounter = []; - for(let i = 0; i < keyLen; i++) keyCounter[i] = 0; - for(let l = 0; l < nodeNum; l++) { - let keyStr = ''; - for(let k = 0; k < keyLen; k++) { - let mark = document.createElement('span'); - mark.setAttribute('class', 'eaf-mark'); - let key = letters[keyCounter[k]]; - mark.textContent = key; - markerContainer.children[l].appendChild(mark); - keyStr += key; - cAdd1(keyCounter, 0, letters.length); - } - markerContainer.children[l].id = keyStr; - } - } - - let style = document.createElement('style'); - document.head.appendChild(style); - style.type = 'text/css'; - style.setAttribute('class', 'eaf-style'); - style.appendChild(document.createTextNode('\ -.eaf-mark {\ -background: none;\ -border: none;\ -bottom: auto;\ -box-shadow: none;\ -color: black !important;\ -cursor: auto;\ -display: inline;\ -float: none;\ -font-size: inherit;\ -font-variant: normal;\ -font-weight: bold;\ -height: auto;\ -left: auto;\ -letter-spacing: 0;\ -line-height: 100%;\ -margin: 0;\ -max-height: none;\ -max-width: none;\ -min-height: 0;\ -min-width: 0;\ -opacity: 1;\ -padding: 0;\ -position: static;\ -right: auto;\ -text-align: left;\ -text-decoration: none;\ -text-indent: 0;\ -text-shadow: none;\ -text-transform: none;\ -top: auto;\ -vertical-align: baseline;\ -white-space: normal;\ -width: auto;\ -z-index: 100000;\ -}')); - - style.appendChild(document.createTextNode('\ -.eaf-marker {\ -position: fixed;\ -display: block;\ -white-space: nowrap;\ -overflow: hidden;\ -font-size: 11.5px;\ -background: linear-gradient(to bottom, #ffdd6e 0%, #deb050 100%);\ -padding-left: 3px;\ -padding-right: 3px;\ -border: 1px solid #c38a22;\ -border-radius: 3px;\ -box-shadow: 0px 3px 7px 0px rgba(0, 0, 0, 0.3);\ -z-index: 100000;\ -}')); - let operationString = "%2"; - let validRects = []; - if (operationString != "select_text") { - addElementToRects(validRects, document.links); // collect links - addElementToRects(validRects, document.querySelectorAll('a')); // collect without href - addElementToRects(validRects, document.querySelectorAll('input')); // collect - addElementToRects(validRects, document.querySelectorAll('button')); // collect