aquagremlin
aquagremlin

Reputation: 3549

Javascript code to "click" compose button in yahoo mail?

I tried this as a bookmarklet but nothing happens:

javascript:(function(){window.document.getElementById(".Compose").click();})

The error condole shows this:

[Error] TypeError: 'null' is not an object (evaluating 'window.document.getElementById(".Compose").click')
    (anonymous function) (launch, line 1)

I want to do this because I use webmail only and not a mail client. My goal is to be able to copy text from a webpage and then open up a yahoo mail window, click 'compose' and paste in the object I had copied. I am trying to make a bookmarklet that will open up yahoo mail and click compose for me.Then I could manually fill in the destination address myself. Where is my syntax off?

Addendum: This also fails as a bookmarklet:

javascript:(function(){$(".btn-compose").click()})();

The console window does not show any errors either. However this works in the console window

$(".btn-compose").click()

Upvotes: 0

Views: 756

Answers (3)

Tom Fenech
Tom Fenech

Reputation: 74595

I just went on my Yahoo mail and did this in the JS console:

$(".btn-compose").click()

It loaded the compose window.

It appears that the jQuery library is already used by Yahoo mail, so you can get all elements with the class name "btn-compose" and trigger the "click" event using the code above.

If you want to avoid using jQuery, this also works:

document.getElementsByClassName('btn-compose')[0].click()

getElementsByClassName returns a list, so the [0] gets the first element.

Upvotes: 1

aquagremlin
aquagremlin

Reputation: 3549

well, to get jQuery to work in a bookmarklet as indicated by Tom's code, i have to load query first in the bookmarklet. I found this site that generates the necessary code for that:

http://benalman.com/code/test/jquery-run-code-bookmarklet/

crunched it into a bookmarklet too!

That single line that works in the console gets inflated to:

javascript:(function(e,a,g,h,f,c,b,d)%7Bif(!(f=e.jQuery)%7C%7Cg%3Ef.fn.jquery%7C%7Ch(f))%7Bc=a.createElement(%22script%22);c.type=%22text/javascript%22;c.src=%22http://ajax.googleapis.com/ajax/libs/jquery/%22+g+%22/jquery.min.js%22;c.onload=c.onreadystatechange=function()%7Bif(!b&&(!(d=this.readyState)%7C%7Cd==%22loaded%22%7C%7Cd==%22complete%22))%7Bh((f=e.jQuery).noConflict(1),b=1);f(c).remove()%7D%7D;a.documentElement.childNodes%5B0%5D.appendChild(c)%7D%7D)(window,document,%221.3.2%22,function($,L)%7Bjavascript:(function()%7B$(%22.btn-compose%22).click()%7D)();%7D);

which can be beautified to:

javascript: (function(e, a, g, h, f, c, b, d) {
    if (!(f = e.jQuery) || g > f.fn.jquery || h(f)) {
        c = a.createElement("script");
        c.type = "text/javascript";
        c.src = "http://ajax.googleapis.com/ajax/libs/jquery/" + g + "/jquery.min.js";
        c.onload = c.onreadystatechange = function() {
            if (!b && (!(d = this.readyState) || d == "loaded" || d == "complete")) {
                h((f = e.jQuery).noConflict(1), b = 1);
                f(c).remove()
            }
        };
        a.documentElement.childNodes[0].appendChild(c)
    }
})(window, document, "1.3.2", function($, L) {
    javascript: (function() {
        $(".btn-compose").click()
    })();
});

I wish I had the knowledge to write it jQuery-less though.

Upvotes: 0

Nathan Taylor
Nathan Taylor

Reputation: 24606

Unless the id of the field is ".Compose" (it isn't), this code will not work.

Try: document.getElementById('Compose').

Upvotes: 1

Related Questions