<commit_before>/* LanguageTool WebExtension
 * Copyright (C) 2016-2017 <NAME> (http://www.danielnaber.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
"use strict";

let defaultServerUrl = 'https://languagetool.org/api/v2';   // keep in sync with defaultServerUrl in options.js
let defaultPremiumServerUrl = 'https://languagetoolplus.com/api/v2';

let thisExtensionUrl = "https://chrome.google.com/webstore/detail/languagetool/oldceeleldhonbafppcapldpdifcinji";

let googleDocsExtension = "https://chrome.google.com/webstore/detail/languagetool/kjcoklfhicmkbfifghaecedbohbmofkm";

// see https://github.com/languagetool-org/languagetool-browser-addon/issues/70:
let unsupportedReplacementSitesRegex = /^https?:\/\/(www\.)?(facebook|medium).com.*/;

// turn off some rules by default because they are not that useful in a typical web context:
const ruleIdsIgnoredByDefault = [
    // English:
    {id: 'EN_QUOTES', language: 'en'},
    {id: 'DASH_RULE', language: 'en'},
    // German:
    {id: 'TYPOGRAFISCHE_ANFUEHRUNGSZEICHEN', language: 'de'},
    {id: 'FALSCHE_VERWENDUNG_DES_BINDESTRICHS', language: 'de'},
    {id: 'BISSTRICH', language: 'de'},
    {id: 'AUSLASSUNGSPUNKTE', language: 'de'},
];

// ask the user for a review in the store if they have used this add-on at least this many times:
let minUsageForReviewRequest = 30;
let pageUrlParam = "";
const pageUrlPosition = window.location.href.indexOf("?pageUrl=");
if (pageUrlPosition !== -1) {
  pageUrlParam = window.location.href.substr(pageUrlPosition + 9);
}

var testMode = false;
var serverUrl = defaultServerUrl;
var ignoreQuotedLines = true;
var quotedLinesIgnored = false;
var motherTongue = "";
var preferredVariants = [];
var manuallySelectedLanguage = "";
var errorGettingToken = false;
var missingPremiumSession = false;

function getToken(callback, errorCallback) {
    const tokenUrl = "https://languagetoolplus.com/token";
    const req = new XMLHttpRequest();
    req.open('GET', tokenUrl);
    req.onload = function() {
        let response = req.response;
        const loggedIn = response !== '';
        if (req.status !== 200) {
            errorCallback(chrome.i18n.getMessage("noValidResponseFromServer", [tokenUrl, req.response, req.status]), "noValidResponseFromServer");
            return;
        }
        callback(loggedIn, response);
    };
    req.onerror = function() {
        errorCallback(chrome.i18n.getMessage("networkError", tokenUrl), "networkError");
    };
    req.ontimeout = function() {
        errorCallback(chrome.i18n.getMessage("timeoutError", tokenUrl), "timeoutError");
    };
    req.send();
}

