@@ -25,95 +25,96 @@ export function exactMatchPattern(string) {
2525 }
2626}
2727
28- function TargetSelector ( callback , cleanupCallback ) {
29- this . callback = callback ;
30- this . cleanupCallback = cleanupCallback ;
28+ class TargetSelector {
29+ constructor ( callback , cleanupCallback ) {
30+ this . callback = callback ;
31+ this . cleanupCallback = cleanupCallback ;
32+ // This is for XPCOM/XUL addon and can't be used
33+ //var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
34+ //this.win = wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
3135
32- // This is for XPCOM/XUL addon and can't be used
33- //var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
34- //this.win = wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
35-
36- // Instead, we simply assign global content window to this.win
37- this . win = window ;
38- const doc = this . win . document ;
39- const div = doc . createElement ( "div" ) ;
40- div . setAttribute ( "style" , "display: none;" ) ;
41- doc . body . insertBefore ( div , doc . body . firstChild ) ;
42- this . div = div ;
43- this . e = null ;
44- this . r = null ;
45- doc . addEventListener ( "mousemove" , this , true ) ;
46- doc . addEventListener ( "click" , this , true ) ;
47- }
36+ // Instead, we simply assign global content window to this.win
37+ this . win = window ;
38+ const doc = this . win . document ;
39+ const div = doc . createElement ( "div" ) ;
40+ div . setAttribute ( "style" , "display: none;" ) ;
41+ doc . body . insertBefore ( div , doc . body . firstChild ) ;
42+ this . div = div ;
43+ this . e = null ;
44+ this . r = null ;
45+ doc . addEventListener ( "mousemove" , this , true ) ;
46+ doc . addEventListener ( "click" , this , true ) ;
47+ }
4848
49- TargetSelector . prototype . cleanup = function ( ) {
50- try {
51- if ( this . div ) {
52- if ( this . div . parentNode ) {
53- this . div . parentNode . removeChild ( this . div ) ;
49+ cleanup ( ) {
50+ try {
51+ if ( this . div ) {
52+ if ( this . div . parentNode ) {
53+ this . div . parentNode . removeChild ( this . div ) ;
54+ }
55+ this . div = null ;
56+ }
57+ if ( this . win ) {
58+ const doc = this . win . document ;
59+ doc . removeEventListener ( "mousemove" , this , true ) ;
60+ doc . removeEventListener ( "click" , this , true ) ;
61+ }
62+ } catch ( e ) {
63+ if ( e != "TypeError: can't access dead object" ) {
64+ throw e ;
5465 }
55- this . div = null ;
56- }
57- if ( this . win ) {
58- const doc = this . win . document ;
59- doc . removeEventListener ( "mousemove" , this , true ) ;
60- doc . removeEventListener ( "click" , this , true ) ;
6166 }
62- } catch ( e ) {
63- if ( e != "TypeError: can't access dead object" ) {
64- throw e ;
67+ this . win = null ;
68+ if ( this . cleanupCallback ) {
69+ this . cleanupCallback ( ) ;
6570 }
6671 }
67- this . win = null ;
68- if ( this . cleanupCallback ) {
69- this . cleanupCallback ( ) ;
70- }
71- } ;
7272
73- TargetSelector . prototype . handleEvent = function ( evt ) {
74- switch ( evt . type ) {
75- case "mousemove" :
76- this . highlight ( evt . target . ownerDocument , evt . clientX , evt . clientY ) ;
77- break ;
78- case "click" :
79- if ( evt . button == 0 && this . e && this . callback ) {
80- this . callback ( this . e , this . win ) ;
81- } //Right click would cancel the select
82- evt . preventDefault ( ) ;
83- evt . stopPropagation ( ) ;
84- this . cleanup ( ) ;
85- break ;
73+ handleEvent ( evt ) {
74+ switch ( evt . type ) {
75+ case "mousemove" :
76+ this . highlight ( evt . target . ownerDocument , evt . clientX , evt . clientY ) ;
77+ break ;
78+ case "click" :
79+ if ( evt . button == 0 && this . e && this . callback ) {
80+ this . callback ( this . e , this . win ) ;
81+ } //Right click would cancel the select
82+ evt . preventDefault ( ) ;
83+ evt . stopPropagation ( ) ;
84+ this . cleanup ( ) ;
85+ break ;
86+ }
8687 }
87- } ;
8888
89- TargetSelector . prototype . highlight = function ( doc , x , y ) {
90- if ( doc ) {
91- const e = doc . elementFromPoint ( x , y ) ;
92- if ( e && e != this . e ) {
93- this . highlightElement ( e ) ;
89+ highlight ( doc , x , y ) {
90+ if ( doc ) {
91+ const e = doc . elementFromPoint ( x , y ) ;
92+ if ( e && e != this . e ) {
93+ this . highlightElement ( e ) ;
94+ }
9495 }
9596 }
96- } ;
9797
98- TargetSelector . prototype . highlightElement = function ( element ) {
99- if ( element && element != this . e ) {
100- this . e = element ;
101- } else {
102- return ;
103- }
104- const r = element . getBoundingClientRect ( ) ;
105- const or = this . r ;
106- if ( r . left >= 0 && r . top >= 0 && r . width > 0 && r . height > 0 ) {
107- if ( or && r . top == or . top && r . left == or . left && r . width == or . width && r . height == or . height ) {
98+ highlightElement ( element ) {
99+ if ( element && element != this . e ) {
100+ this . e = element ;
101+ } else {
108102 return ;
109103 }
110- this . r = r ;
111- const style = "pointer-events: none; position: absolute; background-color: rgb(78, 171, 230); opacity: 0.4; border: 1px solid #0e0e0e; z-index: 100;" ;
112- const pos = "top:" + ( r . top + this . win . scrollY ) + "px; left:" + ( r . left + this . win . scrollX ) + "px; width:" + r . width + "px; height:" + r . height + "px;" ;
113- this . div . setAttribute ( "style" , style + pos ) ;
114- } else if ( or ) {
115- this . div . setAttribute ( "style" , "display: none;" ) ;
104+ const r = element . getBoundingClientRect ( ) ;
105+ const or = this . r ;
106+ if ( r . left >= 0 && r . top >= 0 && r . width > 0 && r . height > 0 ) {
107+ if ( or && r . top == or . top && r . left == or . left && r . width == or . width && r . height == or . height ) {
108+ return ;
109+ }
110+ this . r = r ;
111+ const style = "pointer-events: none; position: absolute; background-color: rgb(78, 171, 230); opacity: 0.4; border: 1px solid #0e0e0e; z-index: 100;" ;
112+ const pos = `top:${ r . top + this . win . scrollY } px; left:${ r . left + this . win . scrollX } px; width:${ r . width } px; height:${ r . height } px;` ;
113+ this . div . setAttribute ( "style" , style + pos ) ;
114+ } else if ( or ) {
115+ this . div . setAttribute ( "style" , "display: none;" ) ;
116+ }
116117 }
117- } ;
118+ }
118119
119120export default TargetSelector ;
0 commit comments