tony-o / t.co-bypass

104 stars 20 forks source link

What is the difference to the "t.co is the devil" greasemonkey method #8

Closed ansell closed 10 years ago

ansell commented 10 years ago

I have been using t.co is the devil greasemonkey script, successfully, to do this. However, it appears to use a completely different method, which still appears to work with the current interface. What are the significant differences between the method used here and that?

Script was originally from https://userscripts.org/scripts/show/115929 , and is reproduced below:

// ==UserScript==
// @name           t.co is the devil
// @namespace      net.miell.tco-is-the-devil
// @description    Rewrite t.co links to point directly to their target
// @include        https://twitter.com/*
// @include        http://twitter.com/*
// ==/UserScript==

(function ()
{
function fixLinks(rootNode) {
    var timelineLinks = rootNode.getElementsByClassName("twitter-timeline-link");

    for (let i = 0; i < timelineLinks.length; ++i) {
        let link = timelineLinks[i];
        if (link.dataset.ultimateUrl !== undefined) {
            link.href = link.dataset.ultimateUrl;
        } else if (link.dataset.expandedUrl !== undefined) {
            link.href = link.dataset.expandedUrl;
        }
    }
}

function nodeChanged(event) {
        fixLinks(event.target);   
}

window.addEventListener("load", function(e) {
    fixLinks(document);
    document.addEventListener("DOMNodeInserted", nodeChanged, false);
    document.addEventListener("DOMNodeInsertedIntoDocument", nodeChanged, false);
    document.addEventListener("DOMAttrModified", nodeChanged, false);
}, false);
})();
dannytranlx commented 10 years ago

One have jQuery dependency.

And it's pretty much the same behavior. t.co is evil gets all links by Twitter with the twitter-time-link class and this one goes through all the links in the page with href matching //t.co.

Though, thinking about it. t.co is evil will only catch links in the timeline, I don't know if there are t.co links in some modals or in the interface.

<a href="http://t.co/nTIbRSv9aW" data-expanded-url="http://ift.tt/1nPR4Fq" class="twitter-timeline-link" [...]>[...]</a>

Then both replace the href with the data in data-expanded-url and sets an event listener on the DOM, so if the DOM changed with auto-load or something else, we re-run the script to update the new t.co links.

tony-o commented 10 years ago

this repo no longer relies on jquery unless you're using safari, just an fyi

timdorr commented 10 years ago

Also, it is now using a MutationObserver instead of DOM node events. They tend to be very noisy and fire quite often. It should be far more efficient now, updating an order of magnitude less often.

dannytranlx commented 10 years ago

Things change fast around here :satisfied: So if I anwsered correctly OP's question, we could close this issue! :wink:

ansell commented 10 years ago

Yes, that was a good explanation. The MutationObserver looks cleaner than the fixLinks(document) method. :+1: