This userscript is an improvment proposition for http://userscripts.org/scripts/show/139503
Created
August 17, 2012 11:23
-
-
Save tzi/3378121 to your computer and use it in GitHub Desktop.
A #javascript #userscript: Fix redirect twitter link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Twitter Redirect-Fixer | |
// @include https://*.twitter.com/* | |
// @include https://twitter.com/* | |
// @include http://*.twitter.com/* | |
// @include http://twitter.com/* | |
// ==/UserScript== | |
(function() { | |
function huntForTweets( event ) { | |
var node = event.target; | |
if ( isTweetNode( node ) ) { | |
fixTweetLinks( node ); | |
} | |
} | |
var isTweetNode = function( node ) { | |
// Test1: filtering by element, low cost test | |
// Test2: filtering by data-item-type, for tweets added at the bottom of your timeline | |
// Test3: filtering by class, for new tweets added at the top of your timeline | |
return ( node.tagName == 'DIV' && ( | |
node.getAttribute( 'data-item-type' ) == 'tweet' || | |
node.className.substr( 0, 6 ) == 'tweet ' | |
) ); | |
} | |
var fixTweetLinks = function( tweetNode ) { | |
var links = tweetNode.getElementsByClassName( 'twitter-timeline-link' ); | |
for ( var i=0; i<links.length; i++ ) { | |
links[i].href = getFixedHref( links[i] ); | |
} | |
} | |
// Method based on script from http://userscripts.org/scripts/review/117942 | |
var getFixedHref = function( node ) { | |
var realURL = node.getAttribute('data-ultimate-url'); | |
/* | |
BUGFIX1: Google Groups redirect to main page. (t.co bug probably) More Info: | |
https://addons.opera.com/en/extensions/details/twitter-redirect-fixer/?reports&display=en#r39265-30425-comment | |
BUGFIX2: Certain Promoted Tweets with Youtube Links redirect to a page to verify age. The tweet that triggers this bug located at: | |
https://twitter.com/sleepingdogs/status/200315064803270656 | |
*/ | |
if ( ! realURL || realURL.match( /groups\.google\.com/ ) || realURL.match( /youtube\.com/ ) ) { | |
realURL = node.getAttribute('data-expanded-url'); | |
} | |
if ( realURL ) { | |
return realURL; | |
} | |
return node.href; | |
}; | |
// Add event when a tweet is added | |
document.addEventListener( 'DOMNodeInserted', huntForTweets, false ); | |
// Parse every tweets links that are already present when the page is loaded | |
fixTweetLinks( document ); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment