<commit_before>/**
 * JavaScript Bookmarklet Code Version 0.1A
 * Raises a div above the existing page, for the user to add notes to the
 * charting system.
 *
 * TODO:
 * - Make first 140 characters of Body red.
 * - Handle login to charting server if session does not exist.
 * - Use API key.
 * - Handle no-goals.
 * - Extend use of JS and DOM namespace to avoid collisions with existing script
 *   or HTML.
 * - Fix layout widths: why does it work in some cases (e.g.
 *   http://chinesefood.about.com) and not others?
 * - Stop original page CSS polluting form (e.g. http://www.ducea.com)
 * - Make generally more visually appealing.
 * - Clean up this code into a single charting class/object.
 */

var serverLocation = 'http://<%=request.host%>:<%=request.port%>';
var goalsScript = serverLocation + "/goals.js";
var div = document.createElement('div');
var body = document.getElementsByTagName('body')[0];
// stubbed, for now.
var name = "fred";
//var key = "";

var charting =
{
  theGoals: null,
  setGoals: function (theGoals)
  {
    this.theGoals = theGoals;
  }
}

function mhnChartingMain()
{
  var goals = getGoals();
  // execution is continued at getGoalsBottom
}

/*
 * Retrieves the goals for this user, as an array of indexed strings.
 */
function getGoals()
{
  runScript(goalsScript, getGoalsBottom);
}

/*
 * Callback to be called once goals have been retrieved via JSONP. Finishes the
 * initialisation of the form.
 */
function getGoalsBottom()
{
  setForm(charting.theGoals);
  body.appendChild(div);

  var formBodyField = document.getElementsByName('note[body]')[0];
  formBodyField.value = getSelectedText();

}

/*
 * runs a script as specified.
 */
function runScript(src, callbackFunc)
{
  var retValue = null;
  // HTML Elements for injection.
  var head=document.getElementsByTagName('head')[0];  // doc head
  var script = document.createElement('script');      // script element
  // MHN Charting Script Source -- script to inject.
  script.src=src;
  // set to true once script init is complete, to guard re-init.
  var initComplete=false;
  // function called to initialise script after injection.
  var initFunc = 
    function()
    {
      if
        (
         !initComplete &&
         (
          !this.readyState ||
          // different readyState states for different browsers
          this.readyState == 'loaded' ||
          this.readyState == 'complete'
         )
        )
        {
          // avoid multiple triggerings due to races.
          initComplete=true;
          callbackFunc();
          // avoid re-init by removing reference to this func.
          script.onload = null;
          script.onreadystatechange = null;
          // remove init script element.
          head.removeChild(script);
        }
    };
  script.onload = initFunc;
  script.onreadystatechange = initFunc;

  // add script to head of page.
  head.appendChild(script);
  return retValue;
}


/**
 * Initialise form for div.
 */
function setForm(goals)
{
  div.style.width =           "310px";
  div.style.position =        "fixed";
  div.style.backgroundColor = "#216CF7";     // grey
  div.style.zIndex =          "1337";     // on top of other DIVs
  div.style.top =             "0";        // at top of the screen
  div.style.right =           "0";        // and the far right
  div.style.textAlign =       "left";     // inner elements LHS aligned
  div.style.border =          "1px solid black";
  div.style.padding =         "10px";
  div.style.font =            "10pt Sans-Serif";
  div.style.color =           "white";
  div.style.overflow =        "auto";
  div.innerHTML =
    "<form id=\"note_add_form\" name=\"note_add_form\" action=\"" + serverLocation + "/notes\" " +
    "method=\"post\" target=\"_blank\" onsubmit=\"setTimeout('cancel()', 200); return true\">" +

    "<input type=\"hidden\" name=\"user[name]\" value=\"" + name + "\" />" +

    //"<input type=\"hidden\" name=\"note[key]\" value=\"" + key + "\" />" +

    "<input type=\"hidden\" name=\"source[location]\" value=\"" +
    document.location.href + "\" />" +

    "<input type=\"hidden\" name=\"source[doctype]\" value=\"webpage\" />" +

    "<strong>Title</strong><br/>" +
    "<input type=\"text\" name=\"source[title]\" value=\"" +
    document.title +
    "\" style=\"border: 1px solid black; font: 10pt Sans-Serif; width: 300px\" " +
    "/><br />" +

    "<strong>Content</strong><br/>" +
    "<textarea name=\"note[body]\" " +
    "style=\"border: 1px solid black; font: 10pt Sans-Serif; width: 300px; " +
    "height: 100px\"></textarea><br />" +

    getGoalSelect(goals) + "<br /><br />" +
    "<div id=\"added_goals\"><strong>Goals:</strong><br/></div><br />" +

    "<center>" +
    "<input type=\"submit\" value=\"Submit\"/>&nbsp;" +
    "<input type=\"button\" value=\"Reset\" " +
    "onclick=\"mhnChartingMain();\"/>&nbsp;" +
    "<input type=\"button\" value=\"Cancel\" " +
    "onclick=\"cancel();\">" + 
    "</center>" +
    
    "</form>";
}

/**
 * Removes the div from the body, hiding the form. The form is effectively reset
 * at this point, since it will be re-initialised when the bookmarklet is
 * relaunched.
 */
