Skip to content

Commit 84aacac

Browse files
committed
rewrite targetselector as class
1 parent 3fdecee commit 84aacac

File tree

1 file changed

+76
-74
lines changed

1 file changed

+76
-74
lines changed

packages/selenium-ide/src/content/targetSelector.js

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,95 +17,97 @@
1717

1818
// Modified in tools.js from selenium-IDE
1919

20-
function TargetSelector(callback, cleanupCallback) {
21-
this.callback = callback;
22-
this.cleanupCallback = cleanupCallback;
20+
class TargetSelector {
21+
constructor(callback, cleanupCallback) {
22+
this.callback = callback;
23+
this.cleanupCallback = cleanupCallback;
2324

24-
// This is for XPCOM/XUL addon and can't be used
25-
//var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
26-
//this.win = wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
25+
// This is for XPCOM/XUL addon and can't be used
26+
//var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
27+
//this.win = wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
2728

28-
// Instead, we simply assign global content window to this.win
29-
this.win = window;
30-
const doc = this.win.document;
31-
const div = doc.createElement("div");
32-
div.setAttribute("style", "display: none;");
33-
doc.body.insertBefore(div, doc.body.firstChild);
34-
this.div = div;
35-
this.e = null;
36-
this.r = null;
37-
doc.addEventListener("mousemove", this, true);
38-
doc.addEventListener("click", this, true);
39-
}
29+
// Instead, we simply assign global content window to this.win
30+
this.win = window;
31+
const doc = this.win.document;
32+
const div = doc.createElement("div");
33+
div.setAttribute("style", "display: none;");
34+
doc.body.insertBefore(div, doc.body.firstChild);
35+
this.div = div;
36+
this.e = null;
37+
this.r = null;
38+
doc.addEventListener("mousemove", this, true);
39+
doc.addEventListener("click", this, true);
40+
}
4041

41-
TargetSelector.prototype.cleanup = function () {
42-
try {
43-
if (this.div) {
44-
if (this.div.parentNode) {
45-
this.div.parentNode.removeChild(this.div);
42+
cleanup() {
43+
try {
44+
if (this.div) {
45+
if (this.div.parentNode) {
46+
this.div.parentNode.removeChild(this.div);
47+
}
48+
this.div = null;
49+
}
50+
if (this.win) {
51+
const doc = this.win.document;
52+
doc.removeEventListener("mousemove", this, true);
53+
doc.removeEventListener("click", this, true);
54+
}
55+
} catch (e) {
56+
if (e != "TypeError: can't access dead object") {
57+
throw e;
4658
}
47-
this.div = null;
48-
}
49-
if (this.win) {
50-
const doc = this.win.document;
51-
doc.removeEventListener("mousemove", this, true);
52-
doc.removeEventListener("click", this, true);
5359
}
54-
} catch (e) {
55-
if (e != "TypeError: can't access dead object") {
56-
throw e;
60+
this.win = null;
61+
if (this.cleanupCallback) {
62+
this.cleanupCallback();
5763
}
5864
}
59-
this.win = null;
60-
if (this.cleanupCallback) {
61-
this.cleanupCallback();
62-
}
63-
};
6465

65-
TargetSelector.prototype.handleEvent = function (evt) {
66-
switch (evt.type) {
67-
case "mousemove":
68-
this.highlight(evt.target.ownerDocument, evt.clientX, evt.clientY);
69-
break;
70-
case "click":
71-
if (evt.button == 0 && this.e && this.callback) {
72-
this.callback(this.e, this.win);
73-
} //Right click would cancel the select
74-
evt.preventDefault();
75-
evt.stopPropagation();
76-
this.cleanup();
77-
break;
66+
handleEvent(evt) {
67+
switch (evt.type) {
68+
case "mousemove":
69+
this.highlight(evt.target.ownerDocument, evt.clientX, evt.clientY);
70+
break;
71+
case "click":
72+
if (evt.button == 0 && this.e && this.callback) {
73+
this.callback(this.e, this.win);
74+
} //Right click would cancel the select
75+
evt.preventDefault();
76+
evt.stopPropagation();
77+
this.cleanup();
78+
break;
79+
}
7880
}
79-
};
8081

81-
TargetSelector.prototype.highlight = function (doc, x, y) {
82-
if (doc) {
83-
const e = doc.elementFromPoint(x, y);
84-
if (e && e != this.e) {
85-
this.highlightElement(e);
82+
highlight(doc, x, y) {
83+
if (doc) {
84+
const e = doc.elementFromPoint(x, y);
85+
if (e && e != this.e) {
86+
this.highlightElement(e);
87+
}
8688
}
8789
}
88-
};
8990

90-
TargetSelector.prototype.highlightElement = function (element) {
91-
if (element && element != this.e) {
92-
this.e = element;
93-
} else {
94-
return;
95-
}
96-
const r = element.getBoundingClientRect();
97-
const or = this.r;
98-
if (r.left >= 0 && r.top >= 0 && r.width > 0 && r.height > 0) {
99-
if (or && r.top == or.top && r.left == or.left && r.width == or.width && r.height == or.height) {
91+
highlightElement(element) {
92+
if (element && element != this.e) {
93+
this.e = element;
94+
} else {
10095
return;
10196
}
102-
this.r = r;
103-
const style = "pointer-events: none; position: absolute; background-color: rgb(78, 171, 230); opacity: 0.4; border: 1px solid #0e0e0e; z-index: 100;";
104-
const pos = "top:" + (r.top + this.win.scrollY) + "px; left:" + (r.left + this.win.scrollX) + "px; width:" + r.width + "px; height:" + r.height + "px;";
105-
this.div.setAttribute("style", style + pos);
106-
} else if (or) {
107-
this.div.setAttribute("style", "display: none;");
97+
const r = element.getBoundingClientRect();
98+
const or = this.r;
99+
if (r.left >= 0 && r.top >= 0 && r.width > 0 && r.height > 0) {
100+
if (or && r.top == or.top && r.left == or.left && r.width == or.width && r.height == or.height) {
101+
return;
102+
}
103+
this.r = r;
104+
const style = "pointer-events: none; position: absolute; background-color: rgb(78, 171, 230); opacity: 0.4; border: 1px solid #0e0e0e; z-index: 100;";
105+
const pos = `top:${r.top + this.win.scrollY}px; left:${r.left + this.win.scrollX}px; width:${r.width}px; height:${r.height}px;`;
106+
this.div.setAttribute("style", style + pos);
107+
} else if (or) {
108+
this.div.setAttribute("style", "display: none;");
109+
}
108110
}
109-
};
111+
}
110112

111113
export default TargetSelector;

0 commit comments

Comments
 (0)