-
-
Save tagawa/1702902 to your computer and use it in GitHub Desktop.
(function() { | |
if (window.location.hostname === "twitter.com") { | |
var tcoToLinkTitleURL = function(ev) { | |
var target = ev.target, dataURI = target.getAttribute("data-expanded-url"); | |
if (/^https?:\/\/t.co\//.test(target.href) && dataURI) target.href = dataURI; | |
}; | |
document.addEventListener("mousedown", tcoToLinkTitleURL, true); | |
} | |
})(); |
Formatted it a bit better. It can now be used as-is in a UserJS (Greasemonkey) script.
For a bookmarklet, just remove line breaks and add javascript:
at the beginning of the code
Use .getAttribute("data-expanded-url")
for wider compatibility on browsers that don't support HTML5.
Good thinking! Updated.
Great!
I tried making a Firefox plugin because you need a plugin or extension, really. It's no good to have to keep clicking the bookmarklet each time you go from "@connect" to "Home". Firefox plugins require a lot of boilerplate -- all for those few lines of code. I failed to get it to work.
I'm afraid I don't know about Firefox plugins (I work for Opera) but I did manage to get it to work as a Greasemonkey script.
Also, I made it into an extension for Opera: https://addons.opera.com/en/addons/extensions/details/usurptco/
I just noticed you've moved the if
statement to surround the the FunctionDeclaration
. That's not valid syntax and should result in a SyntaxError
.
Implementations have various interpretations of what to do when a FunctionDeclaration
appears where only statements are allowed. Please see: http://jibbering.com/faq/#functionStatement
As an alternative, use a FunctionExpresssion
.
Another problem is checking in the if
statement .getAttribute("data-expanded-url")
but then assigning the property target.href = target.dataset.expandedUrl
. The problem is that some browsers will support the attribute but won't support HTML5 dataset property. Instead, you'll get wider compat by using just the attribute.
Why you prefer to regex location.href
over location.hostname
? location.hostname == "twitter.com"
seems clearer and simpler to me.
Untested:
if (location.hostname == "twitter.com") { var tcoToLinkTitleURL = function(ev) { var target = ev.target, dataURI = target.getAttribute("data-expanded-url"); if (/^https?:\/\/t.co\//.test(target.href) && dataURI) target.href = dataURI; }; document.addEventListener("mousedown", tcoToLinkTitleURL, true); }
Thanks for the explanation. I hadn't come across that difference in function declarations & expressions before.
The expandedUrl still in there was my oversight.
As for location.href - I just forgot about hostname! I've added the window object to it so that it'll work in an extension environment.
I appreciate the collaboration!
Because FunctionDeclaration
may not appear where only Statement
s are allowed, it is best to not do that. Who knows what future versions of Opera will do WRT newer recommendations from TG39 in Harmony or ES6. And then you have to scramble to update the (avoidable) bugs you coded in as a result of programming by observation.
One option is to use an anonymous FunctionExpression
as .addEventListener("mousedown", function(){}, true);
.
A classic example of using FunctionExpression
in programming is Google and GMail source, in particular. GMail guys wrote their code, then had a bunch of errors, so decided to wrap everything -- including FunctionDeclaration
in try
/ catch
-- a classic beginner folly.
And so when they did that, they created invalid syntax that worked in the browsers that they thought they were sniffing (browser sniffing is another folly that google has been employing for years). And the funny thing is that when Google wanted to make Closure Compiler (unreliable), is that they got in the predicament of trying to accommodate the invalid syntax that they'd been advised not to use, namely, they had to address the invalid use of FunctionDeclaration
where only Statement
may appear. And the problem with trying to accommodate that invalid syntax is that the behavior of such syntax depends on the implementation. So then they had to try and figure out what was wanted in the code, which of course was impossible to do globally because the desired interpretation was based on purely coincidence of "we did this and then it passed QA". And Google history shows a lot of bugs making it through QA (leading Richard Cornford to question the fact of even if Google does QA. Here's one of Richard's quotes:
Do Google have a QA department? My impression was that they just published their stuff untested and waited for complaints form the users.
What this really boils down to is that programming by is unreliable and not forwards compatible.
Edited the original script so that it works in Opera, only runs on twitter.com, and is enabled for both http and https. Tested in latest stable versions of Opera, Firefox & Chrome.