0

I'm developing a chrome extension which injects a link on every tweet. The injection implementation idea was taken from a official Twitter plugin where I saw that the injection was handled by running setInterval() function every two seconds. I wonder about performance though. Before that, I had been dealing with MutationObserver in order to get new DOM elements, but it didn't work for me on some cases, so I gave a try on the idea above.

In my case, when the user clicks on the injected link, my plugin should request Twitter the geolocation and, if found, return it, else it would be saved (maybe using indexedDB?) so it isn't showed again. This adds an extra query on whether or not to show that injected link on every tweet.

$(document).ready(function() {

    $('body').append('<div id="map"></div>');

    setInterval( function() {
        inyect();
    }, 2000);

});

function inyect() {

    $(".tweet, .js-tweet").each(function(){
        var inyected = $(this).find('.mapLink').length ? true : false;

        if ( ! inyected ) {  
            // also, check against the "geo-fails" list. if not in, do: 
            var id = $(this).attr('data-tweet-id');
            var geoElement = $(this).find('span.ProfileTweet-geo');

            geoElement
                .parent()
                .prepend('<a class="mapLink" tweet-id="'+ id +'" href="#">map</a>');
        }
    });

}

$('body').on('click', 'a.mapLink', function(e) {
    e.preventDefault();

    var elem = $(e.target)
    var id = elem.attr('tweet-id')

    // request Twitter if the tweet contains geolocation data 
    // and save the position in that case

    // show map

    // else, SAVE that tweet id into the "geo-fails" list
    // show message and hide the link.
}

Knowing my context, what are your recommendations on those two aspects (performance on injecting links & storage)?

whitenoisedb
  • 403
  • 1
  • 5
  • 16
  • I'm having trouble following what you're wanting to do. Are you asking about injecting a link on every tweet without using `setInterval`? Or are you asking about using the geolocation when someone clicks on the tweet? If both, you should separate them into separate questions. – Teepeemm Apr 30 '15 at 03:54
  • I'm asking if in my case, `setInterval()` is a good practice. Geolocation is not a question, I just mention it to consider that when links are injected, there's a first condition in which it asks if that link is already inyected, and then **it will query** a list or database to search in the _fail list_. In the case it _is_ on the list the link shouldn't be shown. The fail list populates when a user clicks and there's no geolocation for that tweet ID – whitenoisedb Apr 30 '15 at 04:51
  • You say geolocation is not your question, but most of what you've written relates to the geolocation. So the tweeter posts (and your extension injects a link). When the reader of the tweet clicks the link, your extension does something else. But these are two completely different actions. Why should the reader have any affect on what you tweet? – Teepeemm Apr 30 '15 at 05:02
  • It's easier that that. I inject my link, for every tweet. When user clicks it, I would request Twitter geolocation for that tweet ID. If is successful, it shows a map, else, it saves that tweet ID into an array of fails and hides that link. From now on, that link is not shown again on that tweet. – whitenoisedb Apr 30 '15 at 05:31
  • But you're wanting the extension for the injecting part only? I don't know if the rest of your question about what that link does is at all relevant. – Teepeemm Apr 30 '15 at 14:32
  • It is relevant but shows the context in which I'm using setInterval(). I wonder about the **performance** because every 2 seconds it will be not only checking if all tweet elements are injected but also do a **database query** looking if that tweet ID is on the _fail_ list. – whitenoisedb May 03 '15 at 18:49

0 Answers0