/*
* @title なんちゃってツイートスタンプ
* @description メディア付きツイートをスタンプ的に引用
* @include https://twitter.com/*
* @license MIT License
* @require
* @javascript_url
*/
(async ()=>{
'use strict';
const
tweet_urlobj = new URL(location.href),
matches = tweet_urlobj.pathname.match(/^\/([^/]+)\/status\/(\d+)/),
screen_name = matches?.[1],
tweet_id = matches?.[2];
if ((! /[/.]twitter\.com$/.test(tweet_urlobj.origin)) || (! screen_name) || (! tweet_id)) {
alert('Unsupported page !');
return;
}
const
fetch_result = await fetch(`https://twitter.com/i/api/2/timeline/conversation/${tweet_id}.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_quote_count=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&referrer=tweet&controller_data=DAACDAAEDAABCgABAAX2IAGQCCEAAAAA&count=20&include_ext_has_birdwatch_notes=false&ext=mediaStats%2ChighlightedLabel`, {
method: 'GET',
headers: {
'authorization' : 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA',
'x-csrf-token' : document.cookie.match( /ct0=(.*?)(?:;|$)/ )[ 1 ],
'x-twitter-active-user' : 'yes',
'x-twitter-auth-type' : 'OAuth2Session',
'x-twitter-client-language' : 'en',
},
mode: 'cors',
credentials: 'include',
}
).then(result => result.json()).catch(error => error),
first_media = fetch_result?.globalObjects?.tweets?.[tweet_id]?.extended_entities?.media?.[0],
first_media_url = first_media?.expanded_url;
if (! first_media_url) {
alert('Media not found !');
return;
}
const
hash_tag = (document.documentElement.lang == 'ja') ? '#なんちゃってツイートスタンプ' : '#PseudoTweetStamp',
credit_screen_name = new URL(first_media_url).pathname.match(/\/([^/]+)/)?.[1] ?? screen_name,
tweet_text = `${hash_tag} by @${credit_screen_name} ${first_media_url}`,
tweet_input_url = `${tweet_urlobj.origin}/intent/tweet?text=` + encodeURIComponent(tweet_text),
state = {
key: 'r80bpk',
state: {
fromApp: true,
previousPath: tweet_urlobj.pathname,
},
},
pop_state_event = new PopStateEvent('popstate', { state : state });
history.pushState(state, '', tweet_input_url);
dispatchEvent( pop_state_event );
})();