获取动态加载到页面中的图像

getting the images that load in a page dynamically

本文关键字:图像 动态 加载 获取      更新时间:2024-03-08

我想在加载facebook新闻源时获得它中的所有图像。我正在运行一个篡改的脚本。我遇到了一些问题:

  1. 最终的结果是包含在我排除的带有url的图像中(带有facebook静态url)
  2. 它只包括新闻提要中的一些图像,如果我向下滚动,它不会重新评估其输出。这可能是因为加载函数的原因,但我如何才能使其成为动态加载呢?我在哪里可以添加像.roll这样的功能

我使用jquery只在页面加载时运行函数。我应该做点别的吗?

以下是部分代码:

// ==UserScript==
// @name         Accountability
// @namespace    http://tampermonkey.net/
// @include     https://www.facebook.com/*
// @include     http*://*.facebook.com/*
// @exclude       htt*://*static*.facebook.com*
// @version      0.1
// @description  
// @author       You
// @match        http://tampermonkey.net/scripts.php
// @grant        none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==
/* jshint -W097 */
'use strict';
 window.addEventListener('load', function() {
var all_images = document.evaluate('//img[@src]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
var newsfeed = document.evaluate('//*[contains(@id, topnews_main_stream_408239535924329)]', document, null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); 
var imgSrcs = [];
for (var i=0; i < all_images.snapshotLength; i++){
    var this_image = all_images.snapshotItem(i);
    var src = this_image.src;
    if(src.indexOf('static') > -1){
        continue;
    }
    if(src.indexOf('external') > -1){
        continue;
    }
    imgSrcs.push(src);
    console.log(this_image.src);
    this_image.addEventListener("click", my_func, false);
}
for (var i=0; i < newsfeed.snapshotLength; i++){
    var this_news = newsfeed.snapshotItem(i);
    var src = this_news.src;
    if(this_news.children.length>0){
        }
    if(Object.getOwnPropertyNames(this_news)[0]== '_startTime'){ 
        var x = this_news.onreadystatechange();
        }   
    this_news.addEventListener("click", my_func, false);
    this_news.addEventListener("mouseover", my_func, false);
}
var my_func = function(){
    console.log("the list", imgSrcs);
}
}, false);

您可以在所有img标签上绑定load,如

$("img").on("load", function() {
  console.log($(this)[0].src)
});

如果你真的想用纯javascript来模仿on,你可以参考EmulatejQuery"关于";带有纯javascript 中的选择器