<commit_before>
chrome.runtime.setUninstallURL("https://languagetool.org/webextension/uninstall.php");

function onClickHandler(info, tab) {
  if (chrome && chrome.browserAction && chrome.browserAction.openPopup) {
    if (Tools.isFirefox()) {
      chrome.browserAction.openPopup();
    } else {
      // 'openPopup' is not documented at https://developer.chrome.com/extensions/browserAction,
      // and it's not in Chrome 50 (but in Chromium 49) so we are careful and don't call it if it's not there.
      // Also see https://bugs.chromium.org/p/chromium/issues/detail?id=436489
      chrome.browserAction.openPopup(
        function(popupView) {}
      );
    }
  }
}

/*
This almost works for Firefox, but browser.browserAction.openPopup is missing:
 - https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/BrowserAction/openPopup
 - https://github.com/languagetool-org/languagetool-browser-addon/issues/45
if (browser && browser.browserAction && browser.browserAction.openPopup) {
  browser.contextMenus.create({"title": "FIXME", "contexts":["selection", "editable"], "id": "contextLT"});
}*/

if (chrome && chrome.browserAction && chrome.browserAction.openPopup) {
  chrome.contextMenus.onClicked.addListener(onClickHandler);
  chrome.runtime.onInstalled.addListener(function() {
    chrome.commands.getAll(function(commands) {
      let shortcut = "";
      if (commands && commands.length && commands.length > 0 && commands[0].shortcut) {
        shortcut = commands[0].shortcut;
      }
      // there seems to be no better way to show the shortcut (https://bugs.chromium.org/p/chromium/issues/detail?id=142840):
      const title = shortcut ? chrome.i18n.getMessage("contextMenuItemWithShortcut", shortcut) : chrome.i18n.getMessage("contextMenuItem");
      chrome.contextMenus.create({"title": title, "contexts":["selection", "editable"], "id": "contextLT"});
    });
    // With an entry only for 'editable' we could have a better name, but then Chrome will
    // move both entries into a sub menu, which is very bad for usability, so 'editable' is covered
    // by the entry above instead:
    //chrome.contextMenus.create({"title": "Check text field", "contexts":["editable"], "id": "contextLTeditable"});
  });
}

// Flash the icon as a reminder if the user hasn't used this extension for a long time.
/*function checkUsage() {
  var storage = chrome.storage.sync ? chrome.storage.sync : chrome.storage.local;
  storage.get({
    lastCheck: null
  }, function(items) {
    let now = new Date().getTime();
    let diffSeconds = (now - items.lastCheck) / 1000;
    let diffHours = diffSeconds / 60 / 60;
    //console.log("lastCheck:" + items.lastCheck + ", diffSeconds: " + diffSeconds + ", diffHours: " + diffHours);
    if (diffHours > 7*24) {  // TODO: make sure to not repeat the warning for n hours & only show if user is typing
      flashIcon(3);
    }
    setTimeout(function() {checkUsage()}, 10000);
  });
}

function flashIcon(times) {
  if (times <= 0) {
    return;
  }
  setTimeout(function() {
    chrome.browserAction.setIcon({path: "images/icon48-highlight.png"});
    setTimeout(function () {
      chrome.browserAction.setIcon({path: "images/icon48.png"});
      flashIcon(times - 1);
    }, 500);
  }, 500);
}

checkUsage();
*/

/* workaround handle for FF */
chrome.runtime.onMessage.addListener(handleMessage);
function handleMessage(request, sender, sendResponse) {
  switch (request.action) {
    case "openNewTab": {
      const { url } = request;
      chrome.tabs.create({ url });
      return false;
    }
    case "getActiveTab": {
      chrome.tabs.query(
        {
          active: true,
          currentWindow: true
        },
        tabs => {
          sendResponse({
            action: request.action,
            tabs
          });
        }
      );
      return true;
    }
    default: {
      if (request.tabId) {
        // proxy msg from cs -> bg -> cs
        chrome.tabs.sendMessage(request.tabId, request, response => {
          sendResponse(response);
        });
        return true;
      }
      // TODO: handle for unknown action
      sendResponse({
        action: `unknow ${request.action}`
      });
      return false;
    }
  }
}

chrome.runtime.onConnect.addListener(function(port) {
  console.assert(port.name == "LanguageTool");
  port.onMessage.addListener((msg) => {
    if (msg.action == "checkText") {
      const { markupList, metaData  } = msg.data;
      getCheckResult(markupList, metaData, response => {
        port.postMessage({
          action: 'checkText',
          success: true,
          result: JSON.parse(response)
        });
      }, (errorMessage) => {
        console.warn('found error', errorMessage);
        port.postMessage({
          action: 'checkText',
          success: false,
          errorMessage
        });
      })
    }
  });
});
<commit_msg>remove old code that was already commented out<commit_after>
chrome.runtime.setUninstallURL("https://languagetool.org/webextension/uninstall.php");

function onClickHandler(info, tab) {
  if (chrome && chrome.browserAction && chrome.browserAction.openPopup) {
    if (Tools.isFirefox()) {
      chrome.browserAction.openPopup();
    } else {
      // 'openPopup' is not documented at https://developer.chrome.com/extensions/browserAction,
      // and it's not in Chrome 50 (but in Chromium 49) so we are careful and don't call it if it's not there.
      // Also see https://bugs.chromium.org/p/chromium/issues/detail?id=436489
      chrome.browserAction.openPopup(
        function(popupView) {}
      );
    }
  }
}

if (chrome && chrome.browserAction && chrome.browserAction.openPopup) {
  chrome.contextMenus.onClicked.addListener(onClickHandler);
  chrome.runtime.onInstalled.addListener(function() {
    chrome.commands.getAll(function(commands) {
      let shortcut = "";
      if (commands && commands.length && commands.length > 0 && commands[0].shortcut) {
        shortcut = commands[0].shortcut;
      }
      // there seems to be no better way to show the shortcut (https://bugs.chromium.org/p/chromium/issues/detail?id=142840):
      const title = shortcut ? chrome.i18n.getMessage("contextMenuItemWithShortcut", shortcut) : chrome.i18n.getMessage("contextMenuItem");
      chrome.contextMenus.create({"title": title, "contexts":["selection", "editable"], "id": "contextLT"});
    });
    // With an entry only for 'editable' we could have a better name, but then Chrome will
    // move both entries into a sub menu, which is very bad for usability, so 'editable' is covered
    // by the entry above instead:
    //chrome.contextMenus.create({"title": "Check text field", "contexts":["editable"], "id": "contextLTeditable"});
  });
}

/* workaround handle for FF */
chrome.runtime.onMessage.addListener(handleMessage);
function handleMessage(request, sender, sendResponse) {
  switch (request.action) {
    case "openNewTab": {
      const { url } = request;
      chrome.tabs.create({ url });
      return false;
    }
    case "getActiveTab": {
      chrome.tabs.query(
        {
          active: true,
          currentWindow: true
        },
        tabs => {
          sendResponse({
            action: request.action,
            tabs
          });
        }
      );
      return true;
    }
    default: {
      if (request.tabId) {
        // proxy msg from cs -> bg -> cs
        chrome.tabs.sendMessage(request.tabId, request, response => {
          sendResponse(response);
        });
        return true;
      }
      // TODO: handle for unknown action
      sendResponse({
        action: `unknow ${request.action}`
      });
      return false;
    }
  }
}

chrome.runtime.onConnect.addListener(function(port) {
  console.assert(port.name == "LanguageTool");
  port.onMessage.addListener((msg) => {
    if (msg.action == "checkText") {
      const { markupList, metaData  } = msg.data;
      getCheckResult(markupList, metaData, response => {
        port.postMessage({
          action: 'checkText',
          success: true,
          result: JSON.parse(response)
        });
      }, (errorMessage) => {
        console.warn('found error', errorMessage);
        port.postMessage({
          action: 'checkText',
          success: false,
          errorMessage
        });
      })
    }
  });
});

{'fraction_non_alphanumeric': 0.1385078219013237, 'fraction_numerical': 0.009265944645006017, 'mean_word_length': 2.7319263583295914, 'pattern_counts': {'":': 19, '<': 4, '<?xml version=': 0, '>': 20, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, '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': '2949836', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2427, 'n_words': 563}