-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreading-line.js
executable file
·110 lines (100 loc) · 3.37 KB
/
reading-line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var ReadingLine = {
active: false,
hidden: false,
div: null,
init: function () {
// Cleanup existing div just in case.
var div = document.getElementById("ReadingLine");
if (div) {
document.body.removeChild(div);
}
this.div = document.createElement("div");
this.div.id = "ReadingLine";
this.div.style.top = "-100px";
this.mouseMove = this.mouseMove.bind(this);
this.mouseOut = this.mouseOut.bind(this);
this.shortCut = this.shortCut.bind(this);
this.checkStatus = this.checkStatus.bind(this);
document.addEventListener("keydown", this.shortCut, false);
this.checkStatus();
// The interval will make sure the content script will get disabled on
// extension uninstall.
setInterval(this.checkStatus, 5000);
},
checkStatus: function(){
try {
chrome.runtime.sendMessage("ReadingLine-status", function (status) {
if (status) {
this.enable();
}
else {
this.disable();
}
}.bind(this));
}
catch (e) {
}
},
enable: function () {
if (this.active) return;
var div = document.getElementById("ReadingLine");
if (div === null) {
document.body.appendChild(this.div);
}
document.addEventListener("mousedown", this.mouseMove);
document.addEventListener("mouseup", this.mouseMove);
document.addEventListener("mousemove", this.mouseMove);
document.addEventListener("mouseover", this.mouseMove);
document.addEventListener("mouseout", this.mouseOut);
this.active = true;
},
disable: function () {
if (!this.active) return;
var div = document.getElementById("ReadingLine");
if (div) {
document.body.removeChild(div);
}
document.removeEventListener("mousedown", this.mouseMove);
document.removeEventListener("mouseup", this.mouseMove);
document.removeEventListener("mousemove", this.mouseMove);
document.removeEventListener("mouseover", this.mouseMove);
document.removeEventListener("mouseout", this.mouseOut);
this.active = false;
},
mouseMove: function (e) {
this.div.className = "active";
this.div.style.top = (e.clientY) + "px";
this.hidden = false;
},
mouseOut: function (e) {
if (e.toElement == null && e.relatedTarget == null) {
this.div.className = "hidden";
this.hidden = true;
}
},
shortCut: function (e) {
// Ctrl/Cmd + Alt + -
if ((e.ctrlKey || e.metaKey) && e.altKey && e.keyCode === 189) {
if (!this.active) {
this.enable();
chrome.runtime.sendMessage("ReadingLine-enable");
}
else {
this.disable();
chrome.runtime.sendMessage("ReadingLine-disable");
}
}
}
};
ReadingLine.init();
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
switch (msg.action) {
case "ReadingLine-enable":
ReadingLine.enable();
break;
case "ReadingLine-disable":
ReadingLine.disable();
break;
}
sendResponse(msg.data, true);
});