diff options
| -rw-r--r-- | ext/fg/js/api.js | 42 | ||||
| -rw-r--r-- | ext/fg/js/client.js | 90 | ||||
| -rw-r--r-- | ext/fg/js/frame.js | 6 | ||||
| -rw-r--r-- | ext/fg/js/popup.js | 4 | ||||
| -rw-r--r-- | ext/fg/js/util.js | 114 | ||||
| -rw-r--r-- | ext/manifest.json | 2 | 
6 files changed, 129 insertions, 129 deletions
| diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js deleted file mode 100644 index 643d0360..00000000 --- a/ext/fg/js/api.js +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2016  Alex Yatskov <alex@foosoft.net> - * Author: Alex Yatskov <alex@foosoft.net> - * - * This program 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. - * - * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. - */ - - -function bgSendMessage(action, params) { -    return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve)); -} - -function bgFindTerm(text) { -    return bgSendMessage('findTerm', {text}); -} - -function bgFindKanji(text) { -    return bgSendMessage('findKanji', {text}); -} - -function bgRenderText(data, template) { -    return bgSendMessage('renderText', {data, template}); -} - -function bgCanAddDefinitions(definitions, modes) { -    return bgSendMessage('canAddDefinitions', {definitions, modes}); -} - -function bgAddDefinition(definition, mode) { -    return bgSendMessage('addDefinition', {definition, mode}); -} diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js index 9a0ad25a..daeabf99 100644 --- a/ext/fg/js/client.js +++ b/ext/fg/js/client.js @@ -82,7 +82,7 @@ class Client {              return;          } -        const textSource = Client.textSourceFromPoint(point); +        const textSource = textSourceFromPoint(point);          if (textSource === null || !textSource.containsPoint(point)) {              this.hidePopup();              return; @@ -95,25 +95,25 @@ class Client {          textSource.setEndOffset(this.options.scanLength);          this.pendingLookup = true; -        bgFindTerm(textSource.text()).then(({definitions, length}) => { +        findTerm(textSource.text()).then(({definitions, length}) => {              if (length === 0) {                  this.pendingLookup = false;                  this.hidePopup();              } else {                  textSource.setEndOffset(length); -                const sentence = Client.extractSentence(textSource, this.options.sentenceExtent); +                const sentence = extractSentence(textSource, this.options.sentenceExtent);                  definitions.forEach(definition => {                      definition.url = window.location.href;                      definition.sentence = sentence;                  });                  const sequence = ++this.sequence; -                return bgRenderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'term-list.html').then(content => { +                return renderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'term-list.html').then(content => {                      this.definitions = definitions;                      this.pendingLookup = false;                      this.showPopup(textSource, content); -                    return bgCanAddDefinitions(definitions, ['term_kanji', 'term_kana']); +                    return canAddDefinitions(definitions, ['term_kanji', 'term_kana']);                  }).then(states => {                      if (states !== null) {                          states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence })); @@ -156,7 +156,7 @@ class Client {      api_addNote({index, mode}) {          const state = {[mode]: false}; -        bgAddDefinition(this.definitions[index], mode).then(success => { +        addDefinition(this.definitions[index], mode).then(success => {              if (success) {                  this.popup.sendMessage('setActionState', {index, state, sequence: this.sequence});              } else { @@ -185,14 +185,14 @@ class Client {      }      api_displayKanji(kanji) { -        bgFindKanji(kanji).then(definitions => { +        findKanji(kanji).then(definitions => {              definitions.forEach(definition => definition.url = window.location.href);              const sequence = ++this.sequence; -            return bgRenderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'kanji-list.html').then(content => { +            return renderText({definitions, sequence, root: this.fgRoot, options: this.options}, 'kanji-list.html').then(content => {                  this.definitions = definitions;                  this.popup.setContent(content, definitions); -                return bgCanAddDefinitions(definitions, ['kanji']); +                return canAddDefinitions(definitions, ['kanji']);              }).then(states => {                  if (states !== null) {                      states.forEach((state, index) => this.popup.sendMessage('setActionState', {index, state, sequence})); @@ -200,78 +200,6 @@ class Client {              });          });      } - -    static textSourceFromPoint(point) { -        const element = document.elementFromPoint(point.x, point.y); -        if (element !== null) { -            const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA']; -            if (names.includes(element.nodeName)) { -                return new TextSourceElement(element); -            } -        } - -        const range = document.caretRangeFromPoint(point.x, point.y); -        if (range !== null) { -            return new TextSourceRange(range); -        } - -        return null; -    } - -    static extractSentence(source, extent) { -        const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'}; -        const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'}; -        const terminators = '…。..??!!'; - -        const sourceLocal = source.clone(); -        const position = sourceLocal.setStartOffset(extent); -        sourceLocal.setEndOffset(position + extent); -        const content = sourceLocal.text(); - -        let quoteStack = []; - -        let startPos = 0; -        for (let i = position; i >= startPos; --i) { -            const c = content[i]; - -            if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) { -                startPos = i + 1; -                break; -            } - -            if (quoteStack.length > 0 && c === quoteStack[0]) { -                quoteStack.pop(); -            } else if (c in quotesBwd) { -                quoteStack = [quotesBwd[c]].concat(quoteStack); -            } -        } - -        quoteStack = []; - -        let endPos = content.length; -        for (let i = position; i < endPos; ++i) { -            const c = content[i]; - -            if (quoteStack.length === 0) { -                if (terminators.includes(c)) { -                    endPos = i + 1; -                    break; -                } -                else if (c in quotesBwd) { -                    endPos = i; -                    break; -                } -            } - -            if (quoteStack.length > 0 && c === quoteStack[0]) { -                quoteStack.pop(); -            } else if (c in quotesFwd) { -                quoteStack = [quotesFwd[c]].concat(quoteStack); -            } -        } - -        return content.substring(startPos, endPos).trim(); -    }  }  window.yomiClient = new Client(); diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js index 8f3d3877..30cb7c73 100644 --- a/ext/fg/js/frame.js +++ b/ext/fg/js/frame.js @@ -19,7 +19,7 @@  function registerKanjiLinks() {      for (const link of Array.from(document.getElementsByClassName('kanji-link'))) { -        link.addEventListener('click', (e) => { +        link.addEventListener('click', e => {              e.preventDefault();              window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*');          }); @@ -28,7 +28,7 @@ function registerKanjiLinks() {  function registerAddNoteLinks() {      for (const link of Array.from(document.getElementsByClassName('action-add-note'))) { -        link.addEventListener('click', (e) => { +        link.addEventListener('click', e => {              e.preventDefault();              const ds = e.currentTarget.dataset;              window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*'); @@ -38,7 +38,7 @@ function registerAddNoteLinks() {  function registerAudioLinks() {      for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) { -        link.addEventListener('click', (e) => { +        link.addEventListener('click', e => {              e.preventDefault();              const ds = e.currentTarget.dataset;              window.parent.postMessage({action: 'playAudio', params: ds.index}, '*'); diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js index a0eb725c..930df18d 100644 --- a/ext/fg/js/popup.js +++ b/ext/fg/js/popup.js @@ -81,8 +81,8 @@ class Popup {          this.popup = document.createElement('iframe');          this.popup.id = 'yomichan-popup'; -        this.popup.addEventListener('mousedown', (e) => e.stopPropagation()); -        this.popup.addEventListener('scroll', (e) => e.stopPropagation()); +        this.popup.addEventListener('mousedown', e => e.stopPropagation()); +        this.popup.addEventListener('scroll', e => e.stopPropagation());          document.body.appendChild(this.popup);      } diff --git a/ext/fg/js/util.js b/ext/fg/js/util.js new file mode 100644 index 00000000..176765fc --- /dev/null +++ b/ext/fg/js/util.js @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2016  Alex Yatskov <alex@foosoft.net> + * Author: Alex Yatskov <alex@foosoft.net> + * + * This program 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. + * + * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>. + */ + + +function sendMessage(action, params) { +    return new Promise((resolve, reject) => chrome.runtime.sendMessage({action, params}, resolve)); +} + +function findTerm(text) { +    return sendMessage('findTerm', {text}); +} + +function findKanji(text) { +    return sendMessage('findKanji', {text}); +} + +function renderText(data, template) { +    return sendMessage('renderText', {data, template}); +} + +function canAddDefinitions(definitions, modes) { +    return sendMessage('canAddDefinitions', {definitions, modes}); +} + +function addDefinition(definition, mode) { +    return sendMessage('addDefinition', {definition, mode}); +} + +function textSourceFromPoint(point) { +    const element = document.elementFromPoint(point.x, point.y); +    if (element !== null) { +        const names = ['IMG', 'INPUT', 'BUTTON', 'TEXTAREA']; +        if (names.includes(element.nodeName)) { +            return new TextSourceElement(element); +        } +    } + +    const range = document.caretRangeFromPoint(point.x, point.y); +    if (range !== null) { +        return new TextSourceRange(range); +    } + +    return null; +} + +function extractSentence(source, extent) { +    const quotesFwd = {'「': '」', '『': '』', "'": "'", '"': '"'}; +    const quotesBwd = {'」': '「', '』': '『', "'": "'", '"': '"'}; +    const terminators = '…。..??!!'; + +    const sourceLocal = source.clone(); +    const position = sourceLocal.setStartOffset(extent); +    sourceLocal.setEndOffset(position + extent); +    const content = sourceLocal.text(); + +    let quoteStack = []; + +    let startPos = 0; +    for (let i = position; i >= startPos; --i) { +        const c = content[i]; + +        if (quoteStack.length === 0 && (terminators.includes(c) || c in quotesFwd)) { +            startPos = i + 1; +            break; +        } + +        if (quoteStack.length > 0 && c === quoteStack[0]) { +            quoteStack.pop(); +        } else if (c in quotesBwd) { +            quoteStack = [quotesBwd[c]].concat(quoteStack); +        } +    } + +    quoteStack = []; + +    let endPos = content.length; +    for (let i = position; i < endPos; ++i) { +        const c = content[i]; + +        if (quoteStack.length === 0) { +            if (terminators.includes(c)) { +                endPos = i + 1; +                break; +            } +            else if (c in quotesBwd) { +                endPos = i; +                break; +            } +        } + +        if (quoteStack.length > 0 && c === quoteStack[0]) { +            quoteStack.pop(); +        } else if (c in quotesFwd) { +            quoteStack = [quotesFwd[c]].concat(quoteStack); +        } +    } + +    return content.substring(startPos, endPos).trim(); +} diff --git a/ext/manifest.json b/ext/manifest.json index 080f6f4f..c3864e10 100644 --- a/ext/manifest.json +++ b/ext/manifest.json @@ -15,7 +15,7 @@              "fg/js/source-range.js",              "fg/js/source-element.js",              "fg/js/popup.js", -            "fg/js/api.js", +            "fg/js/util.js",              "fg/js/client.js"          ],          "css": ["fg/css/client.css"] |