diff --git a/src/content.js b/src/content.js index 4c81875..2c1a6be 100644 --- a/src/content.js +++ b/src/content.js @@ -1,118 +1,112 @@ -var language; -var targetWord; - if (typeof browser === "undefined") { - browser = { - runtime: { - sendMessage: function(msg) { - return new Promise(function(resolve, reject) { - chrome.runtime.sendMessage(msg, resolve); - }); - } - }, - i18n: chrome.i18n - } + browser = { + runtime: { + sendMessage: function(msg) { + return new Promise(function(resolve, reject) { + chrome.runtime.sendMessage(msg, resolve); + }); + } + }, + i18n: chrome.i18n + } } chrome.storage.sync.get(["sitesSettings"], function(result) { - const hostnameWithoutWww = location.hostname.replace(/^www\./g, ''); - const hostnameWithoutTopDomain = hostnameWithoutWww.replace(/\.[^.]+$/g, '.*'); - const settingsList = result.sitesSettings || defaultSitesSettings; - const siteSetting = settingsList[hostnameWithoutWww] || settingsList[hostnameWithoutTopDomain]; + const hostnameWithoutWww = location.hostname.replace(/^www\./g, ''); + const hostnameWithoutTopDomain = hostnameWithoutWww.replace(/\.[^.]+$/g, '.*'); + const settingsList = result.sitesSettings || defaultSitesSettings; + const siteSetting = settingsList[hostnameWithoutWww] || settingsList[hostnameWithoutTopDomain]; - if (!siteSetting) { - return; - } + if (!siteSetting) { + return; + } - language = siteSetting.language; - targetWord = siteSetting.targetWordSource === 'url' - ? decodeURIComponent(window.location.toString() - .split('/') - .pop() - .split(/[\\?#]/) - .shift() - .replace(/[+-]/g, ' ') - .replace(/%20/g, ' ')) - : $(siteSetting.targetWordSelector).text(); - const excludedChildrenFromExample = siteSetting.excludedChildrenFromExample; + const language = siteSetting.language; + const targetWord = siteSetting.targetWordSource === 'url' + ? decodeURIComponent(window.location.toString() + .split('/') + .pop() + .split(/[\\?#]/) + .shift() + .replace(/[+-]/g, ' ') + .replace(/%20/g, ' ')) + : $(siteSetting.targetWordSelector).text(); + const excludedChildrenFromExample = siteSetting.excludedChildrenFromExample; - console.log('targetWord: ' + targetWord); + console.log('targetWord: ' + targetWord); - const exampleTag = typeof siteSetting.getExampleFromSibling !== "undefined" - ? $(siteSetting.exampleSelector).siblings(siteSetting.getExampleFromSibling) - : $(siteSetting.exampleSelector); + const exampleTag = typeof siteSetting.getExampleFromSibling !== "undefined" + ? $(siteSetting.exampleSelector).siblings(siteSetting.getExampleFromSibling) + : $(siteSetting.exampleSelector); - exampleTag.each(function(index) { - $(this).append(` [${browser.i18n.getMessage("save")}]`); - }); + exampleTag.each(function(index) { + $(this).append(` [${browser.i18n.getMessage("save")}]`); + }); - $(".saveLink").click(function() { - const self = $(this); - const parent = self.parent(); - const manuallyEditedInput = parent.find(".manuallyEdited").first(); - const manuallyEditedValue = manuallyEditedInput.find("textarea").first().val(); - self.nextAll().remove(); - let stringWithMarkedWord; - if (manuallyEditedValue) { - stringWithMarkedWord = manuallyEditedValue; + $(".saveLink").click(function() { + const self = $(this); + const parent = self.parent(); + const manuallyEditedInput = parent.find(".manuallyEdited").first(); + const manuallyEditedValue = manuallyEditedInput.find("textarea").first().val(); + self.nextAll().remove(); + let stringWithMarkedWord; + if (manuallyEditedValue) { + stringWithMarkedWord = manuallyEditedValue; + } else { + const exampleElement = typeof excludedChildrenFromExample !== 'undefined' + ? parent.clone() + .children(excludedChildrenFromExample) + .remove() + .end() + : parent; + const exampleString = exampleElement.text() + .replace(/\[(.*?)\]/g, "") // removing everything in brackets, including "[Save]" + .trim() + .replace(/^"?|"?$/g, ''); // removing leading & trailing double quotes if any + stringWithMarkedWord = markTargetWord(exampleString); + console.log(stringWithMarkedWord); + if (!isWordMarked(stringWithMarkedWord)) { + parent.append("
"); + return; + } + } + self.hide(); + console.log('Saving: "' + stringWithMarkedWord + '"'); + const saving = $(`${browser.i18n.getMessage("saving")}`).appendTo(parent); + browser.runtime.sendMessage({language: language, str: stringWithMarkedWord, url: window.location.href}) + .then(function(response) { + manuallyEditedInput.remove(); + if (response.success) { + saving.replaceWith(`${browser.i18n.getMessage("saved")}`); } else { - const exampleElement = typeof excludedChildrenFromExample !== 'undefined' - ? parent.clone() - .children(excludedChildrenFromExample) - .remove() - .end() - : parent; - const exampleString = exampleElement - .text() - .replace(/\[(.*?)\]/g, "") // removing everything in brackets, including "[Save]" - .trim() - .replace(/^"?|"?$/g, ''); // removing leading & trailing double quotes if any - stringWithMarkedWord = markTargetWord(exampleString); - console.log(stringWithMarkedWord); - if (!isWordMarked(stringWithMarkedWord)) { - parent.append("
"); - return; - } + self.show(); + console.error('Could not save: "' + stringWithMarkedWord + '"'); + saving.replaceWith(` ${browser.i18n.getMessage("couldNotSave")} (${response.message})`); } - self.hide(); - console.log('Saving: "' + stringWithMarkedWord + '"'); - const saving = $(`${browser.i18n.getMessage("saving")}`).appendTo(parent); - browser.runtime.sendMessage({language: language, str: stringWithMarkedWord, url: window.location.href}) - .then(function(response) { - manuallyEditedInput.remove(); - if (response.success) { - saving.replaceWith(`${browser.i18n.getMessage("saved")}`); - } else { - self.show(); - console.error('Could not save: "' + stringWithMarkedWord + '"'); - saving.replaceWith(` ${browser.i18n.getMessage("couldNotSave")} (${response.message})`); - } - }); - }); -}); + }); -function markTargetWord(str) { - const wordEndingRegex = "[^\\s.?,!:;]*"; - let regex = ""; - for (let word of targetWord.split(" ")) { - if (language === 'en') { + function markTargetWord(str) { + const wordEndingRegex = "[^\\s.?,!:;]*"; + let regex = ""; + for (let word of targetWord.split(" ")) { + if (language === 'en') { if (word !== targetWord && PRONOUNS_EN.has(word)) { - continue; + continue; } else if (word.endsWith('y') || word.endsWith('e')) { - word = word.substring(0, word.length - 1); + word = word.substring(0, word.length - 1); } - } else if (language === 'es') { + } else if (language === 'es') { if (word.length > 3) { - word = word.substring(0, word.length - 2); + word = word.substring(0, word.length - 2); } + } + regex += (regex === "" ? "" : "\\s+") + word + wordEndingRegex; } - regex += (regex === "" ? "" : "\\s+") + word + wordEndingRegex; - } - return str.replace(new RegExp(regex,"ig"), "*$&*"); -} + return str.replace(new RegExp(regex,"ig"), "*$&*"); + } -const PRONOUNS_EN = new Set(["sth", "sb", "something", "somebody", "someone"]); + function isWordMarked(str) { return str.indexOf("*") != -1 } + }); +}); -function isWordMarked(str) { - return str.indexOf("*") != -1; -} \ No newline at end of file +const PRONOUNS_EN = new Set(["sth", "sb", "something", "somebody", "someone"]); \ No newline at end of file