<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const widgets = require("widget");
const data = require("self").data;
const tabs = require("tabs");
const { StartupPanel } = require("./startup-panel");
const { Page } = require("page-worker");
const simplePrefs = require('simple-prefs');

var button = widgets.Widget({
  id: "towtruck-starter",
  label: "Start TowTruck",
  contentURL: data.url("button.html"),
  contentScriptFile: data.url("button.js"),
  onClick: function () {
    startTowTruck();
  },
  width: 48
});

StartupPanel({
  name: "TowTruck",
  contentURL: data.url("startup-help.html")
});

var autoDomains = [];
function updateAutoDomains() {
  var domains = simplePrefs.prefs.autoDomains;
  domains = domains.split(/,/g);
  autoDomains = [];
  domains.forEach(function (item) {
    item = item.replace(/^\s+/, "").replace(/\s+$/, "");
    item = item.split(/;/);
    var domain = item[0];
    var shareId = item[1] || null;
    if (domain.indexOf("//") == -1) {
      // Just a plain domain
      domain = "^https?:\\/\\/" + domain;
    }
    domain = new RegExp(domain, "i");
    autoDomains.push({domain: domain, shareId: shareId});
  });
}
simplePrefs.on("autoDomains", updateAutoDomains);
updateAutoDomains();


function startTowTruck(shareId) {
  var tab = tabs.activeTab;
  if (tab.towtruckCloser) {
    tab.towtruckCloser();
    return;
  }
  tab.towtruckCloser = function () {
    tab.towtruckCloser = null;
    button.port.emit("TowTruckOff");
    tab.removeListener("ready", attachWorker);
  };
  var worker;
  function attachWorker() {
    worker = tab.attach({
      contentScriptFile: [
        data.url("attachment.js")
      ]
    });
    worker.port.on("Close", function () {
      tab.towtruckCloser();
    });
    worker.port.emit("Config", {url: simplePrefs.prefs.towtruckJs, hubBase: simplePrefs.prefs.hubBase, shareId: shareId || null});
  }
  button.port.emit("TowTruckOn");
  tab.on("ready", attachWorker);
  attachWorker();
}
// Need poll for back button code

tabs.on("open", watchTab);

function watchTab(tab) {
  tab.on("ready", function () {
    if (tabs.activeTab == tab && tab.url.indexOf("#&towtruck") != -1) {
      startTowTruck();
    }
    if (tabs.activeTab == tab && ! tab.towtruckCloser) {
      var started = false;
      autoDomains.forEach(function (matcher) {
        if ((! started) && tab.url.search(matcher.domain) != -1) {
          startTowTruck(matcher.shareId);
          started = true;
        }
      });
    }
  });
}

for (var i=0; i<tabs.length; i++) {
  watchTab(tabs[i]);<commit_msg>Fix one problem related to #417, avoid starting up TowTruck on every tab when autoDomains isn't set<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const widgets = require("widget");
const data = require("self").data;
const tabs = require("tabs");
const { StartupPanel } = require("./startup-panel");
const { Page } = require("page-worker");
const simplePrefs = require('simple-prefs');

var button = widgets.Widget({
  id: "towtruck-starter",
  label: "Start TowTruck",
  contentURL: data.url("button.html"),
  contentScriptFile: data.url("button.js"),
  onClick: function () {
    console.log("Starting TowTruck because of click");
    startTowTruck();
  },
  width: 48
});

StartupPanel({
  name: "TowTruck",
  contentURL: data.url("startup-help.html")
});

var autoDomains = [];
function updateAutoDomains() {
  var domains = simplePrefs.prefs.autoDomains;
  domains = domains.split(/,/g);
  autoDomains = [];
  domains.forEach(function (item) {
    if (item.search(/^\s*$/) === 0) {
      return;
    }
    item = item.replace(/^\s+/, "").replace(/\s+$/, "");
    item = item.split(/;/);
    var domain = item[0];
    var shareId = item[1] || null;
    if (domain.indexOf("//") == -1) {
      // Just a plain domain
      domain = "^https?:\\/\\/" + domain;
    }
    domain = new RegExp(domain, "i");
    autoDomains.push({domain: domain, shareId: shareId});
  });
}
simplePrefs.on("autoDomains", updateAutoDomains);
updateAutoDomains();


function startTowTruck(shareId) {
  var tab = tabs.activeTab;
  if (tab.towtruckCloser) {
    tab.towtruckCloser();
    return;
  }
  tab.towtruckCloser = function () {
    tab.towtruckCloser = null;
    button.port.emit("TowTruckOff");
    tab.removeListener("ready", attachWorker);
  };
  var worker;
  function attachWorker() {
    worker = tab.attach({
      contentScriptFile: [
        data.url("attachment.js")
      ]
    });
    worker.port.on("Close", function () {
      tab.towtruckCloser();
    });
    worker.port.emit("Config", {url: simplePrefs.prefs.towtruckJs, hubBase: simplePrefs.prefs.hubBase, shareId: shareId || null});
  }
  button.port.emit("TowTruckOn");
  tab.on("ready", attachWorker);
  attachWorker();
}
// Need poll for back button code

tabs.on("open", watchTab);

function watchTab(tab) {
  tab.on("ready", function () {
    if (tabs.activeTab == tab && tab.url.indexOf("#&towtruck") != -1) {
      console.log("Starting TowTruck on share link", tab.url);
      startTowTruck();
    }
    if (tabs.activeTab == tab && ! tab.towtruckCloser) {
      var started = false;
      autoDomains.forEach(function (matcher) {
        console.log("matcher", matcher, matcher.domain);
        if ((! started) && tab.url.search(matcher.domain) != -1) {
          console.log("Start TowTruck autoDomain");
          startTowTruck(matcher.shareId);
          started = true;
        }
      });
    }
  });
}

for (var i=0; i<tabs.length; i++) {
  watchTab(tabs[i]);
{'fraction_non_alphanumeric': 0.17304064482936032, 'fraction_numerical': 0.004801920768307323, 'mean_word_length': 3.235294117647059, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6767870', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1963, 'n_words': 434}