将数据从一个打开的浏览器选项卡移动到另一个选项卡的有效方法是什么?

时间:2010-02-13 05:01:47

标签: javascript greasemonkey

我正在寻找一种从一个网页上抓取一些数据并将其扔到另一个网页中的快速方法。我无权访问第二页的URL中的查询字符串,因此以这种方式传递数据不是一种选择。现在,我正在使用Greasemonkey用户脚本与JS bookmarklet触发器串联: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,就必须重新加载内部页面以获取内部页面变量的正确信息。我想知道在bookmarklet-clicktime中是否有任何方式GM_getValue()以防止需要刷新。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以将书签移动到按钮或链接 - Greasemonkey会添加到页面吗? 然后,您可以设置click-event处理程序以触发GM_getValue()。

看起来当前的方法正在利用“安全漏洞” - 未来可能会关闭的漏洞。您可以考虑在Firefox扩展中执行所有操作。

可能有用的链接:http://articles.sitepoint.com/article/ten-tips-firefox-extensions/1