【问题标题】:How to perform functions on two different domains?如何在两个不同的域上执行功能?
【发布时间】:2014-01-13 15:21:22
【问题描述】:

这是我想做的算法:

 1.Locates flickr links with class' high_res_link and puts them in array [].

 2.Opens flickr link with extension "sizes/h/" 

 3.finds largest photo dimensions on flickr. Then goes to that link. Or if there arent any big enough goes to step 2 and goes to next
array. 

 4. then opens link to download if downloading is enabled. If not goes to step 2 and goes to next array.

 5. Goes to step 2 and goes to next array.

我正在尝试编写一些跨越两个域的代码:Tumblr 和 Flickr。

我目前使用 Jquery 和 Javascript 编写了 3 个函数,我想在 2 个不同的 URL 上运行它们:

功能#1:

function link_to_flickr() {
var hre = [];
$('.high_res_link').parent(this).each(function(){
  var h = $(this).attr('href') +"sizes/o/";
  hre.push(h);
});
alert(hre[0]);
}

这会在 Tumblr 页面上找到我想要的 Flickr 页面的链接。并将它们放在一个数组中。

功能#2:

function find_large_quality() {
  var w = 1280;
  var h = 720;
  var matchingDivs = $("small").each(function () {
      var match = /^\((\d+) x (\d+)\)$/.exec($(this).text());
      if (match) {
          if (parseInt(match[1], 10) >= w && parseInt(match[2], 10) >= h) {
              return true;
          }
      }
      return false;
  });
  var href = $.trim(matchingDivs.text()).match(/\(.*?\)/g);

  if (matchingDivs.length >= 1) {
      alert("success");
  } else {
      alert("fail");
  }
 var ho = $('small:contains("'+href[href.length - 1]+'")').parent(this).find("a").attr("href");
alert("http://www.flickr.com"+ho);
}

这个函数在 Flickr URL 上搜索一次,然后搜索尺寸大于 720p 的图像。

功能#3:

function Download(){
var heyho = $('a:contains("Download the")').attr('href');
    window.open(heyho, '_blank');
}

这会下载图像文件。访问最高质量的 Flickr URL


每个alert 我都想打开网址。并执行下一个功能。多年来,我一直在尝试一种方法来做这样的事情。 使用 AJAX、使用 PHP、使用 Jsonp、使用jquery.xdomainajax.js 等……但我自己无法想出足够的方法。

有没有人推荐他们做这样的事情?

【问题讨论】:

  • 你想做什么?你能用几句话解释一下吗?!
  • Javascript 通常由页面加载。你是有能力修改 Tumblr 和 Flickr 的 javascript 的人吗?如果没有,你可能应该用 Python 之类的东西来编程。
  • @thelr 我在哪里可以使用 Python 做这样的事情?
  • 好吧,除了将您的算法转换为 Python 之外,您还需要查看 urllib 或 urllib2 以获取从远程 URL 获取内容的方法。许多使用示例将包括解析数据的方法。
  • 你的意思是像 imacros?

标签: javascript jquery cross-domain flickr


【解决方案1】:

由于 CORS,您通常不能在不同的域上运行函数,除非域允许这样做。

如果您正在编写用户脚本/扩展程序,您可以在内容脚本的两个页面上使用postMessage (quick tutorial on how to use it cross-domain),并使用实现的页面间通信来控制您的脚本流程。

另一种方法是使用您要访问的网站的 API。

或者使用 Python 和 BeautifulSoup。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2012-12-16
    相关资源
    最近更新 更多