Twitter单页视频批量提取V0.2
// ==UserScript==
// @name Twitter单页视频批量提取V0.2
// @namespace http://bbs.tampermonkey.net.cn/
// @version 0.2
// @description try to take fuck the world!
// @author 李恒道
// @match https://twitter.com/*
// @icon https://www.google.com/s2/favicons?domain=twitter.com
// @grant GM_setClipboard
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
let userlist=[]
let checkinsert=false;
let videoorpiclist=[];
function GetDownloadText(){
let outtext=''
let getlength=0
for(let index=0;index<userlist.length;index++){
let item=userlist[index]
let textcontent=item.content.items
console.log('textcontent',textcontent,item)
if(textcontent===undefined){
if(item.content.itemContent===undefined){
continue;
}
if(item.content.itemContent.tweet_results.result===undefined){
continue;
}
if(item.content.itemContent.tweet_results.result.legacy===undefined){
continue;
}
textcontent=item.content.itemContent.tweet_results.result.legacy.full_text+'\n'
outtext+=textcontent
getlength++
outtext+='-------------------------------------\n'
}else{
for(let textlengh=0;textlengh<textcontent.length;textlengh++){
let textitem=textcontent[textlengh]
if(textitem.item.clientEventInfo.component!=="suggest_organic_conversation"){
continue;}
if(textitem.item.itemContent.tweet_results.result.legacy===undefined){
continue;
}
outtext+=textitem.item.itemContent.tweet_results.result.legacy.full_text+'\n'
getlength++
}
outtext+='-------------------------------------\n'
}
}
GM_setClipboard(outtext)
alert('已设置到剪辑版,共获取推文:'+getlength+'个')
}
function GetVideoorPic(media){
if(media===undefined){
return;
}
if(media.media!==undefined){
media=media.media
}
for(let index=0;index<media.length;index++){
let item=media[index]
if(item.video_info===undefined){
videoorpiclist.push(item.media_url_https)
//图片
}else{
//视频
videoorpiclist.push(item.video_info.variants[0].url)
}
}
}
function GetDownloadMedia(){
videoorpiclist=[]
for(let index=0;index<userlist.length;index++){
let item=userlist[index]
console.log(item)
let textcontent=item.content.items
if(textcontent===undefined){
if(item.content.itemContent===undefined){
continue;
}
if(item.content.itemContent.tweet_results.result===undefined){
continue;
}
if(item.content.itemContent.tweet_results.result.legacy===undefined){
continue;
}
textcontent=item.content.itemContent.tweet_results.result.legacy.extended_entities
GetVideoorPic(textcontent)
}else{
for(let textlengh=0;textlengh<textcontent.length;textlengh++){
let textitem=textcontent[textlengh]
let video=''
if(textitem.item.clientEventInfo.component!=="suggest_organic_conversation"){
continue;}
if(textitem.item.itemContent.tweet_results.result.legacy===undefined){
continue;
}
video=textitem.item.itemContent.tweet_results.result.legacy.extended_entities
GetVideoorPic(video)
}
}
}
GM_setClipboard(videoorpiclist.join('\n'))
alert('已设置到剪辑版,共获取链接:'+videoorpiclist.length+'个')
}
function CheckDrawerBtn(){
if(checkinsert===false){
let parentinsert=document.querySelector('[aria-label="更多"]')
if(parentinsert!==null&&parentinsert.parentElement.localName!=='nav'){
checkinsert=true
}else{
return;
}
parentinsert=parentinsert.parentElement
let downloadtext=document.createElement("div");
downloadtext.style.marginLeft='9.2px'
downloadtext.innerHTML='<div aria-expanded="false" aria-haspopup="menu" aria-label="更多" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1niwhzg r-1ets6dv r-sdzlij r-1phboty r-rs99b7 r-6gpygo r-1kb76zh r-2yi16 r-1qi8awa r-1ny4l3l r-o7ynqc r-6416eg r-lrvibr" data-testid="userActions"><div dir="auto" class="css-901oao r-1awozwy r-18jsvk2 r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0" style="margin: 0px 10px;">提取文字<span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-a023e6 r-rjixqe r-bcqeeo r-qvutc0"></span></div></div>';
downloadtext.onclick=function(event){
GetDownloadText()
};
parentinsert.appendChild(downloadtext)
let downloadmedia=document.createElement("div");
downloadmedia.innerHTML='<div aria-expanded="false" aria-haspopup="menu" aria-label="更多" role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1niwhzg r-1ets6dv r-sdzlij r-1phboty r-rs99b7 r-6gpygo r-1kb76zh r-2yi16 r-1qi8awa r-1ny4l3l r-o7ynqc r-6416eg r-lrvibr" data-testid="userActions"><div dir="auto" class="css-901oao r-1awozwy r-18jsvk2 r-6koalj r-18u37iz r-16y2uox r-37j5jr r-a023e6 r-b88u0q r-1777fci r-rjixqe r-bcqeeo r-q4m81j r-qvutc0" style="margin: 0px 10px;">提取媒体<span class="css-901oao css-16my406 css-bfa6kz r-poiln3 r-a023e6 r-rjixqe r-bcqeeo r-qvutc0"></span></div></div>'
downloadmedia.onclick=function(event){
GetDownloadMedia()
}
parentinsert.appendChild(downloadmedia)
};
}
function addXMLRequestCallback(callback){
var oldSend, i;
if( XMLHttpRequest.callbacks ) {
// we've already overridden send() so just add the callback
XMLHttpRequest.callbacks.push( callback );
} else {
// create a callback queue
XMLHttpRequest.callbacks = [callback];
// store the native send()
oldSend = XMLHttpRequest.prototype.send;
// override the native send()
XMLHttpRequest.prototype.send = function(){
// process the callback queue
// the xhr instance is passed into each callback but seems pretty useless
// you can't tell what its destination is or call abort() without an error
// so only really good for logging that a request has happened
// I could be wrong, I hope so...
// EDIT: I suppose you could override the onreadystatechange handler though
for( i = 0; i < XMLHttpRequest.callbacks.length; i++ ) {
XMLHttpRequest.callbacks[i]( this );
}
// call the native send()
oldSend.apply(this, arguments);
}
}
}
// e.g.
addXMLRequestCallback( function( xhr ) {
xhr.addEventListener("load", function(){
if ( xhr.readyState == 4 && xhr.status == 200 ) {
CheckDrawerBtn()
if(xhr.responseURL.indexOf('UserTweets')!==-1){
let jsonobj=JSON.parse(xhr.response)
let list=jsonobj.data.user.result.timeline.timeline.instructions[0].entries
if(jsonobj.data.user.result.timeline.timeline.instructions[1]!==undefined){
list.push(jsonobj.data.user.result.timeline.timeline.instructions[1].entry)
}
if(xhr.responseURL.indexOf('cursor')===-1){
//首次加载,不存在指针
userlist=list
}else{
userlist=[...userlist,...list]
}
console.log( 'UserTweets',userlist);
}
}
});
});