bjornmartensson / Toodledo Links In New Tab

// ==UserScript==
// @name        Toodledo Links In New Tab
// @namespace   http://bjrn.net
// @description Makes links in Toodledo task notes open in a new tab, rather than open in place and replace the Toodledo page.
// @include     https://www.toodledo.com/*
// @license     CC BY http://creativecommons.org/licenses/by/3.0/
// @version     1
// @grant       none
// ==/UserScript==

// Based on script from http://userscripts-mirror.org/scripts/show/149337 but adapted to only modify Toodledo note links.

if (document.body){
  // Check all links in the body now
  checkNode(document.body);
  // Use a MutationObserver (Fx14+) to watch for additions to the document that could include new links
  var MutOb, chgMon, i, httpels, opts;
  // To disable this feature (might improve performance), comment out the following line:
  var MutOb = (window.MutationObserver) ? window.MutationObserver : window.WebKitMutationObserver;
  if (MutOb){
    chgMon = new MutOb(function(mutationSet){
      mutationSet.forEach(function(mutation){
        // when additions to the document are detected, check for links
        for (i=0; i<mutation.addedNodes.length; i++){
          if (mutation.addedNodes[i].nodeType == 1){
            checkNode(mutation.addedNodes[i]);
          }
        }
      });
    });
    // attach chgMon to document.body
    opts = {childList: true, subtree: true};
    chgMon.observe(document.body, opts);
  }
  /* // For testing only -- all links cyan
  var sty = document.createElement("style");
  sty.setAttribute("type", "text/css");
  sty.appendChild(document.createTextNode("a{background-color:#0ff}"));
  document.body.appendChild(sty);
  */
}

function checkNode(el){
  // Find links that do not have a target attribute; we don't want to change existing targets
  var aLinks = el.querySelectorAll("div[class=note] > a[href]:not([target])");
  var href = "";
  if (aLinks.length > 0){
    for (var j = 0; j < aLinks.length; j++){
      // Check links that do not start with "javascript:"
      href = aLinks[j].href.toLowerCase();
      if (href.indexOf("javascript:") !== 0){
				// Add the target regardless of site
				aLinks[j].setAttribute("target", "_blank");
				/* // For testing only
				aLinks[j].style.backgroundColor = "#ff0";
				*/
      }
    }
  }
}