From aaeeb38ba226b65fea4d598be316854ca2777bbe Mon Sep 17 00:00:00 2001 From: Justin Young Date: Tue, 25 Oct 2016 02:06:45 +0800 Subject: [PATCH] fixed compatibility problem --- src/script/options.js | 246 +++++++++++++++++++++--------------------- 1 file changed, 123 insertions(+), 123 deletions(-) diff --git a/src/script/options.js b/src/script/options.js index 6f625aa..f27186c 100644 --- a/src/script/options.js +++ b/src/script/options.js @@ -5,85 +5,85 @@ import '../style/options.scss'; import Youdao from './util/youdao'; const KEY_CODE = { - BACKSPACE: 8, - CTRL: 17, - SHIFT: 16, - ALT: 18, - CMD: 91 + BACKSPACE: 8, + CTRL: 17, + SHIFT: 16, + ALT: 18, + CMD: 91 }; class Options { - constructor() { - this.options = document.querySelectorAll('input[type=checkbox]'); - this.dblclick = document.querySelector('#dblclick'); - this.shortcut = document.querySelector('#shortcut'); - this.shortcut1 = document.querySelector('#shortcut1'); - this.shortcut2 = document.querySelector('#shortcut2'); - this.selectToTranslate = document.querySelector('#selectToTranslate'); - this.wordbook = document.querySelector('#wordbook'); - this.usePersonalKey = document.querySelector('#usePersonalKey'); - this.keyfrom = document.querySelector('input#keyfrom'); - this.key = document.querySelector('input#key'); - - chrome.storage.sync.get((items) => { - this.dblclick.checked = items.dblclick; - this.shortcut.checked = items.shortcut; - this.selectToTranslate.checked = items.selectToTranslate; - this.wordbook.checked = items.wordbook; - this.usePersonalKey.checked= items.usePersonalKey; - this.keyfrom.value = items.keyfrom; - this.key.value = items.key; - - switch (items.shortcut1) { - case KEY_CODE['CTRL']: - this.shortcut1.value = 'CTRL'; - break; - case KEY_CODE['SHIFT']: - this.shortcut1.value = 'SHIFT'; - break; - case KEY_CODE['ALT']: - this.shortcut1.value = 'ALT'; - break; - default: - this.shortcut1.value = String.fromCharCode(items.shortcut1).toUpperCase(); - } - - switch (items.shortcut2) { - case KEY_CODE['CTRL']: - this.shortcut2.value = 'CTRL'; - break; - case KEY_CODE['SHIFT']: - this.shortcut2.value = 'SHIFT'; - break; - case KEY_CODE['ALT']: - this.shortcut2.value = 'ALT'; - break; - case KEY_CODE['CMD']: - this.shortcut2.value = 'CMD'; - break; - default: - this.shortcut2.value = String.fromCharCode(items.shortcut2).toUpperCase(); - } - }); + constructor() { + this.options = document.querySelectorAll('input[type=checkbox]'); + this.dblclick = document.querySelector('#dblclick'); + this.shortcut = document.querySelector('#shortcut'); + this.shortcut1 = document.querySelector('#shortcut1'); + this.shortcut2 = document.querySelector('#shortcut2'); + this.selectToTranslate = document.querySelector('#selectToTranslate'); + this.wordbook = document.querySelector('#wordbook'); + this.usePersonalKey = document.querySelector('#usePersonalKey'); + this.keyfrom = document.querySelector('input#keyfrom'); + this.key = document.querySelector('input#key'); + + chrome.storage.sync.get((items) => { + this.dblclick.checked = items.dblclick; + this.shortcut.checked = items.shortcut; + this.selectToTranslate.checked = items.selectToTranslate; + this.wordbook.checked = items.wordbook; + this.usePersonalKey.checked = items.usePersonalKey || false; + this.keyfrom.value = items.keyfrom || ''; + this.key.value = items.key || ''; + + switch (items.shortcut1) { + case KEY_CODE['CTRL']: + this.shortcut1.value = 'CTRL'; + break; + case KEY_CODE['SHIFT']: + this.shortcut1.value = 'SHIFT'; + break; + case KEY_CODE['ALT']: + this.shortcut1.value = 'ALT'; + break; + default: + this.shortcut1.value = String.fromCharCode(items.shortcut1).toUpperCase(); + } + + switch (items.shortcut2) { + case KEY_CODE['CTRL']: + this.shortcut2.value = 'CTRL'; + break; + case KEY_CODE['SHIFT']: + this.shortcut2.value = 'SHIFT'; + break; + case KEY_CODE['ALT']: + this.shortcut2.value = 'ALT'; + break; + case KEY_CODE['CMD']: + this.shortcut2.value = 'CMD'; + break; + default: + this.shortcut2.value = String.fromCharCode(items.shortcut2).toUpperCase(); + } + }); this.prevUsePersonalKeyVal = this.usePersonalKey.value; - this.prevKeyfrom = this.keyfrom.value; - this.prevKey = this.key.value; - } - - _saveOptions() { - let dblclick = this.dblclick.checked; - let selectToTranslate = this.selectToTranslate.checked; - let wordbook = this.wordbook.checked; - let shortcut = this.shortcut.checked; - let shortcut1 = Object.is(this.shortcut1.keyCode, KEY_CODE['BACKSPACE']) ? null : this.shortcut1.keyCode; - let shortcut2 = Object.is(this.shortcut2.keyCode, KEY_CODE['BACKSPACE']) ? null : this.shortcut2.keyCode; - let usePersonalKey = this.usePersonalKey.checked; + this.prevKeyfrom = this.keyfrom.value; + this.prevKey = this.key.value; + } + + _saveOptions() { + let dblclick = this.dblclick.checked; + let selectToTranslate = this.selectToTranslate.checked; + let wordbook = this.wordbook.checked; + let shortcut = this.shortcut.checked; + let shortcut1 = Object.is(this.shortcut1.keyCode, KEY_CODE['BACKSPACE']) ? null : this.shortcut1.keyCode; + let shortcut2 = Object.is(this.shortcut2.keyCode, KEY_CODE['BACKSPACE']) ? null : this.shortcut2.keyCode; + let usePersonalKey = this.usePersonalKey.checked; let keyfrom = this.keyfrom.value; - let key = this.key.value; + let key = this.key.value; - if (usePersonalKey) { - let reqResult = this._requestKey(); + if (usePersonalKey) { + let reqResult = this._requestKey(); if (reqResult) { reqResult.then(res => { chrome.storage.sync.set({ @@ -105,7 +105,7 @@ class Options { usePersonalKey = this.usePersonalKey.checked = false; } return; - } else if (usePersonalKey !== this.prevUsePersonalKeyVal) { + } else if (usePersonalKey !== this.prevUsePersonalKeyVal) { chrome.storage.sync.set({ usePersonalKey }, () => { @@ -114,46 +114,46 @@ class Options { return; } - chrome.storage.sync.set({ - dblclick, - selectToTranslate, - wordbook, - shortcut, - shortcut1, - shortcut2 - }, () => { - Materialize.toast('设置成功', 500); - }); - } - - _handleKeyup(ev, ele) { - switch (ev.keyCode) { - case KEY_CODE['BACKSPACE']: - ele.value = ''; - break; - case KEY_CODE['CTRL']: - ele.value = 'CTRL'; - break; - case KEY_CODE['ALT']: - ele.value = 'ALT'; - break; - case KEY_CODE['SHIFT']: - ele.value = 'SHIFT'; - break; - case KEY_CODE['CMD']: - ele.value = 'CMD'; - break; - default: - ele.value = String.fromCharCode(ev.keyCode); - break; - } - ele.keyCode = ev.keyCode; - this._saveOptions(); - } - - _requestKey() { - let keyfrom = this.keyfrom.value; - let key = this.key.value; + chrome.storage.sync.set({ + dblclick, + selectToTranslate, + wordbook, + shortcut, + shortcut1, + shortcut2 + }, () => { + Materialize.toast('设置成功', 500); + }); + } + + _handleKeyup(ev, ele) { + switch (ev.keyCode) { + case KEY_CODE['BACKSPACE']: + ele.value = ''; + break; + case KEY_CODE['CTRL']: + ele.value = 'CTRL'; + break; + case KEY_CODE['ALT']: + ele.value = 'ALT'; + break; + case KEY_CODE['SHIFT']: + ele.value = 'SHIFT'; + break; + case KEY_CODE['CMD']: + ele.value = 'CMD'; + break; + default: + ele.value = String.fromCharCode(ev.keyCode); + break; + } + ele.keyCode = ev.keyCode; + this._saveOptions(); + } + + _requestKey() { + let keyfrom = this.keyfrom.value; + let key = this.key.value; if (!keyfrom || !key) { if (!keyfrom) { @@ -168,7 +168,7 @@ class Options { return youdao.getContent(); } - } + } _trimInput(inputEle) { if (!inputEle instanceof HTMLInputElement) { @@ -190,20 +190,20 @@ class Options { } } - init() { - this.shortcut1.addEventListener('click', (ev) => this.shortcut1.select()); - this.shortcut1.addEventListener('keyup', (ev) => this._handleKeyup(ev, shortcut1)); - this.shortcut2.addEventListener('click', (ev) => this.shortcut2.select()); - this.shortcut2.addEventListener('keyup', (ev) => this._handleKeyup(ev, shortcut2)); + init() { + this.shortcut1.addEventListener('click', (ev) => this.shortcut1.select()); + this.shortcut1.addEventListener('keyup', (ev) => this._handleKeyup(ev, shortcut1)); + this.shortcut2.addEventListener('click', (ev) => this.shortcut2.select()); + this.shortcut2.addEventListener('keyup', (ev) => this._handleKeyup(ev, shortcut2)); this.keyfrom.addEventListener('blur', (ev) => this._trimInput(ev.target)); this.keyfrom.addEventListener('keydown', this._touchUserKey.bind(this)); this.key.addEventListener('blur', (ev) => this._trimInput(ev.target)); this.key.addEventListener('keydown', this._touchUserKey.bind(this)); - Object.keys(this.options).forEach((key) => { - this.options[key].addEventListener('click', this._saveOptions.bind(this)); - }); - } + Object.keys(this.options).forEach((key) => { + this.options[key].addEventListener('click', this._saveOptions.bind(this)); + }); + } } let options = new Options();