3

昨天我发现了书签,我完全爱上了。我写了几个来减少我最喜欢的网站上一些常见任务的点击次数。如果可能的话,我现在想做的是将其中两个小书签的操作组合到一个脚本/链接中。当前设置如下:Bookmarklet 1 (B1) 执行加载页面 (URL 2) 的操作(在 URL 1 上),Bookmarklet 2 (B2) 然后将一组标准数据输入表单并提交。

我尝试了以下各种变体:

javascript:(function(){w=window.open( codeFromB1 ,'CatchyPageTitle'); w.TryToWriteSomethingToTheNewWindowToPassAndCallFunctionB2;)}();

但是我在 w.TryToWriteSomethingToTheTheWindow 的所有尝试都会产生不同类型的错误。我最近的尝试是使用类似的东西:

alert(w.document.getElementsByTagName("form").length); 它给出了第一次调用的调用页面的计数,但是第二次调用的新窗口的计数......

所以无论如何,这就是我今天的故事。

总之,我是一个需要指导的迷失的灵魂。我有两个脚本操作发生在两个不同的页面上,我想将它们组合成一个可以保存为书签的代码片段。我需要有人给我指出正确的方向,这样我才能弄清楚如何“链接”这两个页面/脚本,从而创建一个壮观的 Bookmarklet。

提前感谢您的帮助。


此外,我有一个模拟数据 Array(),我用它来完成脚本的其余部分,并为那些比我更聪明的人发现了一个问题。B2 的一部分内容如下:

wdoc.forms[0].t5.value=#;

其中 t5 是表单中输入/文本的名称。为什么这行得通,但是:

thisInput = 't'.concat(i); // 在 for 循环中 i=5 wdoc.forms[0].thisInput.value=#;

给我一个错误-“thisInput”未定义。我还尝试使用输入/文本名称创建一个数组,例如:

document.forms[0].thisInput[i].value=#;

但这给出了同样的错误。有什么建议么?

4

2 回答 2

1

URL 1 与 URL 2 在不同的域(或子域)上吗?如果是这样,您将遇到跨域问题。您可以使用HTML5 跨窗口消息传递来解决它,至少对于支持它的浏览器。

如果您愿意成为特定于 Firefox 的用户并要求任何使用它的人安装 Greasemonkey 扩展,那么您可能也对 Greasemonkey 感兴趣。我用它来轻松编写多页书签,这些书签可以通过复杂的表格,或者从多个搜索页面收集数据。在脚本开始时,您只需根据 document.location.href 选择下一步要做什么。您还可以利用window.name hack在页面之间共享信息。

于 2009-10-29T20:37:13.207 回答
0

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.

于 2009-10-29T23:26:01.763 回答