Add middle mouse prevention option (#868)

* Add preventMiddleMouse option to TextScanner

* Add preventMiddleMouse options

* Add options

* Apply preventMiddleMouse options
This commit is contained in:
toasted-nutbread 2020-09-26 19:24:21 -04:00 committed by GitHub
parent 9c7b9d6660
commit 73dd578821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 6 deletions

View File

@ -320,6 +320,7 @@
"type": "object", "type": "object",
"required": [ "required": [
"inputs", "inputs",
"preventMiddleMouse",
"touchInputEnabled", "touchInputEnabled",
"pointerEventsEnabled", "pointerEventsEnabled",
"selectText", "selectText",
@ -454,6 +455,33 @@
} }
} }
}, },
"preventMiddleMouse": {
"type": "object",
"required": [
"onWebPages",
"onPopupPages",
"onSearchPages",
"onSearchQuery"
],
"properties": {
"onWebPages": {
"type": "boolean",
"default": false
},
"onPopupPages": {
"type": "boolean",
"default": false
},
"onSearchPages": {
"type": "boolean",
"default": false
},
"onSearchQuery": {
"type": "boolean",
"default": false
}
}
},
"touchInputEnabled": { "touchInputEnabled": {
"type": "boolean", "type": "boolean",
"default": true "default": true

View File

@ -507,6 +507,7 @@ class OptionsUtil {
// Added hideDelay. // Added hideDelay.
// Added inputs to profileOptions.scanning. // Added inputs to profileOptions.scanning.
// Added pointerEventsEnabled to profileOptions.scanning. // Added pointerEventsEnabled to profileOptions.scanning.
// Added preventMiddleMouse to profileOptions.scanning.
for (const {conditionGroups} of options.profiles) { for (const {conditionGroups} of options.profiles) {
for (const {conditions} of conditionGroups) { for (const {conditions} of conditionGroups) {
for (const condition of conditions) { for (const condition of conditions) {
@ -531,6 +532,12 @@ class OptionsUtil {
profileOptions.general.usePopupWindow = false; profileOptions.general.usePopupWindow = false;
profileOptions.scanning.hideDelay = 0; profileOptions.scanning.hideDelay = 0;
profileOptions.scanning.pointerEventsEnabled = false; profileOptions.scanning.pointerEventsEnabled = false;
profileOptions.scanning.preventMiddleMouse = {
onWebPages: false,
onPopupPages: false,
onSearchPages: false,
onSearchQuery: false
};
const {modifier, middleMouse} = profileOptions.scanning; const {modifier, middleMouse} = profileOptions.scanning;
delete profileOptions.scanning.modifier; delete profileOptions.scanning.modifier;

View File

@ -414,6 +414,16 @@
<label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.deepDomScan"> Deep content scan</label> <label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.deepDomScan"> Deep content scan</label>
</div> </div>
<div class="checkbox options-advanced">
<strong>Prevent middle mouse button actions on:</strong>
<div style="margin-left: 1em;">
<label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.preventMiddleMouse.onWebPages"> Webpages</label><br>
<label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.preventMiddleMouse.onPopupPages"> Popups</label><br>
<label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.preventMiddleMouse.onSearchPages"> Search page</label><br>
<label><input type="checkbox" id="deep-dom-scan" data-setting="scanning.preventMiddleMouse.onSearchQuery"> Search query</label><br>
</div>
</div>
<div class="form-group options-advanced"> <div class="form-group options-advanced">
<div class="row"> <div class="row">
<div class="col-xs-6"> <div class="col-xs-6">

View File

@ -320,6 +320,7 @@ class Frontend {
await this._updatePopup(); await this._updatePopup();
const preventMiddleMouse = this._getPreventMiddleMouseValueForPageType(scanningOptions.preventMiddleMouse);
this._textScanner.setOptions({ this._textScanner.setOptions({
inputs: scanningOptions.inputs, inputs: scanningOptions.inputs,
deepContentScan: scanningOptions.deepDomScan, deepContentScan: scanningOptions.deepDomScan,
@ -329,7 +330,8 @@ class Frontend {
pointerEventsEnabled: scanningOptions.pointerEventsEnabled, pointerEventsEnabled: scanningOptions.pointerEventsEnabled,
scanLength: scanningOptions.length, scanLength: scanningOptions.length,
sentenceExtent: options.anki.sentenceExt, sentenceExtent: options.anki.sentenceExt,
layoutAwareScan: scanningOptions.layoutAwareScan layoutAwareScan: scanningOptions.layoutAwareScan,
preventMiddleMouse
}); });
this._updateTextScannerEnabled(); this._updateTextScannerEnabled();
@ -616,4 +618,13 @@ class Frontend {
await this._updatePendingOptions(); await this._updatePendingOptions();
return await this.getOptionsContext(); return await this.getOptionsContext();
} }
_getPreventMiddleMouseValueForPageType(preventMiddleMouseOptions) {
switch (this._pageType) {
case 'web': return preventMiddleMouseOptions.onWebPages;
case 'popup': return preventMiddleMouseOptions.onPopupPages;
case 'search': return preventMiddleMouseOptions.onSearchPages;
default: return false;
}
}
} }

View File

@ -251,7 +251,8 @@ class Display extends EventDispatcher {
pointerEventsEnabled: scanning.pointerEventsEnabled, pointerEventsEnabled: scanning.pointerEventsEnabled,
scanLength: scanning.length, scanLength: scanning.length,
sentenceExtent: options.anki.sentenceExt, sentenceExtent: options.anki.sentenceExt,
layoutAwareScan: scanning.layoutAwareScan layoutAwareScan: scanning.layoutAwareScan,
preventMiddleMouse: scanning.preventMiddleMouse.onSearchQuery
} }
}); });
} }

View File

@ -49,6 +49,7 @@ class TextScanner extends EventDispatcher {
this._scanLength = 1; this._scanLength = 1;
this._sentenceExtent = 1; this._sentenceExtent = 1;
this._layoutAwareScan = false; this._layoutAwareScan = false;
this._preventMiddleMouse = false;
this._inputs = []; this._inputs = [];
this._enabled = false; this._enabled = false;
@ -113,7 +114,7 @@ class TextScanner extends EventDispatcher {
} }
} }
setOptions({inputs, deepContentScan, selectText, delay, touchInputEnabled, pointerEventsEnabled, scanLength, sentenceExtent, layoutAwareScan}) { setOptions({inputs, deepContentScan, selectText, delay, touchInputEnabled, pointerEventsEnabled, scanLength, sentenceExtent, layoutAwareScan, preventMiddleMouse}) {
if (Array.isArray(inputs)) { if (Array.isArray(inputs)) {
this._inputs = inputs.map(({ this._inputs = inputs.map(({
include, include,
@ -151,6 +152,9 @@ class TextScanner extends EventDispatcher {
if (typeof layoutAwareScan === 'boolean') { if (typeof layoutAwareScan === 'boolean') {
this._layoutAwareScan = layoutAwareScan; this._layoutAwareScan = layoutAwareScan;
} }
if (typeof preventMiddleMouse === 'boolean') {
this._preventMiddleMouse = preventMiddleMouse;
}
} }
getTextSourceContent(textSource, length, layoutAwareScan) { getTextSourceContent(textSource, length, layoutAwareScan) {
@ -282,9 +286,18 @@ class TextScanner extends EventDispatcher {
return false; return false;
} }
if (e.button === 0) { // Primary switch (e.button) {
case 0: // Primary
this._scanTimerClear(); this._scanTimerClear();
this.clearSelection(false); this.clearSelection(false);
break;
case 1: // Middle
if (this._preventMiddleMouse) {
e.preventDefault();
e.stopPropagation();
return false;
}
break;
} }
} }

View File

@ -316,6 +316,12 @@ function createProfileOptionsUpdatedTestData1() {
layoutAwareScan: false, layoutAwareScan: false,
hideDelay: 0, hideDelay: 0,
pointerEventsEnabled: false, pointerEventsEnabled: false,
preventMiddleMouse: {
onWebPages: false,
onPopupPages: false,
onSearchPages: false,
onSearchQuery: false
},
inputs: [ inputs: [
{ {
include: 'shift', include: 'shift',