Skip to content

Commit

Permalink
Remove disableRule button, add addCandidate button
Browse files Browse the repository at this point in the history
  • Loading branch information
kalekale committed Apr 20, 2020
1 parent a1dad10 commit 5becf42
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 68 deletions.
4 changes: 2 additions & 2 deletions webextension/_locales/en/messages.json
Expand Up @@ -233,8 +233,8 @@
"description": "Link shown in the popup to accept a word to the internal dictionary"
},
"addToDictionaryTitle": {
"message": "add \"$word$\" to personal dictionary",
"description": "Title hover shown in the popup to append a word into the internal dictionary",
"message": "add \"$word$\" as a candidate",
"description": "Title hover shown in the popup to append a word to dictionary as a candidate",
"placeholders": {
"word": {
"content": "$1",
Expand Down
32 changes: 32 additions & 0 deletions webextension/bs/common.js
Expand Up @@ -27,6 +27,38 @@ Tools.getStorage().get({
ignoreQuotedLines = items.ignoreQuotedLines;
});

function addCandidate(word, callback, errorCallback) {
Tools.getApiServerUrl(serverUrl => {
const req = new XMLHttpRequest();
const url = serverUrl + (serverUrl.endsWith("/") ? "api/candidate" : "/api/candidate");
req.open('POST', url);
req.setRequestHeader("Content-Type", "application/json");
req.onload = function() {
let response = req.response;
if (!response) {
errorCallback(chrome.i18n.getMessage("noResponseFromServer", serverUrl), "noResponseFromServer");
return;
}
if (req.status !== 200) {
errorCallback(chrome.i18n.getMessage("noValidResponseFromServer", [serverUrl, req.response, req.status]), "noValidResponseFromServer", req.status);
return;
}
callback(response);
};
req.onerror = function() {
errorCallback(chrome.i18n.getMessage("networkError", serverUrl), "networkError");
};
req.ontimeout = function() {
errorCallback(chrome.i18n.getMessage("timeoutError", serverUrl), "timeoutError");
};
let params = { 'subterms': [{ 'category': 'singular',
'word': word}],
'term': {'part-of-speech': 'noun',
'approved': true}};
req.send(JSON.stringify(params));
});
}

function getCheckResult(markupList, metaData, callback, errorCallback) {
Tools.getApiServerUrl(serverUrl => {
const startTime = new Date().getTime();
Expand Down
71 changes: 5 additions & 66 deletions webextension/popup/popup.js
Expand Up @@ -174,14 +174,10 @@ function renderMatchesToHtml(resultJson, response, tabs, callback) {
}
} else {
html += "<div class=\"suggestionRow " + suggestionClass(m) + "\">\n";
if (isSpellingError(m)) {
if (m.rule.id === "non-listed") {
const escapedWord = Tools.escapeHtml(wordSanitized);
html += "<div class='addToDict' data-addtodict='" + escapedWord + "'" +
" title='" + chrome.i18n.getMessage("addToDictionaryTitle", escapedWord).replace(/'/, "&apos;") + "'></div>";
} else {
html += "<div class='turnOffRule' data-ruleIdOff='" + Tools.escapeHtml(ruleIdSanitized) + "'" +
" data-ruleDescription='" + Tools.escapeHtml(descriptionSanitized) + "'" +
" title='" + chrome.i18n.getMessage("turnOffRule").replace(/'/, "&apos;") + "'></div>";
}
html += Tools.escapeHtml(messageSanitized);
html += renderContext(contextSanitized, errStart, errLen, errColor);
Expand Down Expand Up @@ -239,12 +235,6 @@ function renderMatchesToHtml(resultJson, response, tabs, callback) {
renderStatus(html);

document.getElementById('ltIcon').src = chrome.extension.getURL("images/logo34x34.png");
if (items.havePremiumAccount) {
document.getElementById('ltLink').href = "https://languagetoolplus.com";
} else {
document.getElementById('ltLink').href = "https://languagetool.org";
}
document.getElementById('ltLink').target = "_blank";
setHintListener();
if (disabledOnThisDomain) {
setReactivateIconListener(response.url || pageUrlParam, tabs);
Expand Down Expand Up @@ -442,54 +432,9 @@ function renderReplacements(contextSanitized, m, createLinks) {
}

function addLinkListeners(response, tabs, languageCode) {
document.getElementById("language").addEventListener("change", function() {
const prevLanguage = document.getElementById("prevLanguage").value;
if (!initLanguage) initLanguage = prevLanguage;
manuallySelectedLanguage = document.getElementById("language").value;
sendMessageToTab(tabs[0].id, {
action: "saveLanguagesSettings",
data: {
initLanguage : initLanguage,
manuallySelectedLanguage: manuallySelectedLanguage
}
});

const langSwitch = prevLanguage + " -> " + manuallySelectedLanguage;
doCheck(tabs, "switch_language", langSwitch);
});
document.getElementById("closeLink").addEventListener("click", function() {
self.close();
});
const saveVariantLink = document.getElementById("saveVariantLink");
if (saveVariantLink) {
saveVariantLink.addEventListener("click", function(e) {
const languageGroup = saveVariantLink.getAttribute("data-languageGroup");
const languageVariant = saveVariantLink.getAttribute("data-languageVariant");

const options = {};
options[languageGroup + "Variant"] = languageVariant;
Tools.getStorage().set(options);

// update preferredVariants
let added = false;
for (let i = 0; i < preferredVariants.length; i++) {
if (preferredVariants[i].indexOf(languageGroup) === 0) {
preferredVariants[i] = languageVariant;
added = true;
break;
}
}

if (!added) {
preferredVariants.push(languageVariant);
}

initLanguage = languageVariant;

document.getElementById("saveVariant").remove();
e.preventDefault();
});
}
const closeLink2 = document.getElementById("close");
if (closeLink2) {
closeLink2.addEventListener("click", function() {
Expand Down Expand Up @@ -546,17 +491,11 @@ function addListenerActions(elements, tabs, response, languageCode) {
});

} else if (link.getAttribute('data-addtodict')) {
storage.get({
dictionary: []
}, function(items) {
const dictionary = items.dictionary;
dictionary.push(link.getAttribute('data-addtodict'));
storage.set({'dictionary': dictionary}, function() {
closePopupAfterRecheck = true;
reCheck(tabs, "add_to_dict")
});
addCandidate(link.getAttribute('data-addtodict'), function (res) {
reCheck(tabs, "add_to_dict");
}, function (err) {
console.info(err);
});

} else if (link.getAttribute('data-errortext')) {
const data = {
action: 'applyCorrection',
Expand Down

0 comments on commit 5becf42

Please sign in to comment.