<commit_before>///////////////////////// Component-global "Constants" /////////////////////////

var DESCRIPTION = "GM_GreasemonkeyService";
var CONTRACTID = "@greasemonkey.mozdev.org/greasemonkey-service;1";
var CLASSID = Components.ID("{77bf3650-1cd6-11da-8cd6-0800200c9a66}");

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var gmRunScriptFilename = "resource://greasemonkey/runScript.js";
var gExtensionPath = (function() {
  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
      .getService(Components.interfaces.nsIIOService);
  if ('jar:' == Components.stack.filename.substr(0, 4)) {
    // Unpacked XPI case.
    return Components.stack.filename.replace(/\!\/.*/, '');
  } else if ('file:' == Components.stack.filename.substr(0, 5)){
    // Raw file, development case.
    // Turn the file:/// URL into an nsIFile ...
    var uri = ioService.newURI(Components.stack.filename, null, null);
    var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
    // ... to find the containing directory.
    var dir = file.parent.parent;
    // Then get the URL back for that path.
    return ioService.newFileURI(dir).spec;
  } else {
    throw Error('Could not detect gExtensionPath!');
  }
})();

// Only a particular set of strings are allowed.  See: http://goo.gl/ex2LJ
var gMaxJSVersion = "ECMAv5";

var gMenuCommands = [];
var gStartupHasRun = false;

var gFileProtocolHandler = Components
    .classes["@mozilla.org/network/protocol;1?name=file"]
    .getService(Ci.nsIFileProtocolHandler);
var gTmpDir = Components.classes["@mozilla.org/file/directory_service;1"]
    .getService(Components.interfaces.nsIProperties)
    .get("TmpD", Components.interfaces.nsILocalFile);

/////////////////////// Component-global Helper Functions //////////////////////

// TODO: Remove this, see #1318.
function alert(msg) {
  Cc["@mozilla.org/embedcomp/prompt-service;1"]
    .getService(Ci.nsIPromptService)
    .alert(null, "Greasemonkey alert", msg);
}

function anonWrap(aSource) {
  return ['(function(){', aSource, '})()'].join('');
}

// Examines the stack to determine if an API should be callable.
function GM_apiLeakCheck(apiName) {
  var stack = Components.stack;

  do {
    // Valid locations for GM API calls are:
    //  * Greasemonkey modules.
    //  * All of chrome.  (In the script update case, chrome will list values.)
    //  * Greasemonkey extension by path. (FF 3 does this instead of the above.)
    // Anything else on the stack and we will reject the API, to make sure that
    // the content window (whose path would be e.g. http://...) has no access.
    if (2 == stack.language
        && stack.filename.substr(0, 24) !== 'resource://greasemonkey/'
        && stack.filename.substr(0, 9) !== 'chrome://'
        && stack.filename.substr(0, gExtensionPath.length) !== gExtensionPath
        ) {
      GM_util.logError(new Error("Greasemonkey access violation: " +
          "unsafeWindow cannot call " + apiName + "."));
      return false;
    }

    stack = stack.caller;
  } while (stack);

  return true;
}

