Skip to content

Instantly share code, notes, and snippets.

@ikanoano
Created December 3, 2021 19:10
Show Gist options
  • Save ikanoano/ae0288ed1d637980921dd42f34a768da to your computer and use it in GitHub Desktop.
Save ikanoano/ae0288ed1d637980921dd42f34a768da to your computer and use it in GitHub Desktop.
extract tweets from timeline
var env = function () {
const extUrl = /\(([^)]+)\)/;
const mo =
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
const target = document.querySelector('.js-chirp-container');
const observer = new mo(function (mutations) {
mutations.forEach((mutation) => {
const column = mutation.target;
let limit = 20;
let extracted = '';
column.childNodes.forEach((box) => {
if (--limit < 0) return;
tweet = box.querySelector('.js-tweet');
if (!tweet) return;
// author
header = tweet.querySelector('.js-tweet-header');
fullname = header.querySelector('.fullname');
// text
text = tweet.querySelector('.js-tweet-text');
// pic / video
medias = tweet.querySelectorAll('.js-media-image-link'); // .js-media-preview-container
let thumbnails = '';
medias.forEach((m) => {
const url = m.attributes.style.value.match(extUrl)[1];
if (!url) return;
thumbnails += '<img src="' + url + '">';
});
extracted += header.outerHTML;
extracted += text.outerHTML;
extracted += thumbnails;
});
console.log(extracted);
});
});
const config = { attributes: true, childList: true, characterData: true };
observer.observe(target, config);
return window.setTimeout(() => {
observer.disconnect();
console.log('observer killed');
}, 60 * 1000);
};
env();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment