<commit_before>/* LanguageTool WebExtension
 * Copyright (C) 2016 <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 quotedLinesIgnored = false;
let ignoreQuotedLines = true;

Tools.getStorage().get({
    ignoreQuotedLines: ignoreQuotedLines,
}, function(items) {
  ignoreQuotedLines = items.ignoreQuotedLines;
});

function getCheckResult(markupList, metaData, callback, errorCallback) {
    Tools.getApiServerUrl(serverUrl => {
      let text = Markup.markupList2text(markupList);
      if (ignoreQuotedLines) {
          const textOrig = text;
          // A hack so the following replacements don't happen on messed up character positions.
          // See https://github.com/languagetool-org/languagetool-browser-addon/issues/25:
          text = text.replace(/^>.*?\n/gm, function(match) {
              return " ".repeat(match.length - 1) + "\n";
          });
          quotedLinesIgnored = text != textOrig;
      }
      if (text.trim().length === 0) {
        return callback('{}');
      }
      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;
          }
          if (req.status !== 200) {
              errorCallback(chrome.i18n.getMessage("noValidResponseFromServer", [serverUrl, req.response, req.status]), "noValidResponseFromServer");
              return;
          }
          callback(response);
      };
      req.onerror = function() {
          errorCallback(chrome.i18n.getMessage("networkError", serverUrl), "networkError");
      };
      req.ontimeout = function() {
          errorCallback(chrome.i18n.getMessage("timeoutError", serverUrl), "timeoutError");
      };
      let userAgent = "webextension";
      if (Tools.isFirefox()) {
          userAgent += "-firefox";
      } else if (Tools.isChrome()) {
          userAgent += "-chrome";
      } else {
          userAgent += "-unknown";
      }
      let params = 'disabledRules=WHITESPACE_RULE' +   // needed because we might replace quoted text by spaces (see issue #25) 
          '&useragent=' + userAgent;
      Tools.getStorage().get({
          havePremiumAccount: false,
          username: "",
          password: "",
          motherTongue: false,
          enVariant: "en-US",
          deVariant: "de-DE",
          ptVariant: "pt-PT",
          caVariant: "ca-ES",
      }, function(items) {
          const { motherTongue, havePremiumAccount, username, password, enVariant, deVariant, ptVariant, caVariant } = items;
          //console.log("metaData", metaData);
          //console.log("havePremiumAccount", items.havePremiumAccount);
          if (havePremiumAccount) {  // requires LT 3.9 or later
              const json = {text: text, metaData: metaData};
              params += '&data=' + encodeURIComponent(JSON.stringify(json));
          } else {
              params += '&text=' + encodeURIComponent(text);
          }
          if (motherTongue) {
              params += "&motherTongue=" + motherTongue;
          }
          if (typeof manuallySelectedLanguage !== 'undefined' && manuallySelectedLanguage) {
              params += "&language=" + manuallySelectedLanguage;
              manuallySelectedLanguage = "";
          } else {
              params += "&language=auto";<commit_msg>track slow checks in Piwik<commit_after>/* LanguageTool WebExtension
 * Copyright (C) 2016 <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 quotedLinesIgnored = false;
let ignoreQuotedLines = true;

Tools.getStorage().get({
    ignoreQuotedLines: ignoreQuotedLines,
}, function(items) {
  ignoreQuotedLines = items.ignoreQuotedLines;
});

function getCheckResult(markupList, metaData, callback, errorCallback) {
    Tools.getApiServerUrl(serverUrl => {
      const startTime = new Date().getTime();
      let text = Markup.markupList2text(markupList);
      if (ignoreQuotedLines) {
          const textOrig = text;
          // A hack so the following replacements don't happen on messed up character positions.
          // See https://github.com/languagetool-org/languagetool-browser-addon/issues/25:
          text = text.replace(/^>.*?\n/gm, function(match) {
              return " ".repeat(match.length - 1) + "\n";
          });
          quotedLinesIgnored = text != textOrig;
      }
      if (text.trim().length === 0) {
        return callback('{}');
      }
      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;
          }
          if (req.status !== 200) {
              errorCallback(chrome.i18n.getMessage("noValidResponseFromServer", [serverUrl, req.response, req.status]), "noValidResponseFromServer");
              return;
          }
          callback(response);
          const runTime = new Date().getTime() - startTime;
          if (runTime > 3000) {
            const runTimeSecs = (runTime/1000).toFixed(0);
            Tools.track("http://unknown.host", "SlowCheckSeconds", runTimeSecs);
          }
      };
      req.onerror = function() {
          errorCallback(chrome.i18n.getMessage("networkError", serverUrl), "networkError");
      };
      req.ontimeout = function() {
          errorCallback(chrome.i18n.getMessage("timeoutError", serverUrl), "timeoutError");
      };
      let userAgent = "webextension";
      if (Tools.isFirefox()) {
          userAgent += "-firefox";
      } else if (Tools.isChrome()) {
          userAgent += "-chrome";
      } else {
          userAgent += "-unknown";
      }
      let params = 'disabledRules=WHITESPACE_RULE' +   // needed because we might replace quoted text by spaces (see issue #25)
          '&useragent=' + userAgent;
      Tools.getStorage().get({
          havePremiumAccount: false,
          username: "",
          password: "",
          motherTongue: false,
          enVariant: "en-US",
          deVariant: "de-DE",
          ptVariant: "pt-PT",
          caVariant: "ca-ES",
      }, function(items) {
          const { motherTongue, havePremiumAccount, username, password, enVariant, deVariant, ptVariant, caVariant } = items;
          //console.log("metaData", metaData);
          //console.log("havePremiumAccount", items.havePremiumAccount);
          if (havePremiumAccount) {  // requires LT 3.9 or later
              const json = {text: text, metaData: metaData};
              params += '&data=' + encodeURIComponent(JSON.stringify(json));
          } else {
              params += '&text=' + encodeURIComponent(text);
          }
          if (motherTongue) {
              params += "&motherTongue=" + motherTongue;
          }
          if (typeof manuallySelectedLanguage !== 'undefined' && manuallySelectedLanguage) {
              params += "&language=" + manuallySelectedLanguage;
              manuallySelectedLanguage = "";
          } else {
              params += "&language=auto";
{'fraction_non_alphanumeric': 0.1201697565333929, 'fraction_numerical': 0.010609783337056065, 'mean_word_length': 2.479020979020979, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3364204', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2423, 'n_words': 677}