1- chrome . extension . sendMessage ( { } , function ( response ) {
2- var readyStateCheckInterval = setInterval ( function ( ) {
3- if ( document . readyState === "complete" ) {
4- clearInterval ( readyStateCheckInterval ) ;
5-
6- // Check Page
7- checkPage ( ) ;
8-
9- // Check page again when any input field is changed
10- const inputs = document . querySelectorAll ( 'input' ) ;
11- [ ...inputs ] . forEach ( function ( input ) {
12- input . addEventListener ( 'change' , checkPage ) ;
13- } ) ;
1+ ( function ( ) {
2+ let elementsWithZLCC = [ ] ;
143
15- }
16- } , 10 ) ;
4+ const checkPage = function ( ) {
5+ const allElements = document . getElementsByTagName ( '*' ) ;
6+ [ ...allElements ] . forEach ( checkElement ) ;
7+ }
178
18- } ) ;
9+ // From: https://jsfiddle.net/tim333/np874wae/13/
10+ const checkElement = function ( element ) {
11+ const text = textWithoutChildren ( element ) ;
1912
20- const checkPage = function ( ) {
21- const allElements = document . getElementsByTagName ( '*' ) ;
22- [ ...allElements ] . forEach ( checkElement ) ;
23- }
13+ [ ...text ] . forEach ( function ( character ) {
14+ unicodeCode = character . codePointAt ( 0 ) ;
2415
25- // From: https://jsfiddle.net/tim333/np874wae/13/
26- const checkElement = function ( element ) {
27- const text = textWithoutChildren ( element ) ;
28- const zeroLengthCharacterCodes = [ 8203 , 8204 , 8205 , 8288 ] ;
16+ if (
17+ zeroLengthCharacterCodes . includes ( unicodeCode )
18+ && ! elementsWithZLCC . includes ( element )
19+ ) {
20+ elementsWithZLCC . push ( element )
21+ }
22+ } ) ;
2923
30- [ ...text ] . forEach ( function ( character ) {
31- unicodeCode = character . codePointAt ( 0 ) ;
32- if ( zeroLengthCharacterCodes . includes ( unicodeCode ) ) {
24+ elementsWithZLCC . forEach ( function ( element ) {
3325 element . classList . add ( 'zero-length-characters' ) ;
26+ } )
27+ }
28+
29+ // From: https://stackoverflow.com/a/9340862/535363
30+ const textWithoutChildren = function ( element ) {
31+ let child = element . firstChild ,
32+ texts = [ ] ;
33+
34+ while ( child ) {
35+ if ( child . nodeType == 3 ) {
36+ texts . push ( child . data ) ;
37+ }
38+ child = child . nextSibling ;
3439 }
40+
41+ return texts . join ( "" ) ;
42+ }
43+
44+ chrome . extension . sendMessage ( { } , function ( response ) {
45+ var readyStateCheckInterval = setInterval ( function ( ) {
46+ if ( document . readyState === "complete" ) {
47+ clearInterval ( readyStateCheckInterval ) ;
48+
49+ // Check Page
50+ checkPage ( ) ;
51+
52+ // Check page again when any input field is changed
53+ const inputs = document . querySelectorAll ( 'input' ) ;
54+
55+ [ ...inputs ] . forEach ( function ( input ) {
56+ input . addEventListener ( 'change' , checkPage ) ;
57+ } ) ;
58+ }
59+ } , 10 ) ;
3560 } ) ;
36- }
3761
38- // From: https://stackoverflow.com/a/9340862/535363
39- const textWithoutChildren = function ( element ) {
40- child = element . firstChild ,
41- texts = [ ] ;
62+ document . body . addEventListener ( 'mouseup' , function ( e ) {
63+ const selection = window . getSelection ( ) ;
64+
65+ const shouldSanitizeSelection = elementsWithZLCC
66+ . map ( function ( element ) {
67+ return selection . containsNode ( element , true ) ;
68+ } )
69+ . includes ( true ) ;
4270
43- while ( child ) {
44- if ( child . nodeType == 3 ) {
45- texts . push ( child . data ) ;
71+ try {
72+ chrome . runtime . sendMessage ( {
73+ "shouldSanitizeSelection" : shouldSanitizeSelection ,
74+ "selection" : selection . toString ( )
75+ } ) ;
76+ } catch ( e ) {
77+ if (
78+ e . message . match ( / I n v o c a t i o n o f f o r m r u n t i m e \. c o n n e c t / ) &&
79+ e . message . match ( / d o e s n ' t m a t c h d e f i n i t i o n r u n t i m e \. c o n n e c t / )
80+ ) {
81+ console . error ( 'Chrome extension has been reloaded. Please refresh the page' ) ;
82+ } else {
83+ throw ( e ) ;
4684 }
47- child = child . nextSibling ;
48- }
85+ }
86+ } ) ;
4987
50- return texts . join ( "" ) ;
51- }
88+ } ) ( ) ;
0 commit comments