harvesthq / platform

A whole new way to add time tracking to your web application.
https://www.getharvest.com/platform
59 stars 11 forks source link

Have to clear cache and reload page to make timers visible #12

Closed veevandyke closed 10 years ago

veevandyke commented 10 years ago

I have a tampermonkey script I have written for Chrome. In order for the timers to show, I have to clear my browser cache and reload my page. Every time.

Any idea why this would be happening or how I can prevent it?

There are no console errors and I have a button to loadTimers that manually calls the timers:add function, and it's triggering, but still no graphic.

adunkman commented 10 years ago

@veevandyke sounds like it might be a load-order problem — maybe the platform script is loading too quickly when it’s cached?

Do you mind copying your tampermonkey script in here so I can take a look?

veevandyke commented 10 years ago

No amount of refreshing will make it work, however... if I go in and clear the cache, it loads perfectly on the next refresh. and stays loaded until I refresh the page again. Here you go..... Let me know what I could do differently. (As a side note: works without a hitch in Greasemonkey.)

// ==UserScript== // @name RTM Harvest Script // @namespace http://www.rememberthemilk.com/home/ // @include http://www.rememberthemilk.com/home/ // @include http://www.rememberthemilk.com/home/ // @include https://www.rememberthemilk.com/home/ // @include http://_.www.rememberthemilk.com/home/_ // @include https://_.www.rememberthemilk.com/home/_ // @version 1 // @grant none // ==/UserScript==

(function() { window._harvestPlatformConfig = { "applicationName": "Remember The Milk Link", "permalink": " http://rememberthemilk.com/home/veevandyke/#section.tasks/%ITEM_ID%" }; var s = document.createElement('script'); s.src = '//platform.harvestapp.com/assets/platform.js'; s.async = true; var ph = document.getElementsByTagName('script')[0]; ph.parentNode.insertBefore(s, ph); })();

