simplify frontend disable override

This commit is contained in:
siikamiika 2020-04-11 16:20:12 +03:00
parent d93e3e1a67
commit 5c3641eadb
3 changed files with 17 additions and 19 deletions

View File

@ -80,6 +80,7 @@ async function main() {
}; };
let frontend = null; let frontend = null;
let frontendPreparePromise = null;
const applyOptions = async () => { const applyOptions = async () => {
const optionsContext = {depth: isSearchPage ? 0 : depth, url}; const optionsContext = {depth: isSearchPage ? 0 : depth, url};
@ -99,8 +100,10 @@ async function main() {
if (frontend === null) { if (frontend === null) {
frontend = new Frontend(popup); frontend = new Frontend(popup);
await frontend.prepare(); frontendPreparePromise = frontend.prepare();
await frontendPreparePromise;
} else { } else {
await frontendPreparePromise;
if (isSearchPage) { if (isSearchPage) {
const disabled = !options.scanning.enableOnSearchPage; const disabled = !options.scanning.enableOnSearchPage;
frontend.setDisabledOverride(disabled); frontend.setDisabledOverride(disabled);

View File

@ -30,8 +30,7 @@ class Frontend extends TextScanner {
super( super(
window, window,
() => this.popup.isProxy() ? [] : [this.popup.getContainer()], () => this.popup.isProxy() ? [] : [this.popup.getContainer()],
[(x, y) => this.popup.containsPoint(x, y)], [(x, y) => this.popup.containsPoint(x, y)]
() => this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride
); );
this.popup = popup; this.popup = popup;
@ -138,10 +137,7 @@ class Frontend extends TextScanner {
setDisabledOverride(disabled) { setDisabledOverride(disabled) {
this._disabledOverride = disabled; this._disabledOverride = disabled;
// other cases handed by regular options update this.setEnabled(this.options.general.enable, this._canEnable());
if (disabled && this.enabled) {
this.setEnabled(false);
}
} }
async setPopup(popup) { async setPopup(popup) {
@ -151,7 +147,7 @@ class Frontend extends TextScanner {
} }
async updateOptions() { async updateOptions() {
this.setOptions(await apiOptionsGet(this.getOptionsContext())); this.setOptions(await apiOptionsGet(this.getOptionsContext()), this._canEnable());
const ignoreNodes = ['.scan-disable', '.scan-disable *']; const ignoreNodes = ['.scan-disable', '.scan-disable *'];
if (!this.options.scanning.enableOnPopupExpressions) { if (!this.options.scanning.enableOnPopupExpressions) {
@ -290,6 +286,11 @@ class Frontend extends TextScanner {
}); });
} }
_canEnable() {
if (this.options === null) { return true; } // called by updateOptions for the first time
return this.popup.depth <= this.options.scanning.popupNestingMaxDepth && !this._disabledOverride;
}
async _updatePopupPosition() { async _updatePopupPosition() {
const textSource = this.getCurrentTextSource(); const textSource = this.getCurrentTextSource();
if (textSource !== null && await this.popup.isVisible()) { if (textSource !== null && await this.popup.isVisible()) {

View File

@ -22,13 +22,11 @@
*/ */
class TextScanner { class TextScanner {
constructor(node, ignoreElements, ignorePoints, canEnable=null) { constructor(node, ignoreElements, ignorePoints) {
this.node = node; this.node = node;
this.ignoreElements = ignoreElements; this.ignoreElements = ignoreElements;
this.ignorePoints = ignorePoints; this.ignorePoints = ignorePoints;
this.canEnable = canEnable;
this.ignoreNodes = null; this.ignoreNodes = null;
this.scanTimerPromise = null; this.scanTimerPromise = null;
@ -226,12 +224,8 @@ class TextScanner {
} }
} }
setEnabled(enabled) { setEnabled(enabled, canEnable) {
if (this.canEnable !== null && !this.canEnable()) { if (enabled && canEnable) {
enabled = false;
}
if (enabled) {
if (!this.enabled) { if (!this.enabled) {
this.hookEvents(); this.hookEvents();
this.enabled = true; this.enabled = true;
@ -277,9 +271,9 @@ class TextScanner {
]; ];
} }
setOptions(options) { setOptions(options, canEnable=true) {
this.options = options; this.options = options;
this.setEnabled(this.options.general.enable); this.setEnabled(this.options.general.enable, canEnable);
} }
async searchAt(x, y, cause) { async searchAt(x, y, cause) {