将数据从一个打开的浏览器选项卡移动到另一个打开的浏览器选项卡的有效方法是什么?
我正在寻找一种快速方法来从一个网页中获取一些数据并将其放入另一个网页中。我无权访问第二页 URL 中的查询字符串,因此不能选择以这种方式传递数据。现在,我正在使用 Greasemonkey 用户脚本和 JS 小书签触发器: javascript:doIt();
// ==UserScript==
// @include public_site
// @include internal_site
// ==/UserScript==
if (document.location.host.match(internal_site)) {
var datum1 = GM_getValue("d1");
var datum2 = GM_getValue("d2");
}
unsafeWindow.doIt = function() {
if(document.location.host.match(public_site)) {
var d1 = innerHTML of page element 1;
var d2 = innerHTML of page element 2;
//Next two lines use setTimeout to bypass GM_setValue restriction
window.setTimeout(function() {GM_setValue("d1", d1);}, 0);
window.setTimeout(function() {GM_setValue("d2", d2);}, 0);
}
else if(document.location.host.match(internal_site)) {
document.getElementById("field1").value = datum1;
document.getElementById("field2").value = datum2;
}
}
虽然我对另一种方法持开放态度,但如果可能的话,我更愿意保留这个基本模型,因为这只是 doIt()
中代码的一小部分,该代码在其他几个页面上使用,主要用于自动填充基于日期的表单;人们真的很喜欢他们的“神奇按钮”。
上面的代码可以工作,但是工作流程会中断:为了让用户知道从公共站点的哪个页面获取数据,必须首先打开内部页面。然后,一旦从公共页面设置了 GM cookie,就必须重新加载内部页面才能将正确的信息获取到内部页面变量中。我想知道是否有任何方法可以在小书签点击时 GM_getValue()
来防止刷新的需要。谢谢!
I am looking for a quick way to grab some data off of one Web page and throw it into another. I don't have access to the query string in the URL of the second page, so passing the data that way is not an option. Right now, I am using a Greasemonkey user script in tandem with a JS bookmarklet trigger: javascript:doIt();
// ==UserScript==
// @include public_site
// @include internal_site
// ==/UserScript==
if (document.location.host.match(internal_site)) {
var datum1 = GM_getValue("d1");
var datum2 = GM_getValue("d2");
}
unsafeWindow.doIt = function() {
if(document.location.host.match(public_site)) {
var d1 = innerHTML of page element 1;
var d2 = innerHTML of page element 2;
//Next two lines use setTimeout to bypass GM_setValue restriction
window.setTimeout(function() {GM_setValue("d1", d1);}, 0);
window.setTimeout(function() {GM_setValue("d2", d2);}, 0);
}
else if(document.location.host.match(internal_site)) {
document.getElementById("field1").value = datum1;
document.getElementById("field2").value = datum2;
}
}
While I am open to another method, I would prefer to stay with this basic model if possible, as this is just a small fraction of the code in doIt()
which is used on several other pages, mostly to automate date-based form fills; people really like their "magic button."
The above code works, but there's an interruption to the workflow: In order for the user to know which page on the public site to grab data from, the internal page has to be opened first. Then, once the GM cookie is set from the public page, the internal page has to be reloaded to get the proper information into the internal page variables. I'm wondering if there's any way to GM_getValue()
at bookmarklet-clicktime to prevent the need for a refresh. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您能否将小书签移动到 Greasemonkey 将添加到页面的按钮或链接?
然后您可以设置点击事件处理程序来触发 GM_getValue()。
看起来当前的方法正在利用一个“安全漏洞”——这个漏洞将来可能会被关闭。您可能会考虑在 Firefox 扩展中完成所有操作。
可能有用的链接: http://articles.sitepoint.com/article/十个提示-firefox-extensions/1
Can you move the bookmarklet to a button or link -- that Greasemonkey will add to the page(s)?
Then you could set click-event handlers to fire GM_getValue().
It looks like the current method is exploiting a "security hole" -- one that may be closed in the future. You might consider doing everything in a Firefox extension, instead.
Possibly useful link: http://articles.sitepoint.com/article/ten-tips-firefox-extensions/1