Canonical links

Replace twitter redirection url (http://t.co/...) by the real URLs as to improve privacy.

目前為 2015-05-14 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Canonical links
// @author      Guillaume
// @description Replace twitter redirection url (http://t.co/...) by the real URLs as to improve privacy. 
// @namespace   https://greasyfork.org/fr/users/11386-guillaume
// @include     https://twitter.com/*
// @version     1.1
// @grant       none
// @run-at      document-start
// @license     CC by-sa http://creativecommons.org/licenses/by-sa/3.0/
// ==/UserScript==

// Credits:
//
// - getVisibleText, textContentVisible:
//   This is derived from a work by Ethan Brown.
//   Original license: cc-by sa
//   Source: http://stackoverflow.com/questions/19985306/get-the-innertext-of-an-element-but-exclude-hidden-children

function getVisibleText( node )
{
  if ( node.nodeType === 1 ) // Element
  {
    var rect = node.getBoundingClientRect();
    if ( rect.width === 0 || rect.height === 0 )
      return '';
  }
  else if ( node.nodeType === 3 ) // Text
  {
    return node.textContent;
  }
  var text = '';
  for( var i = 0; i < node.childNodes.length; i++ ) 
    text += getVisibleText( node.childNodes[i] );
  return text;
}

if ( ! Node.prototype.hasOwnProperty('textContentVisible') )
{
  Object.defineProperty(Node.prototype, 'textContentVisible', {
      get: function() { 
         return getVisibleText( this );
      }, 
      enumerable: true
  });
}

var makeLinkCanonical = function(el)
{
  var text = document.createTextNode(el.textContentVisible);
  var url = el.dataset.expandedUrl;
  
  var newLink = document.createElement('a');
  newLink.href = url;
  newLink.title = el.title;
  newLink.className = el.className;
  newLink.dir = el.dir;
  newLink.appendChild(text);
  
  el.parentNode.replaceChild(newLink, el);
};

window.addEventListener("load", function(ev) {
  // unshadow for better debugging
  delete console.log;
}, true);

document.addEventListener("load", function(ev) {
  [].slice.call(document.querySelectorAll('a[data-expanded-url]')).forEach(makeLinkCanonical);
}, true);