function cancel()
{
  body.removeChild(div);
}<commit_msg>Trying with content.document.title rather than document.title to see if it will work in chrome.<commit_after>/**
 * JavaScript Bookmarklet Code Version 0.1A
 * Raises a div above the existing page, for the user to add notes to the
 * charting system.
 *
 * TODO:
 * - Make first 140 characters of Body red.
 * - Handle login to charting server if session does not exist.
 * - Use API key.
 * - Handle no-goals.
 * - Extend use of JS and DOM namespace to avoid collisions with existing script
 *   or HTML.
 * - Fix layout widths: why does it work in some cases (e.g.
 *   http://chinesefood.about.com) and not others?
 * - Stop original page CSS polluting form (e.g. http://www.ducea.com)
 * - Make generally more visually appealing.
 * - Clean up this code into a single charting class/object.
 */

var serverLocation = 'http://<%=request.host%>:<%=request.port%>';
var goalsScript = serverLocation + "/goals.js";
var div = document.createElement('div');
var body = document.getElementsByTagName('body')[0];
// stubbed, for now.
var name = "fred";
//var key = "";

var charting =
{
  theGoals: null,
  setGoals: function (theGoals)
  {
    this.theGoals = theGoals;
  }
}

function mhnChartingMain()
{
  var goals = getGoals();
  // execution is continued at getGoalsBottom
}

/*
 * Retrieves the goals for this user, as an array of indexed strings.
 */
function getGoals()
{
  runScript(goalsScript, getGoalsBottom);
}

/*
 * Callback to be called once goals have been retrieved via JSONP. Finishes the
 * initialisation of the form.
 */
function getGoalsBottom()
{
  setForm(charting.theGoals);
  body.appendChild(div);

  var formBodyField = document.getElementsByName('note[body]')[0];
  formBodyField.value = getSelectedText();

}

/*
 * runs a script as specified.
 */
function runScript(src, callbackFunc)
{
  var retValue = null;
  // HTML Elements for injection.
  var head=document.getElementsByTagName('head')[0];  // doc head
  var script = document.createElement('script');      // script element
  // MHN Charting Script Source -- script to inject.
  script.src=src;
  // set to true once script init is complete, to guard re-init.
  var initComplete=false;
  // function called to initialise script after injection.
  var initFunc = 
    function()
    {
      if
        (
         !initComplete &&
         (
          !this.readyState ||
          // different readyState states for different browsers
          this.readyState == 'loaded' ||
          this.readyState == 'complete'
         )
        )
        {
          // avoid multiple triggerings due to races.
          initComplete=true;
          callbackFunc();
          // avoid re-init by removing reference to this func.
          script.onload = null;
          script.onreadystatechange = null;
          // remove init script element.
          head.removeChild(script);
        }
    };
  script.onload = initFunc;
  script.onreadystatechange = initFunc;

  // add script to head of page.
  head.appendChild(script);
  return retValue;
}


/**
 * Initialise form for div.
 */
function setForm(goals)
{
  div.style.width =           "310px";
  div.style.position =        "fixed";
  div.style.backgroundColor = "#216CF7";     // grey
  div.style.zIndex =          "1337";     // on top of other DIVs
  div.style.top =             "0";        // at top of the screen
  div.style.right =           "0";        // and the far right
  div.style.textAlign =       "left";     // inner elements LHS aligned
  div.style.border =          "1px solid black";
  div.style.padding =         "10px";
  div.style.font =            "10pt Sans-Serif";
  div.style.color =           "white";
  div.style.overflow =        "auto";
  div.innerHTML =
    "<form id=\"note_add_form\" name=\"note_add_form\" action=\"" + serverLocation + "/notes\" " +
    "method=\"post\" target=\"_blank\" onsubmit=\"setTimeout('cancel()', 200); return true\">" +

    "<input type=\"hidden\" name=\"user[name]\" value=\"" + name + "\" />" +

    //"<input type=\"hidden\" name=\"note[key]\" value=\"" + key + "\" />" +

    "<input type=\"hidden\" name=\"source[location]\" value=\"" +
    document.location.href + "\" />" +

    "<input type=\"hidden\" name=\"source[doctype]\" value=\"webpage\" />" +

    "<strong>Title</strong><br/>" +
    "<input type=\"text\" name=\"source[title]\" value=\"" +
    content.document.title +
    "\" style=\"border: 1px solid black; font: 10pt Sans-Serif; width: 300px\" " +
    "/><br />" +

    "<strong>Content</strong><br/>" +
    "<textarea name=\"note[body]\" " +
    "style=\"border: 1px solid black; font: 10pt Sans-Serif; width: 300px; " +
    "height: 100px\"></textarea><br />" +

    getGoalSelect(goals) + "<br /><br />" +
    "<div id=\"added_goals\"><strong>Goals:</strong><br/></div><br />" +

    "<center>" +
    "<input type=\"submit\" value=\"Submit\"/>&nbsp;" +
    "<input type=\"button\" value=\"Reset\" " +
    "onclick=\"mhnChartingMain();\"/>&nbsp;" +
    "<input type=\"button\" value=\"Cancel\" " +
    "onclick=\"cancel();\">" + 
    "</center>" +
    
    "</form>";
}

/**
 * Removes the div from the body, hiding the form. The form is effectively reset
 * at this point, since it will be re-initialised when the bookmarklet is
 * relaunched.
 */
function cancel()
{
  body.removeChild(div);
}
{'fraction_non_alphanumeric': 0.15818940975517176, 'fraction_numerical': 0.008350730688935281, 'mean_word_length': 2.6055422511118715, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 67, 'https://': 0, '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': '2118082', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3247, 'n_words': 911}