合并两个小书签
昨天我发现了 Bookmarklet,并且完全爱上了它。我写了几个来减少我最喜欢的网站上一些常见任务的点击次数。如果可能的话,我现在想做的是将其中两个书签的操作合并到一个脚本/链接中。当前设置如下:Bookmarklet 1 (B1) 执行一个操作(在 URL 1 上),加载页面 (URL 2),然后 Bookmarklet 2 (B2) 在表单中输入一组标准数据并提交它。
我尝试了以下各种变体:
javascript:(function(){w=window.open(codeFromB1,'CatchyPageTitle'); w.TryToWriteSomethingToTheNewWindowToPassAndCallFunctionB2;)}();
但我对 w.TryToWriteSomethingToTheTheWindow 的所有尝试都会产生不同类型的错误。我最近的尝试是使用类似的东西:
alert(w.document.getElementsByTagName("form").length);它给出了第一次调用时来自调用页面的计数,但第二次调用时来自新窗口的计数......
所以无论如何,这就是我今天的故事。
总而言之,我是一个迷失的灵魂,需要指导。我有两个脚本操作发生在两个不同的页面上,我想将它们组合成一个可以保存为书签的代码片段。我需要有人为我指明正确的方向,以便我能够弄清楚如何“链接”这两个页面/脚本,从而创建一个壮观的书签。
预先感谢您的任何帮助。
另外,我有一个模拟数据 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=#;
但这给出了同样的错误。有什么建议吗?
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:
document.forms[0].thisInput[i].value=#;
but that gives the same error. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URL 1 与 URL 2 是否位于不同的域(或子域)?如果是这样,您将遇到跨域问题。您可以使用 HTML5 跨窗口消息传送 来解决此问题,至少对于以下浏览器而言支持它。
如果您愿意专门针对 Firefox 并要求使用它的任何人都安装 Greasemonkey 扩展,您可能也会对 Greasemonkey 感兴趣。我用它来轻松编写多页书签,这些书签可以处理复杂的表单,或者从多个搜索页面收集数据。在脚本开始时,您只需根据 document.location.href 选择下一步要执行的操作。您还可以利用window.name hack在页面之间共享信息。
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.
我做了更多测试,发现:
wdoc=w.document;alert(wdoc.location.href);alert(wdoc.location.href);
首先会显示“未定义”,但第二个警报将显示原始窗口中的正确 URL(新窗口的)。因此,我做了一些更多的测试,并得出结论,我昨晚的尝试失败仅仅是因为窗口需要在调用函数之前加载。
所以现在我有:
javascript:(function(){
w=window.open(B1,"CatchyPageTitleThatDoesn'tAppear");
setTimeout("otherFunction();",750);})();
函数其他函数(){
wdoc=w.document;thisDoc=文档;
wdoc.setVariablesInTheFormInTheNewWindow;wdoc.forms[0].submit();}
有效!哇哦!
现在我开始讨论对我来说真正困难的部分。我想从 URL1 中提取附加数据,将其放入数组中,然后使用该数组在 URL2 上的表单中设置值。我应该限定这一点。数组、数据、表格,很简单。我不确定如何准确地提取数据。它看起来有点像这样:
tbody class="first of two faces">
tr>
td> /td>
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>特定文本 /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>
其他特定文本 /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>
所以,我知道(也许?)我可以使用
丑陋的文本 = wdoc.getElementsByClass("第一次出现两次")[0].innerHTML;
将这些混乱的数据放入字符串中,但我不知道如何从那里继续。我理解正则表达式的概念,并假设类似的东西可能在这里起作用,但我自己从未成功编写过。此外,有时有 10 个数据点,有时有 11 个数据点(如果这很重要的话)。
此时,我只关心从 special_text 到 other_specific_text 的 #。因此,任何能够引导我找到一个按顺序抓住它们的解决方案的简单建议都会让我很开心。
再次感谢您的帮助。
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.