Trello Cards Hyperlinks

Adds support for clickable hyperlinks in your Trello card titles.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Trello Cards Hyperlinks
// @namespace    https://github.com/micalevisk/GM_scripts
// @supportURL   https://github.com/micalevisk/GM_scripts/tree/master/Trello_Cards_Hyperlinks
// @version      2.0
// @description  Adds support for clickable hyperlinks in your Trello card titles.
// @author       Micael Levi
// @copyright    2019, micalevisk (https://openuserjs.org//users/micalevisk)
// @license      MIT
// @match        *://trello.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function($) {

  // Visualize this expression here: https://bit.ly/2FpWz2V
  const REGEX_URL = /\b(?:(https?:\/\/(?:www\.)?)|(www\.))([-a-z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-z0-9@:%_\+.~#?&\/\/=]*))/ig;
  const REGEX_LINK_LABEL = /^\s+\(([^\)]+)\)/;

  window.document.addEventListener('load', dispatchRender, true);
  window.document.addEventListener('blur', dispatchRender, true);

  function dispatchRender() {
    window.setTimeout(renderCardLinks, 0);
    // requestAnimationFrame(renderCardLinks);
  }

  const makeLinkMarkup = (href, title, label = title) => `<a class='tcards-title-link' onclick='event.stopPropagation();' href='${href}' title='${title}' target='_blank' rel='noopener'>${label}</a>`;

  function parseNode(nodeElement, placeholdersLookupRef) {
    if (nodeElement.nodeType !== 3) return; // only text nodes

    const text = nodeElement.data;
    const textWithPlaceholders = text.replace(REGEX_URL, (match, protocol, www, rest, startIndex) => {
      const url = (protocol || '//' + www) + rest;
      const placeholder = `§${Date.now() - startIndex}§`;

      placeholdersLookupRef.push({
        placeholder,
        makeNewMarkup: makeLinkMarkup.bind(null, url, match) // Using partial application
      });

      return placeholder;
    });

    nodeElement.textContent = textWithPlaceholders;
  }

  function updateNode(nodeElement) {
    const placeholdersLookup = [];

    for (const child of nodeElement.childNodes) parseNode(child, placeholdersLookup);
    if (!placeholdersLookup.length) return;

    nodeElement.innerHTML = placeholdersLookup.reduce(
      (withLinks, {placeholder, makeNewMarkup}) => {

        const placeholderStartIdx = withLinks.search(placeholder);
        const contentAfterPlaceholder = withLinks.substr(placeholderStartIdx + placeholder.length);

        const matchLabel = contentAfterPlaceholder.match(REGEX_LINK_LABEL);
        if (matchLabel) {
          return withLinks.substr(0, placeholderStartIdx) +
                 makeNewMarkup(matchLabel[1]) + // Usando a label definida pelo usuário
                 withLinks.substr(placeholderStartIdx + placeholder.length + matchLabel[0].length);
        }

        return withLinks.substr(0, placeholderStartIdx) +
               makeNewMarkup() + // Usando a própria URL como label
               contentAfterPlaceholder;

      },
      nodeElement.innerHTML);
  }

  function renderCardLinks() {
    $('.list-card-title:not( :has(> .tcards-title-link) )').each((_, e) => updateNode(e));
  }

})(jQuery || unsafeWindow.jQuery);