function contentEval(source) { // Check for function input. if ('function' == typeof source) { // Execute this function with no arguments, by adding parentheses. // One set around the function, required for valid syntax, and a // second empty set calls the surrounded function. source = '(' + source + ')();' }

// Create a script node holding this source code. var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = source;

// Insert the script node into the page, so it will run, and immediately // remove it to clean up. document.body.appendChild(script); document.body.removeChild(script); }

var TurnListNameIntoLink = function () { ListenForDetails(false);

// run in page context to access globals ie stateMgr contentEval(function () { if (typeof stateMgr != 'object') { return; } var listSpan = document.getElementById("detailslist_span"); var href = window.location.pathname; var task_id = href.substr(href.lastIndexOf('/') + 1); console.error (href); console.error (task_id);

if (listSpan) {
  var listName = listSpan.innerHTML;
    var tags = document.getElementById("detailstags_span");
    if (tags){
        console.error ("tags");
        console.error(tags.childNodes.length);
  for (var i = 0; i < tags.childNodes.length; i++)
  {
      console.error (tags.childNodes[i].innerHTML);
      console.error (tags.childNodes[i].innerHTML.indexOf('¶'));
      if (tags.childNodes[i].innerHTML.indexOf('-') != 0)
      {
          var str = tags.childNodes[i].innerHTML.split("-");
          listName = str[str.length - 1];
      }
  }
    }
  var list_id = 0;
  var task = document.getElementById("detailstitle_span");
  var taskName = task.innerHTML;
  //var task_id = 0;

  console.error ("1. " + taskName + ": " + listName);
  var task_text_node = document.getElementById("taskdetails");

  if (task_text_node.lastChild.id == 'harvest_id') {
      info_span = task_text_node.lastChild;
      while (info_span.hasChildNodes()) {
          info_span.removeChild(info_span.firstChild);
      }
  }
  else {
      info_span = document.createElement("div");
      info_span.id = 'harvest_id';

      task_text_node.appendChild(info_span);
  }
    info_span.style.cssFloat = 'right';
          info_span.style.height = '25px';
          info_span.style.width = '25px';
    info_span.padding = '1px';

    console.error ("2. " + taskName + ": " + listName);
    var sHarvest = '<div class="harvest-timer"' +
        'data-project=\'\{"id":"' + 0 + '","name":"' + listName +

'"}\'' + 'data-item=\'{"id":"' + task_id + '","name":"' + taskName + '"}\'>' + '

'; harvestSpan = document.createElement("div"); harvestSpan.innerHTML = sHarvest; info_span.appendChild(harvestSpan); } });

loadTimers();
ListenForDetails(true);

}

var ListenForDetails = function (listen) { var listName = document.getElementById("tasks"); if (listName) { if (listen) { listName.addEventListener("click", TurnListNameIntoLink, false); } else { listName.removeEventListener("click", TurnListNameIntoLink, false); } } }

window.addEventListener('unload', function () { ListenForDetails(false) }, false);

var loadTimers = function () { $(".harvest-timer").each(function (i, obj) { var harvestEvents = { type: "harvest-event:timers:add", element: $(this) } $("#harvest-messaging").trigger(harvestEvents); }); }

/* ENABLE FEATURES */

ListenForDetails(true); //ListenForTasks(true);

On Tue, May 20, 2014 at 2:33 PM, Andrew Dunkman notifications@github.comwrote:

@veevandyke https://github.com/veevandyke sounds like it might be a load-order problem — maybe the platform script is loading too quickly when it’s cached?

Do you mind copying your tampermonkey script in here so I can take a look?

— Reply to this email directly or view it on GitHubhttps://github.com/harvesthq/platform/issues/12#issuecomment-43665412 .

Veronica G. Van Dyke Leadership Simplified | DVD Consulting Incorporated office: 941.776.1121 | mobile: 727.871.0434 | www.leadershipsimplified.com

[image: Leadership Simplified Inc.]

This email message and any attachment(s) are confidential information intended only for the person(s) named above. If you have received this transmission in error, please notify us immediately at the above phone number and destroy the original message and all copies.

grant commented 10 years ago

Please use ``` when commenting code.

veevandyke commented 10 years ago

@grant Are you referring to the GitHub interface for commenting code or within my code itself. As mentioned above, it works fine once the cache is cleared.

Using ``` doesn't actually comment out the code in Tampermonkey. Can you elaborate? Thanks.

prsimp commented 10 years ago

@veevandyke I believe @grant meant that when including code snippets in GitHub comments, it's best to use Markdown to style the snippet.

For shorter snippets, use the inline style by surrounding the snippet in backticks (`).

For longer snippets, and to take advantage of syntax highlighting, use a fenced code block by surrounding the entire snippet with triple-backticks. GitHub will try and guess the syntax or you can designate it. For more info check out the GitHub Flavored Markdown guide. Your snippet in a fenced code block:

// ==UserScript==
// @name        RTM Harvest Script
// @namespace   http://www.rememberthemilk.com/home/*
// @include     http://www.rememberthemilk.com/home/*
// @include     http://www.rememberthemilk.com/home/*
// @include     https://www.rememberthemilk.com/home/*
// @include     http://*.www.rememberthemilk.com/home/*
// @include     https://*.www.rememberthemilk.com/home/*
// @version     1
// @grant       none
// ==/UserScript==

(function() {
        window._harvestPlatformConfig = {
          "applicationName": "Remember The Milk Link",
          "permalink": "
http://rememberthemilk.com/home/veevandyke/#section.tasks/%ITEM_ID%"
        };
        var s = document.createElement('script');
        s.src = '//platform.harvestapp.com/assets/platform.js';
        s.async = true;
        var ph = document.getElementsByTagName('script')[0];
        ph.parentNode.insertBefore(s, ph);
      })();

function contentEval(source) {
  // Check for function input.
  if ('function' == typeof source) {
    // Execute this function with no arguments, by adding parentheses.
    // One set around the function, required for valid syntax, and a
    // second empty set calls the surrounded function.
    source = '(' + source + ')();'
  }

  // Create a script node holding this  source code.
  var script = document.createElement('script');
  script.setAttribute("type", "application/javascript");
  script.textContent = source;

  // Insert the script node into the page, so it will run, and immediately
  // remove it to clean up.
  document.body.appendChild(script);
  document.body.removeChild(script);
}

var TurnListNameIntoLink = function () {
  ListenForDetails(false);

  // run in page context to access globals ie stateMgr
  contentEval(function () {
    if (typeof stateMgr != 'object') {
      return;
    }
    var listSpan = document.getElementById("detailslist_span");
    var href = window.location.pathname;
    var task_id = href.substr(href.lastIndexOf('/') + 1);
console.error (href);
      console.error (task_id);

    if (listSpan) {
      var listName = listSpan.innerHTML;
        var tags = document.getElementById("detailstags_span");
        if (tags){
            console.error ("tags");
            console.error(tags.childNodes.length);
      for (var i = 0; i < tags.childNodes.length; i++)
      {
          console.error (tags.childNodes[i].innerHTML);
          console.error (tags.childNodes[i].innerHTML.indexOf('¶'));
          if (tags.childNodes[i].innerHTML.indexOf('-') != 0)
          {
              var str = tags.childNodes[i].innerHTML.split("-");
              listName = str[str.length - 1];
          }
      }
        }
      var list_id = 0;
      var task = document.getElementById("detailstitle_span");
      var taskName = task.innerHTML;
      //var task_id = 0;

      console.error ("1. " + taskName + ": " + listName);
      var task_text_node = document.getElementById("taskdetails");

      if (task_text_node.lastChild.id == 'harvest_id') {
          info_span = task_text_node.lastChild;
          while (info_span.hasChildNodes()) {
              info_span.removeChild(info_span.firstChild);
          }
      }
      else {
          info_span = document.createElement("div");
          info_span.id = 'harvest_id';

          task_text_node.appendChild(info_span);
      }
        info_span.style.cssFloat = 'right';
              info_span.style.height = '25px';
              info_span.style.width = '25px';
        info_span.padding = '1px';

        console.error ("2. " + taskName + ": " + listName);
        var sHarvest = '<div class="harvest-timer"' +
            'data-project=\'\{"id":"' + 0 + '","name":"' + listName +
'"\}\'' +
            'data-item=\'\{"id":"' + task_id + '","name":"' + taskName +
'"\}\'>' +
            '</div>';
        harvestSpan = document.createElement("div");
        harvestSpan.innerHTML = sHarvest;
        info_span.appendChild(harvestSpan);
    }
  });

    loadTimers();
    ListenForDetails(true);
}

var ListenForDetails = function (listen) {
  var listName = document.getElementById("tasks");
  if (listName) {
    if (listen) {
      listName.addEventListener("click", TurnListNameIntoLink, false);
    }
    else {
      listName.removeEventListener("click", TurnListNameIntoLink, false);
    }
  }
 }

window.addEventListener('unload', function () { ListenForDetails(false) },
false);

var loadTimers = function () {
    $(".harvest-timer").each(function (i, obj) {
        var harvestEvents = {
        type: "harvest-event:timers:add",
         element: $(this)
        }
        $("#harvest-messaging").trigger(harvestEvents);
    });
}

/* ENABLE FEATURES */

ListenForDetails(true);
//ListenForTasks(true);
veevandyke commented 10 years ago

Sorry. I just replied to the email in order to be expeditious.

V

On Wed, May 21, 2014 at 10:23 AM, Paul Simpson notifications@github.comwrote:

@veevandyke https://github.com/veevandyke I believe @granthttps://github.com/grantmeant that when including code snippets in GitHub comments, it's best to use Markdown to style the snippet.

For shorter snippets, use the inline style by surrounding the snippet in backticks (`).

For longer snippets, and to take advantage of syntax highlighting, use a fenced code block by surrounding the entire snippet with triple-backticks. GitHub will try and guess the syntax or you can designate it. For more info check out the GitHub Flavored Markdown guidehttps://help.github.com/articles/github-flavored-markdown. Your snippet in a fenced code block:

// ==UserScript==// @name RTM Harvest Script// @namespace http://www.rememberthemilk.com/home/// @include http://www.rememberthemilk.com/home/// @include http://www.rememberthemilk.com/home/// @include https://www.rememberthemilk.com/home/// @include http://_.www.rememberthemilk.com/home/_// @include https://_.www.rememberthemilk.com/home/_// @version 1// @grant none// ==/UserScript== (function() { window._harvestPlatformConfig = { "applicationName": "Remember The Milk Link", "permalink": "http://rememberthemilk.com/home/veevandyke/#section.tasks/%ITEM_ID%" }; var s = document.createElement('script'); s.src = '//platform.harvestapp.com/assets/platform.js'; s.async = true; var ph = document.getElementsByTagName('script')[0]; ph.parentNode.insertBefore(s, ph); })(); function contentEval(source) { // Check for function input. if ('function' == typeof source) { // Execute this function with no arguments, by adding parentheses. // One set around the function, required for valid syntax, and a // second empty set calls the surrounded function. source = '(' + source + ')();' }

// Create a script node holding this source code. var script = document.createElement('script'); script.setAttribute("type", "application/javascript"); script.textContent = source;

// Insert the script node into the page, so it will run, and immediately // remove it to clean up. document.body.appendChild(script); document.body.removeChild(script);}

var TurnListNameIntoLink = function () { ListenForDetails(false);

// run in page context to access globals ie stateMgr contentEval(function () { if (typeof stateMgr != 'object') { return; } var listSpan = document.getElementById("detailslist_span"); var href = window.location.pathname; var task_id = href.substr(href.lastIndexOf('/') + 1);console.error (href); console.error (task_id);

if (listSpan) {
  var listName = listSpan.innerHTML;
    var tags = document.getElementById("detailstags_span");
    if (tags){
        console.error ("tags");
        console.error(tags.childNodes.length);
  for (var i = 0; i < tags.childNodes.length; i++)
  {
      console.error (tags.childNodes[i].innerHTML);
      console.error (tags.childNodes[i].innerHTML.indexOf('¶'));
      if (tags.childNodes[i].innerHTML.indexOf('-') != 0)
      {
          var str = tags.childNodes[i].innerHTML.split("-");
          listName = str[str.length - 1];
      }
  }
    }
  var list_id = 0;
  var task = document.getElementById("detailstitle_span");
  var taskName = task.innerHTML;
  //var task_id = 0;

  console.error ("1. " + taskName + ": " + listName);
  var task_text_node = document.getElementById("taskdetails");

  if (task_text_node.lastChild.id == 'harvest_id') {
      info_span = task_text_node.lastChild;
      while (info_span.hasChildNodes()) {
          info_span.removeChild(info_span.firstChild);
      }
  }
  else {
      info_span = document.createElement("div");
      info_span.id = 'harvest_id';

      task_text_node.appendChild(info_span);
  }
    info_span.style.cssFloat = 'right';
          info_span.style.height = '25px';
          info_span.style.width = '25px';
    info_span.padding = '1px';

    console.error ("2. " + taskName + ": " + listName);
    var sHarvest = '<div class="harvest-timer"' +
        'data-project=\'\{"id":"' + 0 + '","name":"' + listName +'"\}\'' +
        'data-item=\'\{"id":"' + task_id + '","name":"' + taskName +'"\}\'>' +
        '</div>';
    harvestSpan = document.createElement("div");
    harvestSpan.innerHTML = sHarvest;
    info_span.appendChild(harvestSpan);
}

});

loadTimers();
ListenForDetails(true);}

var ListenForDetails = function (listen) { var listName = document.getElementById("tasks"); if (listName) { if (listen) { listName.addEventListener("click", TurnListNameIntoLink, false); } else { listName.removeEventListener("click", TurnListNameIntoLink, false); } } } window.addEventListener('unload', function () { ListenForDetails(false) },false);

var loadTimers = function () { $(".harvest-timer").each(function (i, obj) { var harvestEvents = { type: "harvest-event:timers:add", element: $(this) } $("#harvest-messaging").trigger(harvestEvents); });} /* ENABLE FEATURES */ ListenForDetails(true);//ListenForTasks(true);

— Reply to this email directly or view it on GitHubhttps://github.com/harvesthq/platform/issues/12#issuecomment-43760491 .

Veronica G. Van Dyke Leadership Simplified | DVD Consulting Incorporated office: 941.776.1121 | mobile: 727.871.0434 | www.leadershipsimplified.com

[image: Leadership Simplified Inc.]

This email message and any attachment(s) are confidential information intended only for the person(s) named above. If you have received this transmission in error, please notify us immediately at the above phone number and destroy the original message and all copies.

adunkman commented 10 years ago

@veevandyke I took a look at this just now, and although I’m not 100% sure on how the code functions, I think it might help if you messaged the Harvest Platform code as you create the timer elements.

Here’s your code sample with a few lines added to send the harvest-event:timers:add event as the timer elements are created:

(function() {
        window._harvestPlatformConfig = {
          "applicationName": "Remember The Milk Link",
          "permalink": "
http://rememberthemilk.com/home/veevandyke/#section.tasks/%ITEM_ID%"
        };
        var s = document.createElement('script');
        s.src = '//platform.harvestapp.com/assets/platform.js';
        s.async = true;
        var ph = document.getElementsByTagName('script')[0];
        ph.parentNode.insertBefore(s, ph);
      })();

function contentEval(source) {
  // Check for function input.
  if ('function' == typeof source) {
    // Execute this function with no arguments, by adding parentheses.
    // One set around the function, required for valid syntax, and a
    // second empty set calls the surrounded function.
    source = '(' + source + ')();'
  }

  // Create a script node holding this  source code.
  var script = document.createElement('script');
  script.setAttribute("type", "application/javascript");
  script.textContent = source;

  // Insert the script node into the page, so it will run, and immediately
  // remove it to clean up.
  document.body.appendChild(script);
  document.body.removeChild(script);
}

var TurnListNameIntoLink = function () {
  ListenForDetails(false);

  // run in page context to access globals ie stateMgr
  contentEval(function () {
    if (typeof stateMgr != 'object') {
      return;
    }
    var listSpan = document.getElementById("detailslist_span");
    var href = window.location.pathname;
    var task_id = href.substr(href.lastIndexOf('/') + 1);
console.error (href);
      console.error (task_id);

    if (listSpan) {
      var listName = listSpan.innerHTML;
        var tags = document.getElementById("detailstags_span");
        if (tags){
            console.error ("tags");
            console.error(tags.childNodes.length);
      for (var i = 0; i < tags.childNodes.length; i++)
      {
          console.error (tags.childNodes[i].innerHTML);
          console.error (tags.childNodes[i].innerHTML.indexOf('¶'));
          if (tags.childNodes[i].innerHTML.indexOf('-') != 0)
          {
              var str = tags.childNodes[i].innerHTML.split("-");
              listName = str[str.length - 1];
          }
      }
        }
      var list_id = 0;
      var task = document.getElementById("detailstitle_span");
      var taskName = task.innerHTML;
      //var task_id = 0;

      console.error ("1. " + taskName + ": " + listName);
      var task_text_node = document.getElementById("taskdetails");

      if (task_text_node.lastChild.id == 'harvest_id') {
          info_span = task_text_node.lastChild;
          while (info_span.hasChildNodes()) {
              info_span.removeChild(info_span.firstChild);
          }
      }
      else {
          info_span = document.createElement("div");
          info_span.id = 'harvest_id';

          task_text_node.appendChild(info_span);
      }
        info_span.style.cssFloat = 'right';
              info_span.style.height = '25px';
              info_span.style.width = '25px';
        info_span.padding = '1px';

        console.error ("2. " + taskName + ": " + listName);
        var sHarvest = '<div class="harvest-timer"' +
            'data-project=\'\{"id":"' + 0 + '","name":"' + listName +
'"\}\'' +
            'data-item=\'\{"id":"' + task_id + '","name":"' + taskName +
'"\}\'>' +
            '</div>';
        harvestSpan = document.createElement("div");
        harvestSpan.innerHTML = sHarvest;
        info_span.appendChild(harvestSpan);

        var messaging = document.getElementById("harvest-messaging");

        if (messaging) {
          messaging.dispatchEvent(new CustomEvent("harvest-event:timers:add", {
            detail: { element: harvestSpan.querySelector(".harvest-timer") }
          }));
        }
        else {
          console.log("Harvest Platform has not yet loaded; new timers will be initialized when the platform initializes.");
        }
    }
  });

    loadTimers();
    ListenForDetails(true);
}

var ListenForDetails = function (listen) {
  var listName = document.getElementById("tasks");
  if (listName) {
    if (listen) {
      listName.addEventListener("click", TurnListNameIntoLink, false);
    }
    else {
      listName.removeEventListener("click", TurnListNameIntoLink, false);
    }
  }
 }

window.addEventListener('unload', function () { ListenForDetails(false) },
false);

var loadTimers = function () {
    $(".harvest-timer").each(function (i, obj) {
        var harvestEvents = {
        type: "harvest-event:timers:add",
         element: $(this)
        }
        $("#harvest-messaging").trigger(harvestEvents);
    });
}

/* ENABLE FEATURES */

ListenForDetails(true);
//ListenForTasks(true);

Hopefully that works better for you! :)