function createSandbox(
    aScript, aContentWin, aChromeWin, aFirebugConsole, aUrl
) {
  var sandbox = new Components.utils.Sandbox(
      aContentWin,
      {
        'sandboxName': aScript.id,
        'sandboxPrototype': aContentWin,
        'wantXrays': true,
      });
  sandbox.unsafeWindow = aContentWin.wrappedJSObject;
  if (aFirebugConsole) sandbox.console = aFirebugConsole;

  // Temporary workaround for #1318.  TODO: Remove when upstream bug fixed.
  sandbox.alert = alert;

  var imp = sandbox.importFunction;
  if (GM_util.inArray(aScript.grants, 'GM_addStyle')) {
    imp(function(css) { GM_addStyle(aContentWin.document, css); },
        'GM_addStyle');
  }<commit_msg>Apply alert workaround only to broken Firefoxen.<commit_after>///////////////////////// Component-global "Constants" /////////////////////////

var DESCRIPTION = "GM_GreasemonkeyService";
var CONTRACTID = "@greasemonkey.mozdev.org/greasemonkey-service;1";
var CLASSID = Components.ID("{77bf3650-1cd6-11da-8cd6-0800200c9a66}");

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var gmRunScriptFilename = "resource://greasemonkey/runScript.js";
var gExtensionPath = (function() {
  var ioService = Components.classes["@mozilla.org/network/io-service;1"]
      .getService(Components.interfaces.nsIIOService);
  if ('jar:' == Components.stack.filename.substr(0, 4)) {
    // Unpacked XPI case.
    return Components.stack.filename.replace(/\!\/.*/, '');
  } else if ('file:' == Components.stack.filename.substr(0, 5)){
    // Raw file, development case.
    // Turn the file:/// URL into an nsIFile ...
    var uri = ioService.newURI(Components.stack.filename, null, null);
    var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
    // ... to find the containing directory.
    var dir = file.parent.parent;
    // Then get the URL back for that path.
    return ioService.newFileURI(dir).spec;
  } else {
    throw Error('Could not detect gExtensionPath!');
  }
})();

// Only a particular set of strings are allowed.  See: http://goo.gl/ex2LJ
var gMaxJSVersion = "ECMAv5";

var gMenuCommands = [];
var gStartupHasRun = false;

var gFileProtocolHandler = Components
    .classes["@mozilla.org/network/protocol;1?name=file"]
    .getService(Ci.nsIFileProtocolHandler);
var gTmpDir = Components.classes["@mozilla.org/file/directory_service;1"]
    .getService(Components.interfaces.nsIProperties)
    .get("TmpD", Components.interfaces.nsILocalFile);

/////////////////////// Component-global Helper Functions //////////////////////

// TODO: Remove this, see #1318.
function alert(msg) {
  Cc["@mozilla.org/embedcomp/prompt-service;1"]
    .getService(Ci.nsIPromptService)
    .alert(null, "Greasemonkey alert", msg);
}

function anonWrap(aSource) {
  return ['(function(){', aSource, '})()'].join('');
}

// Examines the stack to determine if an API should be callable.
function GM_apiLeakCheck(apiName) {
  var stack = Components.stack;

  do {
    // Valid locations for GM API calls are:
    //  * Greasemonkey modules.
    //  * All of chrome.  (In the script update case, chrome will list values.)
    //  * Greasemonkey extension by path. (FF 3 does this instead of the above.)
    // Anything else on the stack and we will reject the API, to make sure that
    // the content window (whose path would be e.g. http://...) has no access.
    if (2 == stack.language
        && stack.filename.substr(0, 24) !== 'resource://greasemonkey/'
        && stack.filename.substr(0, 9) !== 'chrome://'
        && stack.filename.substr(0, gExtensionPath.length) !== gExtensionPath
        ) {
      GM_util.logError(new Error("Greasemonkey access violation: " +
          "unsafeWindow cannot call " + apiName + "."));
      return false;
    }

    stack = stack.caller;
  } while (stack);

  return true;
}

function createSandbox(
    aScript, aContentWin, aChromeWin, aFirebugConsole, aUrl
) {
  var sandbox = new Components.utils.Sandbox(
      aContentWin,
      {
        'sandboxName': aScript.id,
        'sandboxPrototype': aContentWin,
        'wantXrays': true,
      });
  sandbox.unsafeWindow = aContentWin.wrappedJSObject;
  if (aFirebugConsole) sandbox.console = aFirebugConsole;

  if (GM_util.compareFirefoxVersion("16.0") < 0) {
    // See #1350.  The upstream bug was fixed in Firefox 16; apply workaround
    // only in older versions.
    sandbox.alert = alert;
  }

  var imp = sandbox.importFunction;
  if (GM_util.inArray(aScript.grants, 'GM_addStyle')) {
    imp(function(css) { GM_addStyle(aContentWin.document, css); },
        'GM_addStyle');
  }
{'fraction_non_alphanumeric': 0.15830460870675347, 'fraction_numerical': 0.01327716073024384, 'mean_word_length': 3.116657908565423, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3797725', 'n_tokens_mistral': 2569, 'n_tokens_neox': 2463, 'n_words': 599}