Skip to content

Instantly share code, notes, and snippets.

@MillerMedia
Last active November 23, 2022 12:26
Show Gist options
  • Save MillerMedia/b8dd811ea4e4f9638ac94328e4c1982d to your computer and use it in GitHub Desktop.
Save MillerMedia/b8dd811ea4e4f9638ac94328e4c1982d to your computer and use it in GitHub Desktop.
Quick and Dirty Twitter Search Improvement
/**
MIT License
Copyright (c) 2022 Matt Miller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
/**
• Tested from the 'Explore' page on Chrome and Firefox with this code pasted in the Console prior to search
• Tokenizes the 'From:' in the search
• Shows a list of the last users you've liked posts from once 'From:' is entered if you enter a bearer token below (no way to get your user information without token; this can be obtained if you go to the 'Network' tab in Chrome console, filter by XHR requests, click on one and pull it from the Auth header)
• @todo See if V2 API possibly allows better access to items without bearer token
• @todo Known bug that if you type 'From:', delete and then type it again, it breaks the search
• @todo Improve the right spacing after the tokenized 'From:' area
**/
bearer_token="{ENTER_BEARER_TOKEN}";
function getCookieForTwitter(e){let r=`; ${document.cookie}`,s=r.split(`; ${e}=`);if(2===s.length)return s.pop().split(";").shift()}(bearer_token).includes("{")||fetch("https://twitter.com/i/api/graphql/lr2pk7rKqCqLSqWRGRaW5Q/Likes?variables=%7B%22userId%22%3A%22"+(twitter_user_id=getCookieForTwitter("twid").replace("u%3D",""))+"%22%2C%22count%22%3A20%2C%22includePromotedContent%22%3Afalse%2C%22withSuperFollowsUserFields%22%3Atrue%2C%22withDownvotePerspective%22%3Afalse%2C%22withReactionsMetadata%22%3Afalse%2C%22withReactionsPerspective%22%3Afalse%2C%22withSuperFollowsTweetFields%22%3Atrue%2C%22withClientEventToken%22%3Afalse%2C%22withBirdwatchNotes%22%3Afalse%2C%22withVoice%22%3Atrue%2C%22withV2Timeline%22%3Atrue%7D&features=%7B%22responsive_web_twitter_blue_verified_badge_is_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22unified_cards_ad_metadata_container_dynamic_card_content_query_enabled%22%3Atrue%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_uc_gql_enabled%22%3Atrue%2C%22vibe_api_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Afalse%2C%22interactive_text_enabled%22%3Atrue%2C%22responsive_web_text_conversations_enabled%22%3Afalse%2C%22responsive_web_enhance_cards_enabled%22%3Atrue%7D",{headers:{authorization:"Bearer "+bearer_token,"x-csrf-token":getCookieForTwitter("ct0"),cookie:document.cookie+"' -H 'x-csrf-token: "+getCookieForTwitter("ct0")},body:null,method:"GET"}).then(function(e){e.json().then(function(e){html_to_insert='<div role="progressbar" aria-valuemax="100" aria-valuetext="" class="css-1dbjc4n r-gfcssk r-1udh08x r-lrvibr r-11j9u27 r-13qz1uu r-184en5c" aria-valuenow="0"><div class="css-1dbjc4n r-l5o3uw r-1pi2tsx r-1joz4e6 r-11iat2r r-1c4c958 r-1wyyakw" data-testid="progressBar-bar" style="width: 0%;"></div></div>';for(let r=0;r<e.data.user.result.timeline_v2.timeline.instructions[0].entries.length;r++)if("TimelineTimelineItem"==e.data.user.result.timeline_v2.timeline.instructions[0].entries[r].content.entryType&&void 0!==(user_itemcontent=e.data.user.result.timeline_v2.timeline.instructions[0].entries[r].content.itemContent)&&void 0!==(user_core=user_itemcontent.tweet_results.result.core)){if(verified_badge=(user_info=user_core.user_results.result.legacy).verified?'<svg viewBox="0 0 24 24" aria-label="Verified account" role="img" class="r-13v1u17 r-4qtqp9 r-yyyyoo r-1xvli5t r-9cviqr r-f9ja8p r-og9te1 r-bnwqim r-1plcrui r-lrvibr"><g><path d="M22.25 12c0-1.43-.88-2.67-2.19-3.34.46-1.39.2-2.9-.81-3.91s-2.52-1.27-3.91-.81c-.66-1.31-1.91-2.19-3.34-2.19s-2.67.88-3.33 2.19c-1.4-.46-2.91-.2-3.92.81s-1.26 2.52-.8 3.91c-1.31.67-2.2 1.91-2.2 3.34s.89 2.67 2.2 3.34c-.46 1.39-.21 2.9.8 3.91s2.52 1.26 3.91.81c.67 1.31 1.91 2.19 3.34 2.19s2.68-.88 3.34-2.19c1.39.45 2.9.2 3.91-.81s1.27-2.52.81-3.91c1.31-.67 2.19-1.91 2.19-3.34zm-11.71 4.2L6.8 12.46l1.41-1.42 2.26 2.26 4.8-5.23 1.47 1.36-6.2 6.77z"></path></g></svg>':"",user_info.following)following_icon='<svg viewBox="0 0 24 24" aria-hidden="true" class="r-1bwzh9t r-4qtqp9 r-yyyyoo r-tbmifm r-dnmrzs r-bnwqim r-1plcrui r-lrvibr r-16eto9q"><g><path d="M17.863 13.44c1.477 1.58 2.366 3.8 2.632 6.46l.11 1.1H3.395l.11-1.1c.266-2.66 1.155-4.88 2.632-6.46C7.627 11.85 9.648 11 12 11s4.373.85 5.863 2.44zM12 2C9.791 2 8 3.79 8 6s1.791 4 4 4 4-1.79 4-4-1.791-4-4-4z"></path></g></svg>';else var s=t=user_info.description,t=s.length>58?s.substring(0,55)+"...":s;html_to_insert+='<div role="option" aria-selected="false" class="css-1dbjc4n" data-testid="typeaheadResult"><div role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-6dt33c r-1ny4l3l r-o7ynqc r-6416eg"><div role="button" tabindex="0" class="css-18t94o4 css-1dbjc4n r-1ny4l3l r-ymttw5 r-1f1sjgu" data-testid="TypeaheadUser"><div class="css-1dbjc4n r-18u37iz"><div class="css-1dbjc4n r-v2d8zz r-18kxxzh r-1h0z5md r-1b7u577"><div class="css-1dbjc4n r-1adg3ll r-1mwlp6a r-bztko3 r-13qz1uu" data-testid="UserAvatar-Container-ireland"><div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div><div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu"><div class="css-1dbjc4n r-1adg3ll r-1pi2tsx r-1wyvozj r-bztko3 r-u8s1d r-1v2oles r-desppf r-13qz1uu"><div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div><div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu"><div class="css-1dbjc4n r-sdzlij r-ggadg3 r-1udh08x r-u8s1d r-8jfcpp" style="height: calc(100% - -4px); width: calc(100% - -4px);"><div aria-hidden="true" role="presentation" tabindex="-1" class="css-1dbjc4n r-1niwhzg r-1pi2tsx r-1ny4l3l r-13qz1uu"><div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);"><div class="css-1dbjc4n r-1niwhzg r-1pi2tsx r-13qz1uu"></div></div><div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);"><div class="css-1dbjc4n r-kemksi r-1pi2tsx r-13qz1uu"></div></div><div class="css-1dbjc4n r-kemksi r-sdzlij r-1wyvozj r-1udh08x r-633pao r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);"><div class="css-1dbjc4n r-1adg3ll r-1udh08x" style=""><div class="r-1adg3ll r-13qz1uu" style="padding-bottom: 100%;"></div><div class="r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-ipm5af r-13qz1uu"><div aria-label="" class="css-1dbjc4n r-1p0dtai r-1mlwlqe r-1d2f490 r-1udh08x r-u8s1d r-zchlnj r-ipm5af r-417010"><div class="css-1dbjc4n r-1niwhzg r-vvn4in r-u6sd8q r-4gszlv r-1p0dtai r-1pi2tsx r-1d2f490 r-u8s1d r-zchlnj r-ipm5af r-13qz1uu r-1wyyakw" style="background-image: url(&quot;'+user_info.profile_image_url_https+'&quot;);"></div><img alt="" draggable="true" src="'+user_info.profile_image_url_https+'" class="css-9pa8cd"></div></div></div></div><div class="css-1dbjc4n r-sdzlij r-1wyvozj r-1udh08x r-u8s1d r-1v2oles r-desppf" style="height: calc(100% - 4px); width: calc(100% - 4px);"><div class="css-1dbjc4n r-172uzmj r-1pi2tsx r-1ny4l3l r-13qz1uu"></div></div></div></div></div></div></div></div></div><div class="css-1dbjc4n r-1iusvr4 r-16y2uox"><div class="css-1dbjc4n r-1awozwy r-18u37iz r-1wtj0ep"><div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs r-1ny4l3l"><div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs r-1ny4l3l"><div class="css-1dbjc4n r-1wbh5a2 r-dnmrzs r-1ny4l3l"><div class="css-1dbjc4n r-1awozwy r-18u37iz r-dnmrzs"><div dir="auto" class="css-901oao r-1awozwy r-1nao33i r-6koalj r-37j5jr r-a023e6 r-b88u0q r-rjixqe r-bcqeeo r-1udh08x r-3s2u2q r-qvutc0"><span class="css-901oao css-16my406 css-1hf3ou5 r-poiln3 r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">'+user_info.name+'</span></span></div><div dir="auto" class="css-901oao r-1nao33i r-xoduu5 r-18u37iz r-1q142lx r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0">'+verified_badge+'</div></div></div><div class="css-1dbjc4n r-1awozwy r-18u37iz r-1wbh5a2"><div tabindex="-1" class="css-1dbjc4n r-1wbh5a2 r-dnmrzs r-1ny4l3l"><div class="css-1dbjc4n"><div dir="ltr" class="css-901oao css-1hf3ou5 r-1bwzh9t r-18u37iz r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">'+user_info.screen_name+'</span></div></div></div></div></div></div></div><div class="css-1dbjc4n r-1awozwy r-18u37iz r-1v8amoe"><div class="css-1dbjc4n r-1q142lx">'+following_icon+'</div><div dir="auto" class="css-901oao css-1hf3ou5 r-1bwzh9t r-37j5jr r-a023e6 r-16dba41 r-rjixqe r-bcqeeo r-nk90ef r-qvutc0"><span dir="ltr" class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-poiln3 r-bcqeeo r-qvutc0">'+(user_info.following?"Following":t)+"</span></span></div></div></div></div></div></div></div>"}localStorage.html_to_insert=html_to_insert})}),light_mode=document.body.getAttribute("style").includes("255, 255, 255");let x=document.querySelector("[data-testid='SearchBox_Search_Input']");x.addEventListener("keyup",e=>{if("Shift"!=e.key&&"Meta"!=e.key&&"Alt"!=e.key&&"Control"!=e.key){let r=e.target.parentElement;if(e.target.value.toLowerCase().includes("from:")&&!r.contains(document.getElementById("from_token"))){e.target.value=e.target.value.split("from:").join("");let s=document.createElement("div");s.setAttribute("id","from_token"),s.innerText="From:",r.prepend(s),s.style.padding="5px 5px",s.style.height="15px",s.style["border-radius"]="5px",s.style["margin-top"]="5px",s.style["padding-bottom"]="10px",s.style["font-size"]="15px",s.style.background=light_mode?"rgb(0,0,0)":"rgb(233,233,233)",s.style.position="absolute",s.style.color=light_mode?"white":"black";var t=document.getElementById("typeaheadDropdown-1"),i=document.getElementById("typeaheadDropdown-2"),_=document.getElementById("typeaheadDropdown-3");t?t.innerHTML=localStorage.html_to_insert:i?i.innerHTML=localStorage.html_to_insert:_&&(_.innerHTML=localStorage.html_to_insert)}else!e.target.value.toLowerCase().includes("from:")&&r.contains(document.getElementById("from_token"))&&document.getElementById("from_token").remove()}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment