Debtwire Highlights

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Debtwire Highlights
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.debtwire.com/intelligence/view/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    var highlightedIndex = {
  intelId: 0,
  highlightedParagraphPaths: []
}
var highlightedRegister = []
var intelArticles = document.querySelectorAll('.headline-card.intel-article')

intelArticles.forEach(function(articleTag) {
  var o = Object.assign({}, highlightedIndex)
  o.intelId = articleTag.querySelector('.headline-card__main h1').id.split('-')[2]
  highlightedRegister.push(o)
})


document.addEventListener('mouseup', function() {
      // toggleHighlightElement(getSelectedText().containingElement)
      window.location.hash = JSON.stringify(toggleHighlight(getSelectedText().containingElement))
}, false);

function getSelectedText() {
  if (window.getSelection) {
    var selection = window.getSelection();
    return {
      text: selection.toString(),
      containingElement: selection.baseNode,
      baseOffset: selection.baseOffset,
      focusOffset: selection.focusOffset
    };
  }
  return null;
}

function getDomPath(el) {
  if (!el) {
    return;
  }
  var stack = [];
  while (el.parentNode != null) {
    // console.log(el.nodeName);
    var sibCount = 0;
    var sibIndex = 0;
    // get sibling indexes
    for ( var i = 0; i < el.parentNode.childNodes.length; i++ ) {
      var sib = el.parentNode.childNodes[i];
      if ( sib.nodeName == el.nodeName ) {
        if ( sib === el ) {
          sibIndex = sibCount;
        }
        sibCount++;
      }
    }
    var nodeName = el.nodeName.toLowerCase();
    if ( sibCount > 1 ) {
      stack.unshift(nodeName + ':nth-of-type(' + (sibIndex + 1) + ')');
    } else {
      stack.unshift(nodeName);
    }
    el = el.parentNode;
  }
  stack.splice(0,1); // removes the html element
  return stack.join(' > ');
}

function toggleHighlight(el) {
  var closestParagraph = getClosest(el.parentNode, 'p')
  var intelId = getClosest(el.parentNode, 'article').querySelector('.headline-card__main h1').id.split('-')[2]

  toggleHighlightElement(closestParagraph)

  return highlightedRegister.filter(function(o){
    if(o.intelId === intelId) {
      togglePath(o, getDomPath(closestParagraph))
      return o
    }
  })

}

function togglePath(o, path) {
  var index = o.highlightedParagraphPaths.indexOf(path)
  if(index === -1) {
    o.highlightedParagraphPaths.push(path)
  } else {
    o.highlightedParagraphPaths.splice(index)
  }
}

function toggleHighlightElement(el) {
  el.innerHTML = el.firstChild.tagName === 'MARK' ? el.firstChild.innerHTML : "<mark>"+el.innerHTML+"</mark>"
}

function getClosest( elem, selector ) {

  // Element.matches() polyfill
  if (!Element.prototype.matches) {
    Element.prototype.matches =
      Element.prototype.matchesSelector ||
      Element.prototype.mozMatchesSelector ||
      Element.prototype.msMatchesSelector ||
      Element.prototype.oMatchesSelector ||
      Element.prototype.webkitMatchesSelector ||
      function(s) {
        var matches = (this.document || this.ownerDocument).querySelectorAll(s),
          i = matches.length;
        while (--i >= 0 && matches.item(i) !== this) {}
        return i > -1;
      };
  }

  // Get closest match
  for ( ; elem && elem !== document; elem = elem.parentNode ) {
    if ( elem.matches( selector ) ) {
      return elem;
    }
  }

  return null;

};



})();