function getCheckResult(markupList, metaData, callback, errorCallback) {
    const req = new XMLHttpRequest();
    req.timeout = 60 * 1000; // milliseconds
    const url = serverUrl + (serverUrl.endsWith("/") ? "check" : "/check");
    req.open('POST', url);
    req.onload = function() {
        let response = req.response;
        if (!response) {
            errorCallback(chrome.i18n.getMessage("noResponseFromServer", serverUrl), "noResponseFromServer");
            return;
        }<commit_msg>cache the premium token for better performance<commit_after>/* LanguageTool WebExtension
 * Copyright (C) 2016-2017 <NAME> (http://www.danielnaber.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
"use strict";

let defaultServerUrl = 'https://languagetool.org/api/v2';   // keep in sync with defaultServerUrl in options.js
let defaultPremiumServerUrl = 'https://languagetoolplus.com/api/v2';

let thisExtensionUrl = "https://chrome.google.com/webstore/detail/languagetool/oldceeleldhonbafppcapldpdifcinji";

let googleDocsExtension = "https://chrome.google.com/webstore/detail/languagetool/kjcoklfhicmkbfifghaecedbohbmofkm";

// see https://github.com/languagetool-org/languagetool-browser-addon/issues/70:
let unsupportedReplacementSitesRegex = /^https?:\/\/(www\.)?(facebook|medium).com.*/;

const maxTokenCacheAgeMinutes = 60;

// turn off some rules by default because they are not that useful in a typical web context:
const ruleIdsIgnoredByDefault = [
    // English:
    {id: 'EN_QUOTES', language: 'en'},
    {id: 'DASH_RULE', language: 'en'},
    // German:
    {id: 'TYPOGRAFISCHE_ANFUEHRUNGSZEICHEN', language: 'de'},
    {id: 'FALSCHE_VERWENDUNG_DES_BINDESTRICHS', language: 'de'},
    {id: 'BISSTRICH', language: 'de'},
    {id: 'AUSLASSUNGSPUNKTE', language: 'de'},
];

// ask the user for a review in the store if they have used this add-on at least this many times:
let minUsageForReviewRequest = 30;
let pageUrlParam = "";
const pageUrlPosition = window.location.href.indexOf("?pageUrl=");
if (pageUrlPosition !== -1) {
  pageUrlParam = window.location.href.substr(pageUrlPosition + 9);
}

var testMode = false;
var serverUrl = defaultServerUrl;
var ignoreQuotedLines = true;
var quotedLinesIgnored = false;
var motherTongue = "";
var preferredVariants = [];
var manuallySelectedLanguage = "";
var errorGettingToken = false;
var missingPremiumSession = false;

function getToken(callback, errorCallback) {
    Tools.getStorage().get({
        token: null,
        tokenDate: null
    }, function(items) {
        const token = items.token;
        const tokenDate = items.tokenDate;
        if (token && token !== '' && tokenDate) {
            const tokenDate = new Date(items.tokenDate);
            console.log("cached token date: ", tokenDate.toISOString());
            const tokenAgeMinutes = (new Date() - tokenDate) / 1000 / 60;
            console.log("cached tokenAgeMinutes: ", tokenAgeMinutes);
            if (tokenAgeMinutes < maxTokenCacheAgeMinutes) {
                console.log("using cached token");
                callback(true, token);
                return;
            } else {
                console.log("getting new token, cached one is too old");
            }
        }
        console.log("getting new token");
        const tokenUrl = "https://languagetoolplus.com/token";
        const req = new XMLHttpRequest();
        req.open('GET', tokenUrl);
        req.onload = function() {
            let response = req.response;
            const loggedIn = response !== '';
            if (req.status !== 200) {
                errorCallback(chrome.i18n.getMessage("noValidResponseFromServer", [tokenUrl, req.response, req.status]), "noValidResponseFromServer");
                return;
            }
            Tools.getStorage().set({
                token: response,
                tokenDate: new Date().getTime()
            });
            callback(loggedIn, response);
        };
        req.onerror = function() {
            errorCallback(chrome.i18n.getMessage("networkError", tokenUrl), "networkError");
        };
        req.ontimeout = function() {
            errorCallback(chrome.i18n.getMessage("timeoutError", tokenUrl), "timeoutError");
        };
        req.send();
    });
}

function getCheckResult(markupList, metaData, callback, errorCallback) {
    const req = new XMLHttpRequest();
    req.timeout = 60 * 1000; // milliseconds
    const url = serverUrl + (serverUrl.endsWith("/") ? "check" : "/check");
    req.open('POST', url);
    req.onload = function() {
        let response = req.response;
        if (!response) {
            errorCallback(chrome.i18n.getMessage("noResponseFromServer", serverUrl), "noResponseFromServer");
            return;
        }
{'fraction_non_alphanumeric': 0.11968449931412894, 'fraction_numerical': 0.011431184270690443, 'mean_word_length': 3.3484095427435387, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 12, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3061606', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2541, 'n_words': 724}