javascript - 如何在两个不同的域上执行功能?

标签 javascript jquery cross-domain flickr

这是我想要做的算法:

 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 我想改为打开 URL。并执行下一个功能。我多年来一直在尝试一种方法来做这样的事情。 使用AJAX、使用PHP、使用Jsonp、使用jquery.xdomainajax.js等等...但我自己无法想出足够的方法。

Has anybody got any way they would recommend going about doing something like this?

最佳答案

由于 CORS,您通常无法在不同的域上运行函数,除非域允许。

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

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

或者将 Python 与 BeautifulSoup 结合使用。

关于javascript - 如何在两个不同的域上执行功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21094870/

相关文章:

javascript - 如何匹配javascript正则表达式中字符之间的字符串

javascript - iPhone HTML5 应用程序滚动问题

javascript - 滚动时运行脚本,但仅运行一次

javascript - 基于单选按钮的Jquery函数是选择

javascript - 是否可以从任何已加载 jquery 的页面向 google.com 发出成功的 ajax get 请求?

.net - 设置 header : P3P: CP ="CAO PSA OUR" 可能带来什么危害

javascript - 在 Create React App 中设置导入的全局服务

javascript正则表达式允许数字和特殊字符但不允许零

javascript - 如果我有一项,如何从选项菜单中选择一项

javascript - 处理弹出窗口关闭的正确方法