add option to disable searching of alphanumeric text, fixes #47

This commit is contained in:
Alex Yatskov 2017-04-22 13:02:06 -07:00
parent 56fe02e945
commit e661c4bd43
6 changed files with 34 additions and 11 deletions

View File

@ -40,6 +40,7 @@ function formRead() {
optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked'); optionsNew.scanning.middleMouse = $('#middle-mouse-button-scan').prop('checked');
optionsNew.scanning.selectText = $('#select-matched-text').prop('checked'); optionsNew.scanning.selectText = $('#select-matched-text').prop('checked');
optionsNew.scanning.imposter = $('#search-form-text-fields').prop('checked'); optionsNew.scanning.imposter = $('#search-form-text-fields').prop('checked');
optionsNew.scanning.alphanumeric = $('#search-alphanumeric').prop('checked');
optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10); optionsNew.scanning.delay = parseInt($('#scan-delay').val(), 10);
optionsNew.scanning.length = parseInt($('#scan-length').val(), 10); optionsNew.scanning.length = parseInt($('#scan-length').val(), 10);
@ -127,6 +128,7 @@ $(document).ready(() => {
$('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse); $('#middle-mouse-button-scan').prop('checked', options.scanning.middleMouse);
$('#select-matched-text').prop('checked', options.scanning.selectText); $('#select-matched-text').prop('checked', options.scanning.selectText);
$('#search-form-text-fields').prop('checked', options.scanning.imposter); $('#search-form-text-fields').prop('checked', options.scanning.imposter);
$('#search-alphanumeric').prop('checked', options.scanning.alphanumeric);
$('#scan-delay').val(options.scanning.delay); $('#scan-delay').val(options.scanning.delay);
$('#scan-length').val(options.scanning.length); $('#scan-length').val(options.scanning.length);

View File

@ -41,10 +41,17 @@ class Translator {
}); });
} }
findTerms(text, dictionaries, softKatakana) { findTerms(text, dictionaries, softKatakana, alphanumeric) {
const titles = Object.keys(dictionaries); const titles = Object.keys(dictionaries);
const cache = {}; const cache = {};
if (!alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
return Promise.resolve({length: 0, definitions: []});
}
}
return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => { return this.findTermsDeinflected(text, titles, cache).then(deinfLiteral => {
const textHiragana = wanakana._katakanaToHiragana(text); const textHiragana = wanakana._katakanaToHiragana(text);
if (text !== textHiragana && softKatakana) { if (text !== textHiragana && softKatakana) {
@ -84,8 +91,8 @@ class Translator {
}); });
} }
findTermsGrouped(text, dictionaries, softKatakana) { findTermsGrouped(text, dictionaries, softKatakana, alphanumeric) {
return this.findTerms(text, dictionaries, softKatakana).then(({length, definitions}) => { return this.findTerms(text, dictionaries, softKatakana, alphanumeric).then(({length, definitions}) => {
return {length, definitions: dictTermsGroup(definitions, dictionaries)}; return {length, definitions: dictTermsGroup(definitions, dictionaries)};
}); });
} }

View File

@ -30,6 +30,20 @@ function promiseCallback(promise, callback) {
} }
/*
* Japanese
*/
function jpIsKanji(c) {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
}
function jpIsKana(c) {
return wanakana.isKana(c);
}
/* /*
* Commands * Commands
*/ */
@ -98,6 +112,7 @@ function optionsSetDefaults(options) {
middleMouse: true, middleMouse: true,
selectText: true, selectText: true,
imposter: true, imposter: true,
alphanumeric: true,
delay: 15, delay: 15,
length: 10 length: 10
}, },
@ -489,14 +504,9 @@ function jsonLoadDb(indexUrl, indexLoaded, termsLoaded, kanjiLoaded) {
*/ */
function handlebarsKanjiLinks(options) { function handlebarsKanjiLinks(options) {
const isKanji = c => {
const code = c.charCodeAt(0);
return code >= 0x4e00 && code < 0x9fb0 || code >= 0x3400 && code < 0x4dc0;
};
let result = ''; let result = '';
for (const c of options.fn(this)) { for (const c of options.fn(this)) {
if (isKanji(c)) { if (jpIsKanji(c)) {
result += `<a href="#" class="kanji-link">${c}</a>`; result += `<a href="#" class="kanji-link">${c}</a>`;
} else { } else {
result += c; result += c;

View File

@ -107,7 +107,7 @@ window.yomichan = new class {
this.translator.findTermsGrouped.bind(this.translator) : this.translator.findTermsGrouped.bind(this.translator) :
this.translator.findTerms.bind(this.translator); this.translator.findTerms.bind(this.translator);
return searcher(text, dictEnabledSet(this.options), this.options.general.softKatakana).then(({definitions, length}) => { return searcher(text, dictEnabledSet(this.options), this.options.general.softKatakana, this.options.scanning.alphanumeric).then(({definitions, length}) => {
return {length, definitions: definitions.slice(0, this.options.general.maxResults)}; return {length, definitions: definitions.slice(0, this.options.general.maxResults)};
}); });
} }

View File

@ -93,6 +93,10 @@
<label><input type="checkbox" id="search-form-text-fields"> Search form text fields</label> <label><input type="checkbox" id="search-form-text-fields"> Search form text fields</label>
</div> </div>
<div class="checkbox">
<label><input type="checkbox" id="search-alphanumeric"> Search alphanumeric text</label>
</div>
<div class="form-group options-advanced"> <div class="form-group options-advanced">
<label for="scan-delay">Scan delay</label> <label for="scan-delay">Scan delay</label>
<input type="number" min="1" id="scan-delay" class="form-control"> <input type="number" min="1" id="scan-delay" class="form-control">

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "Yomichan", "name": "Yomichan",
"version": "1.1.13", "version": "1.1.14",
"description": "Japanese dictionary with Anki integration", "description": "Japanese dictionary with Anki integration",
"icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"}, "icons": {"16": "mixed/img/icon16.png", "48": "mixed/img/icon48.png", "128": "mixed/img/icon128.png"},