I'd like to get all the images in a facebook newsfeed as it is loaded. I'm running a tampermonkey script. I'm having a few problems:
I'm using jquery to run the functions only when the page is loaded. Should I do something else instead?
Below is some part of the code:
// ==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);
You can bind load
on all img
tags like
$("img").on("load", function() {
console.log($(this)[0].src)
});
If you really want to use pure javascript to mimic on
, you can reference Emulate jQuery "on" with selector in pure javascript
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.