summaryrefslogtreecommitdiff
path: root/ext/fg/js
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg/js')
-rw-r--r--ext/fg/js/document.js4
-rw-r--r--ext/fg/js/float.js57
-rw-r--r--ext/fg/js/frontend.js32
-rw-r--r--ext/fg/js/popup-proxy-host.js40
-rw-r--r--ext/fg/js/popup.js2
-rw-r--r--ext/fg/js/source.js2
6 files changed, 64 insertions, 73 deletions
diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js
index ea9ac965..35861475 100644
--- a/ext/fg/js/document.js
+++ b/ext/fg/js/document.js
@@ -50,7 +50,9 @@ function docImposterCreate(element, isTextarea) {
const imposter = document.createElement('div');
const imposterStyle = imposter.style;
- imposter.innerText = element.value;
+ let value = element.value;
+ if (value.endsWith('\n')) { value += '\n'; }
+ imposter.textContent = value;
for (let i = 0, ii = elementStyle.length; i < ii; ++i) {
const property = elementStyle[i];
diff --git a/ext/fg/js/float.js b/ext/fg/js/float.js
index 8f21a9c5..bc459d23 100644
--- a/ext/fg/js/float.js
+++ b/ext/fg/js/float.js
@@ -33,8 +33,27 @@ class DisplayFloat extends Display {
this._messageToken = null;
this._messageTokenPromise = null;
- yomichan.on('orphaned', () => this.onOrphaned());
- window.addEventListener('message', (e) => this.onMessage(e), false);
+ this._onKeyDownHandlers = new Map([
+ ['C', (e) => {
+ if (e.ctrlKey && !window.getSelection().toString()) {
+ this.onSelectionCopy();
+ return true;
+ }
+ return false;
+ }],
+ ...this._onKeyDownHandlers
+ ]);
+
+ this._windowMessageHandlers = new Map([
+ ['setContent', ({type, details}) => this.setContent(type, details)],
+ ['clearAutoPlayTimer', () => this.clearAutoPlayTimer()],
+ ['setCustomCss', ({css}) => this.setCustomCss(css)],
+ ['prepare', ({options, popupInfo, url, childrenSupported, scale, uniqueId}) => this.prepare(options, popupInfo, url, childrenSupported, scale, uniqueId)],
+ ['setContentScale', ({scale}) => this.setContentScale(scale)]
+ ]);
+
+ yomichan.on('orphaned', this.onOrphaned.bind(this));
+ window.addEventListener('message', this.onMessage.bind(this), false);
}
async prepare(options, popupInfo, url, childrenSupported, scale, uniqueId) {
@@ -96,18 +115,6 @@ class DisplayFloat extends Display {
}
}
- onKeyDown(e) {
- const key = Display.getKeyFromEvent(e);
- const handler = DisplayFloat._onKeyDownHandlers.get(key);
- if (typeof handler === 'function') {
- if (handler(this, e)) {
- e.preventDefault();
- return true;
- }
- }
- return super.onKeyDown(e);
- }
-
async getMessageToken() {
// this._messageTokenPromise is used to ensure that only one call to apiGetMessageToken is made.
if (this._messageTokenPromise === null) {
@@ -126,10 +133,10 @@ class DisplayFloat extends Display {
return;
}
- const handler = DisplayFloat._messageHandlers.get(action);
+ const handler = this._windowMessageHandlers.get(action);
if (typeof handler !== 'function') { return; }
- handler(this, params);
+ handler(params);
}
getOptionsContext() {
@@ -153,22 +160,4 @@ class DisplayFloat extends Display {
}
}
-DisplayFloat._onKeyDownHandlers = new Map([
- ['C', (self, e) => {
- if (e.ctrlKey && !window.getSelection().toString()) {
- self.onSelectionCopy();
- return true;
- }
- return false;
- }]
-]);
-
-DisplayFloat._messageHandlers = new Map([
- ['setContent', (self, {type, details}) => self.setContent(type, details)],
- ['clearAutoPlayTimer', (self) => self.clearAutoPlayTimer()],
- ['setCustomCss', (self, {css}) => self.setCustomCss(css)],
- ['prepare', (self, {options, popupInfo, url, childrenSupported, scale, uniqueId}) => self.prepare(options, popupInfo, url, childrenSupported, scale, uniqueId)],
- ['setContentScale', (self, {scale}) => self.setContentScale(scale)]
-]);
-
DisplayFloat.instance = new DisplayFloat();
diff --git a/ext/fg/js/frontend.js b/ext/fg/js/frontend.js
index 67045241..929ab56a 100644
--- a/ext/fg/js/frontend.js
+++ b/ext/fg/js/frontend.js
@@ -39,6 +39,15 @@ class Frontend extends TextScanner {
this._contentScale = 1.0;
this._orphaned = true;
this._lastShowPromise = Promise.resolve();
+
+ this._windowMessageHandlers = new Map([
+ ['popupClose', () => this.onSearchClear(true)],
+ ['selectionCopy', () => document.execCommand('copy')]
+ ]);
+
+ this._runtimeMessageHandlers = new Map([
+ ['popupSetVisibleOverride', ({visible}) => { this.popup.setVisibleOverride(visible); }]
+ ]);
}
async prepare() {
@@ -55,9 +64,9 @@ class Frontend extends TextScanner {
window.visualViewport.addEventListener('resize', this.onVisualViewportResize.bind(this));
}
- yomichan.on('orphaned', () => this.onOrphaned());
- yomichan.on('optionsUpdated', () => this.updateOptions());
- yomichan.on('zoomChanged', (e) => this.onZoomChanged(e));
+ yomichan.on('orphaned', this.onOrphaned.bind(this));
+ yomichan.on('optionsUpdated', this.updateOptions.bind(this));
+ yomichan.on('zoomChanged', this.onZoomChanged.bind(this));
chrome.runtime.onMessage.addListener(this.onRuntimeMessage.bind(this));
this._updateContentScale();
@@ -72,17 +81,17 @@ class Frontend extends TextScanner {
onWindowMessage(e) {
const action = e.data;
- const handler = Frontend._windowMessageHandlers.get(action);
+ const handler = this._windowMessageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
- handler(this);
+ handler();
}
onRuntimeMessage({action, params}, sender, callback) {
- const handler = Frontend._runtimeMessageHandlers.get(action);
+ const handler = this._runtimeMessageHandlers.get(action);
if (typeof handler !== 'function') { return false; }
- const result = handler(this, params, sender);
+ const result = handler(params, sender);
callback(result);
return false;
}
@@ -237,12 +246,3 @@ class Frontend extends TextScanner {
return visualViewport !== null && typeof visualViewport === 'object' ? visualViewport.scale : 1.0;
}
}
-
-Frontend._windowMessageHandlers = new Map([
- ['popupClose', (self) => self.onSearchClear(true)],
- ['selectionCopy', () => document.execCommand('copy')]
-]);
-
-Frontend._runtimeMessageHandlers = new Map([
- ['popupSetVisibleOverride', (self, {visible}) => { self.popup.setVisibleOverride(visible); }]
-]);
diff --git a/ext/fg/js/popup-proxy-host.js b/ext/fg/js/popup-proxy-host.js
index e55801ff..bef2cb16 100644
--- a/ext/fg/js/popup-proxy-host.js
+++ b/ext/fg/js/popup-proxy-host.js
@@ -34,16 +34,16 @@ class PopupProxyHost {
if (typeof frameId !== 'number') { return; }
this._apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, new Map([
- ['getOrCreatePopup', ({id, parentId}) => this._onApiGetOrCreatePopup(id, parentId)],
- ['setOptions', ({id, options}) => this._onApiSetOptions(id, options)],
- ['hide', ({id, changeFocus}) => this._onApiHide(id, changeFocus)],
- ['isVisible', ({id}) => this._onApiIsVisibleAsync(id)],
- ['setVisibleOverride', ({id, visible}) => this._onApiSetVisibleOverride(id, visible)],
- ['containsPoint', ({id, x, y}) => this._onApiContainsPoint(id, x, y)],
- ['showContent', ({id, elementRect, writingMode, type, details}) => this._onApiShowContent(id, elementRect, writingMode, type, details)],
- ['setCustomCss', ({id, css}) => this._onApiSetCustomCss(id, css)],
- ['clearAutoPlayTimer', ({id}) => this._onApiClearAutoPlayTimer(id)],
- ['setContentScale', ({id, scale}) => this._onApiSetContentScale(id, scale)]
+ ['getOrCreatePopup', this._onApiGetOrCreatePopup.bind(this)],
+ ['setOptions', this._onApiSetOptions.bind(this)],
+ ['hide', this._onApiHide.bind(this)],
+ ['isVisible', this._onApiIsVisibleAsync.bind(this)],
+ ['setVisibleOverride', this._onApiSetVisibleOverride.bind(this)],
+ ['containsPoint', this._onApiContainsPoint.bind(this)],
+ ['showContent', this._onApiShowContent.bind(this)],
+ ['setCustomCss', this._onApiSetCustomCss.bind(this)],
+ ['clearAutoPlayTimer', this._onApiClearAutoPlayTimer.bind(this)],
+ ['setContentScale', this._onApiSetContentScale.bind(this)]
]));
}
@@ -87,56 +87,56 @@ class PopupProxyHost {
// Message handlers
- async _onApiGetOrCreatePopup(id, parentId) {
+ async _onApiGetOrCreatePopup({id, parentId}) {
const popup = this.getOrCreatePopup(id, parentId);
return {
id: popup.id
};
}
- async _onApiSetOptions(id, options) {
+ async _onApiSetOptions({id, options}) {
const popup = this._getPopup(id);
return await popup.setOptions(options);
}
- async _onApiHide(id, changeFocus) {
+ async _onApiHide({id, changeFocus}) {
const popup = this._getPopup(id);
return popup.hide(changeFocus);
}
- async _onApiIsVisibleAsync(id) {
+ async _onApiIsVisibleAsync({id}) {
const popup = this._getPopup(id);
return await popup.isVisible();
}
- async _onApiSetVisibleOverride(id, visible) {
+ async _onApiSetVisibleOverride({id, visible}) {
const popup = this._getPopup(id);
return await popup.setVisibleOverride(visible);
}
- async _onApiContainsPoint(id, x, y) {
+ async _onApiContainsPoint({id, x, y}) {
const popup = this._getPopup(id);
return await popup.containsPoint(x, y);
}
- async _onApiShowContent(id, elementRect, writingMode, type, details) {
+ async _onApiShowContent({id, elementRect, writingMode, type, details}) {
const popup = this._getPopup(id);
elementRect = PopupProxyHost._convertJsonRectToDOMRect(popup, elementRect);
if (!PopupProxyHost._popupCanShow(popup)) { return; }
return await popup.showContent(elementRect, writingMode, type, details);
}
- async _onApiSetCustomCss(id, css) {
+ async _onApiSetCustomCss({id, css}) {
const popup = this._getPopup(id);
return popup.setCustomCss(css);
}
- async _onApiClearAutoPlayTimer(id) {
+ async _onApiClearAutoPlayTimer({id}) {
const popup = this._getPopup(id);
return popup.clearAutoPlayTimer();
}
- async _onApiSetContentScale(id, scale) {
+ async _onApiSetContentScale({id, scale}) {
const popup = this._getPopup(id);
return popup.setContentScale(scale);
}
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js
index 4927f4bd..bc40a8c4 100644
--- a/ext/fg/js/popup.js
+++ b/ext/fg/js/popup.js
@@ -260,7 +260,7 @@ class Popup {
'mozfullscreenchange',
'webkitfullscreenchange'
];
- const onFullscreenChanged = () => this._onFullscreenChanged();
+ const onFullscreenChanged = this._onFullscreenChanged.bind(this);
for (const eventName of fullscreenEvents) {
this._fullscreenEventListeners.addEventListener(document, eventName, onFullscreenChanged, false);
}
diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js
index fa785ec4..6dc482bd 100644
--- a/ext/fg/js/source.js
+++ b/ext/fg/js/source.js
@@ -366,7 +366,7 @@ class TextSourceElement {
setEndOffset(length) {
switch (this.element.nodeName.toUpperCase()) {
case 'BUTTON':
- this.content = this.element.innerHTML;
+ this.content = this.element.textContent;
break;
case 'IMG':
this.content = this.element.getAttribute('alt');