/
pass-selected-text-to-background.jsx
184 lines (150 loc) · 6.72 KB
/
pass-selected-text-to-background.jsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
This file is part of Talkie -- text-to-speech browser extension button.
<https://joelpurra.com/projects/talkie/>
Copyright (c) 2016, 2017, 2018, 2019 Joel Purra <https://joelpurra.com/>
Talkie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Talkie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Talkie. If not, see <https://www.gnu.org/licenses/>.
*/
import {
promiseTry,
} from "../../shared/promise";
import React from "react";
import PropTypes from "prop-types";
import {
knownEvents,
} from "../../shared/events";
//import DualLogger from "../frontend/dual-log";
//
//const dualLogger = new DualLogger("status-container.jsx");
export default function passSelectedTextToBackgroundHoc(ComponentToWrap) {
return class PassSelectedTextToBackgroundHoc extends React.Component {
constructor(props) {
super(props);
this.componentCleanup = this.componentCleanup.bind(this);
this.handleFocus = this.handleFocus.bind(this);
this.enable = this.enable.bind(this);
this.disable = this.disable.bind(this);
this.getSelectedTextWithFocusTimestamp = this.getSelectedTextWithFocusTimestamp.bind(this);
this.isListeningToBroadcasts = false;
this.killSwitches = [];
this.mostRecentUse = 0;
}
static contextTypes ={
broadcaster: PropTypes.object.isRequired,
}
componentDidMount() {
window.addEventListener("beforeunload", this.componentCleanup);
window.addEventListener("focus", this.handleFocus);
this.gotFocus();
this.enable();
}
componentWillUnmount() {
this.componentCleanup();
}
shouldComponentUpdate(nextProps, nextState) {
// NOTE: always update.
// TODO: optimize by comparing old and new props/state.
return this.isListeningToBroadcasts;
}
componentCleanup() {
window.removeEventListener("beforeunload", this.componentCleanup);
window.removeEventListener("focus", this.handleFocus);
this.disable();
}
handleFocus() {
this.gotFocus();
}
gotFocus() {
this.mostRecentUse = Date.now();
}
enable() {
// TODO: properly avoid race conditions when enabling/disabling.
if (this.isListeningToBroadcasts) {
return;
}
this.registerBroadcastListeners();
this.isListeningToBroadcasts = true;
}
disable() {
// TODO: properly avoid race conditions when enabling/disabling.
if (!this.isListeningToBroadcasts) {
return;
}
this.isListeningToBroadcasts = false;
this.executeKillSwitches();
}
render() {
return (
<ComponentToWrap
{...this.props}
/>
);
}
getSelectedTextWithFocusTimestamp() {
if (!this.isListeningToBroadcasts) {
return null;
}
// NOTE: duplicated elsewhere in the codebase.
/* eslint-disable no-inner-declarations */
const executeGetFramesSelectionTextAndLanguageCode = (function() {
try {
function talkieGetParentElementLanguages(element) {
return []
.concat((element || null) && element.getAttribute && element.getAttribute("lang"))
.concat((element || null) && element.parentElement && talkieGetParentElementLanguages(element.parentElement));
};
const talkieSelectionData = {
text: ((document || null) && (document.getSelection || null) && (document.getSelection() || null) && document.getSelection().toString()),
htmlTagLanguage: ((document || null) && (document.getElementsByTagName || null) && (document.getElementsByTagName("html") || null) && (document.getElementsByTagName("html").length > 0 || null) && (document.getElementsByTagName("html")[0].getAttribute("lang") || null)),
parentElementsLanguages: (talkieGetParentElementLanguages((document || null) && (document.getSelection || null) && (document.getSelection() || null) && (document.getSelection().rangeCount > 0 || null) && (document.getSelection().getRangeAt || null) && (document.getSelection().getRangeAt(0) || null) && (document.getSelection().getRangeAt(0).startContainer || null))),
};
return talkieSelectionData;
} catch (error) {
return null;
}
}());
/* eslint-enable no-inner-declarations */
const selectedTextWithFocusTimestamp = {
mostRecentUse: this.mostRecentUse,
selectionTextAndLanguageCode: executeGetFramesSelectionTextAndLanguageCode,
};
return selectedTextWithFocusTimestamp;
}
executeKillSwitches() {
// NOTE: expected to have only synchronous methods for the relevant parts.
const killSwitchesToExecute = this.killSwitches;
this.killSwitches = [];
killSwitchesToExecute.forEach((killSwitch) => {
try {
killSwitch();
} catch (error) {
try {
// dualLogger.dualLogError("executeKillSwitches", error);
} catch (ignored) {
// NOTE: ignoring error logging errors.
}
}
});
};
registerBroadcastListeners() {
return promiseTry(
() => {
return Promise.all([
/* eslint-disable no-unused-vars */
this.context.broadcaster.registerListeningAction(knownEvents.passSelectedTextToBackground, (actionName, actionData) => this.getSelectedTextWithFocusTimestamp(actionName, actionData))
.then((killSwitch) => this.killSwitches.push(killSwitch)),
/* eslint-enable no-unused-vars */
]);
}
);
}
};
};