Yesterday I discovered Bookmarklets and am totally in love. I wrote several to reduce the number of clicks for a few common tasks on my favorite websites. What I would like to do now, if possible, is to combine the actions of two of these bookmarklets into one single script/link. The current set up is as follows: Bookmarklet 1 (B1) performs an action (on URL 1) that loads a page (URL 2) where Bookmarklet 2 (B2) then enters a standard set of data into a form and submits it.
I have tried all sorts of variations of the following:
javascript:(function(){w=window.open(codeFromB1,'CatchyPageTitle'); w.TryToWriteSomethingToTheTheNewWindowToPassAndCallFunctionB2;)}();
But all of my attempts at w.TryToWriteSomethingToTheTheWindow yield errors of varying types. My most recent attempt was to use something like:
alert(w.document.getElementsByTagName("form").length); which gives the count from the calling page on the first time called, but the count from the new window on the second time called...
So anyway, that's my story for today.
In summary, I'm a lost soul in need of guidance. I have two script actions that take place on two different pages that I would like to combine into one code snippet that can be saved as a Bookmarklet. I need someone to point me in the right direction so that I can figure out how to 'link' the two pages/scripts so as to create one spectacular Bookmarklet.
Thanks in advance for any help.
Also, I have a mock data Array() that I am using to finish the rest of the script and found one more question for those more intelligent than am I. Part of B2 reads:
wdoc.forms[0].t5.value=#;
where t5 is the name of the input/text in the form. Why does that work but:
thisInput = 't'.concat(i); // where i=5 in the for-loop wdoc.forms[0].thisInput.value=#;
gives me an error - "thisInput" is undefined. I also tried creating an array with the input/text names like:
docume开发者_开发百科nt.forms[0].thisInput[i].value=#;
but that gives the same error. Any suggestions?
Is URL 1 on a different domain (or subdomain) to URL 2? If so, you will be coming up against a cross-domain issue. You could resolve it using HTML5 cross-window messaging, at least for the browsers that support it.
You might also be interested in Greasemonkey if you're willing to be Firefox-specific and require anyone using it to have the Greasemonkey extension installed. I've used it to easily write multi-page bookmarklets which go through complicated forms, or collect data from multiple search pages. At the start of the script, you simply choose what to do next based on document.location.href. You can also exploit the window.name hack to share information between pages.
I did some more tests and found that:
wdoc=w.document;alert(wdoc.location.href);alert(wdoc.location.href);
would first display 'undefined' but the second alert would display the proper url (of the new window) FROM the original window. So I did a few more tests and have come to the conclusion that my attempts from last night were failing simply due to the fact that the window needs to load before calling the functions.
So now I have:
javascript:(function(){ w=window.open(B1,"CatchyPageTitleThatDoesn'tAppear"); setTimeout("otherFunction();",750);})(); function otherFunction(){ wdoc=w.document;thisDoc=document; wdoc.setVariablesInTheFormInTheNewWindow;wdoc.forms[0].submit();}
which works! woohoo!
And now I move on to what, for me, is the really hard part. I would like to pull additional data from URL1, put it into an array and then use that array to set the values in the form on URL2. I should qualify that. Arrays, data, forms, easy. I'm not sure how exactly to go about pulling the data. It looks sort of like this:
tbody class="first of two appearances"> tr> td> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> td> img /> /td> /tr> tr>
th>specific text /th> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> /tr> tr> th>
other specific text /th> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> td># /td> /tr> /tbody>
So, I know (maybe?) that I can use something like
uglyText = wdoc.getElementsByClass("first of two appearances")[0].innerHTML;
to get get that mess of data into a string, but am not sure how to proceed from there. I understand the concept of regular expressions and assume that something like that might work here, but have never successfully written one myself. Also, sometimes there are 10 data points and other times there are 11, if that matters.
At this point I only care about the #s from specific_text to other_specific_text. So any simple suggestion that will lead me to a solution that grabs them in order will make my day.
Thanks again for the help.
精彩评论