1

I have two bookmarklets that work individually 1)

javascript:(function(){window.open("http://mail.yahoo.com");})();

2)

javascript:(function(){;document.getElementsByClassName('btn-compose')[0].click()})();

(the above lines of code were put through a bookmarklet generator here: http://ted.mielczarek.org/code/mozilla/bookmarklet.html and each bookmarklet works fine)

but when they are together like so: javascript:(function(){window.open("http://mail.yahoo.com");document.getElementsByClassName('btn-compose')[0].click()})();

or like so:

javascript:(function(){window.open("http://mail.yahoo.com");})();
javascript:(function(){;document.getElementsByClassName('btn-compose')[0].click()})();

the compose window does not open. I have a feeling the DOM is not ready.

but this fails:

javascript:(function(){window.open("http://mail.yahoo.com");})();
document.onreadystatechange = function () {
     if (document.readyState == "complete") {
    javascript:(function(){;document.getElementsByClassName('btn-compose')[0].click()})();
   }
 }
aquagremlin
  • 3,515
  • 2
  • 29
  • 51

1 Answers1

2

You have several issues going on.

  1. window.open() opens a new window. If you wanted to operate on that window, you would have to get that window handle and then get the document from THAT window. Right now, you're trying to operate on the original window with your second script.

  2. A script launched from one window cannot access a different domain in a different window due to browser security restrictions.

It appears to me that you will either have to manually launch the second script after the Yahoo window opens. Or, you will need to use a browser plugin that can access the new window.


It also appears that you're trying to solve a problem that is probably already solved (bringing up a compose window in Yahoo mail). There is probably a URL you can just open in a new window that will start the compose window directly without having to hack into the window to push a button.

See this post for info on a URL that will take you to a Yahoo compose window. The general form of the URL is this: http://compose.mail.yahoo.com/?to=TO&subject=SUBJECT&body=BODY

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Aha!>A script launched from one window cannot access a different domain in a different window due to browser security restrictions – aquagremlin Sep 16 '14 at 17:03
  • Nope>It also appears that you're trying to solve a problem that is probably already solved (bringing up a compose window in Yahoo mail – aquagremlin Sep 16 '14 at 17:03
  • @aquagremlin - see the last sentence I added to my post. It works. – jfriend00 Sep 16 '14 at 17:04
  • thank you for the enlightenment even if that didnt lead to success. – aquagremlin Sep 16 '14 at 17:04
  • @aquagremlin - Please read the last paragraph I added to my answer. There is success there. – jfriend00 Sep 16 '14 at 17:05
  • indeed. my bad. i only used google. they probably down rate search results with their competitors' names. I guess you search directly here on SO. I am thinking we need a distributed form of google's page rank running on bittorrent. – aquagremlin Sep 16 '14 at 17:08
  • @aquagremlin - I searched on Google. Probably just searched for something different. – jfriend00 Sep 16 '14 at 17:11