3

Hi I'm trying to create a bookmark-let that will open a webpage find a link to a download on that page and then close the webpage. Unless there's a better way to do it I'm opening the page, calling ready(which I think is the part that's not workint) and then searching for the download link. The code for importing jQuery is taken from: http://coding.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/

javascript:(function() {
var v = "1.3.2";

if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
    var done = false;
    var script = document.createElement("script");
    script.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
    script.onload = script.onreadystatechange = function(){
        if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
            done = true;
            initMyBookmarklet();
        }
    };
    document.getElementsByTagName("head")[0].appendChild(script);
}
else {
    initMyBookmarklet();
}

function initMyBookmarklet() {
    var ytm = window.open('http://example.com');
    jQuery(document).ready(function() {
        var div = ytm.document.getElementById("dl_link");
        var links = ytm.document.getElementsByTagName('a');
        var dl = links[1];
        window.open(dl);});
    ytm.close();
}
})();

Thanks In advance!

Buck
  • 268
  • 1
  • 6
  • 1
    Why such a painfully old version of jQuery? – Matt Ball Apr 08 '13 at 17:37
  • @MattBall Copy/paste. – James Montagne Apr 08 '13 at 17:40
  • 1
    @MattBall it's also the minimum version of jQuery necessary. If the window already has a newer version of jQuery imported it uses that instead. – Buck Apr 08 '13 at 17:58
  • @Buck yes, but if you do load jQuery, why do you not load the latest version something like http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js or http://code.jquery.com/jquery-latest.min.js – gion_13 Apr 08 '13 at 18:22
  • @gion_13 fair point, wasn't sure how to do that until now thanks for the heads up – Buck Apr 08 '13 at 19:28
  • +1 from me, also, dont't use jQuery! use only JavaScript! – Ionut Flavius Pogacian Apr 08 '13 at 20:23
  • Are you only using jQuery for `.ready`? It looks like you are using native DOM methods aside from `.ready`. It seems like overkill to import jQuery just to use `.ready`; you might as well use it to simplify the link finding code too since you've already imported it. – Useless Code Apr 08 '13 at 23:36
  • @gion_13 A newer version of jQuery should be used, but he might want to keep using a specific version instead of latest, if there is a backwards-compatibility breaking change to jQuery in a future release the bookmarklet may suddenly break for no obvious reason. Using a specific version will mean needing to update the version number every now and then but when it breaks after updating the version number, the reason it broke will be obvious and it can be fixed. – Useless Code Apr 08 '13 at 23:44

2 Answers2

2
function initMyBookmarklet() {
    var ytm = window.open('http://example.com');
    jQuery(document).ready(function() {

The closest thing to what you probably want is: jQuery(ytm.document).ready(function() {

However, that probably won't work either because you can't even be sure the document exists at all by the time that line executes. How to solve this is like an entirely new question.

The 2nd, 3rd, and 4th answers are relevant here: jQuery/JavaScript: accessing contents of an iframe

        var div = ytm.document.getElementById("dl_link");
        var links = ytm.document.getElementsByTagName('a');
        var dl = links[1];

If you are using jquery you might as well do something like this div = $('#dl_link', ytm.document); dl = $('a', ytm.document)[1];

        window.open(dl);});

Should probably be window.open(dl.href)

    ytm.close();
}

BUT You will have bigger problems with the same origin policy if this bookmarklet is not run on the same domain as the window you intend to open. Search in Google and in Stackoverflow for same original policy bookmarklet to learn more. Also, the 1st answer is relevant here: jQuery/JavaScript: accessing contents of an iframe

Community
  • 1
  • 1
DG.
  • 3,417
  • 2
  • 23
  • 28
0

Based on that code, your bookmarklet code which is using jquery should be within a function called initMyBookmarklet. It isn't, so jquery has not yet loaded when you execute the code.

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • with your exp, you should know that you must use JavaScript when developing a bookmarklet for cross-domains – Ionut Flavius Pogacian Apr 08 '13 at 20:24
  • 1
    @IonutFlaviusPogacian You can use jquery in bookmarklets. It **is** javascript. – James Montagne Apr 08 '13 at 20:27
  • 1
    @IonutFlaviusPogacian A bookmarklet doesn't **need** to be "pure" JS. It is completely legitimate to import supporting code. He is importing jQuery (which as James said **is** JS); once jQuery is imported he then runs code that uses jQuery. – Useless Code Apr 08 '13 at 23:29