TweetXer

Delete all your Tweets for free.

当前为 2023-09-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TweetXer
  3. // @namespace https://gist.github.com/lucahammer/a4d1e957ec9e061e3cccafcbed599e16/
  4. // @version 0.1
  5. // @description Delete all your Tweets for free.
  6. // @author Luca
  7. // @license MIT
  8. // @match https://twitter.com/*
  9. // @icon https://www.google.com/s2/favicons?domain=twitter.com
  10. // @grant unsafeWindow
  11. // ==/UserScript==
  12.  
  13. /*
  14. TweetXer
  15.  
  16. You can use this script to delete all your Tweets. Even if they don't show up on your profile. But you need your Data Export for it to work.
  17. Because this automates the deletion, it may get your account banned. Not that bad. Copies of your Tweets may still exist on backups and so on.
  18.  
  19. # Usage
  20. 0. Log into your Twitter account
  21. 1. You must have at least one visible Tweet on your profile. Post a new one, if there isn't one.
  22. 2. Open the browser console (F12)
  23. 3. Paste the whole script into the console and press enter
  24. 4. A light blue bar appears at the top of the window
  25. 5. Use the file picker to select your tweets.js
  26. 6. Wait a long time for all your Tweets to vanish
  27.  
  28. If the process is interrupted at any time, you can enter how many Tweets have been deleted in the previous run to not start at zero again.
  29.  
  30. # How it works
  31. Never use something like this from an untrusted source. The script intercepts requests from your browser to Twitter and replaces the Tweet-IDs
  32. with IDs from your tweets.js file. This allows it to access the old Tweets and delete them.
  33.  
  34. XHR interception inspired by https://github.com/ttodua/Tamper-Request-Javascript-Tool
  35. Faster deletion inspired by https://github.com/Lyfhael/DeleteTweets
  36.  
  37. Copyright 2023 Nobody
  38. 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:
  39. 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.
  40. */
  41.  
  42. var TweetsXer = {
  43. allowed_requests: [],
  44. dId: "exportUpload",
  45. tIds: [],
  46. tId: "",
  47. ratelimitreset: 0,
  48. more: '[data-testid="tweet"] [aria-label="More"][data-testid="caret"]',
  49. skip: 0,
  50. total: 0,
  51. dCount: 0,
  52. lastHeaders: {},
  53. deleteURL: 'https://twitter.com/i/api/graphql/VaenaVgh5q5ih7kvyVjgtg/DeleteTweet',
  54. username: '',
  55.  
  56. init() {
  57. document.querySelector('header>div>div').setAttribute('class', '')
  58. TweetsXer.username = document.location.href.split('/')[3]
  59. this.createUploadForm()
  60. this.waitForFile(this.dId)
  61. },
  62.  
  63. sleep(ms) {
  64. return new Promise((resolve) => setTimeout(resolve, ms))
  65. },
  66. async waitForElemToExist(selector) {
  67. return new Promise((resolve) => {
  68. if (document.querySelector(selector)) {
  69. return resolve(document.querySelector(selector))
  70. }
  71.  
  72. var observer = new MutationObserver(() => {
  73. if (document.querySelector(selector)) {
  74. resolve(document.querySelector(selector))
  75. observer.disconnect()
  76. }
  77. })
  78.  
  79. observer.observe(document.body, {
  80. subtree: true,
  81. childList: true,
  82. })
  83. })
  84. },
  85.  
  86. initXHR() {
  87. if (typeof AjaxMonitoring_notfired == "undefined")
  88. var AjaxMonitoring_notfired = false
  89. if (!AjaxMonitoring_notfired) {
  90. AjaxMonitoring_notfired = true
  91.  
  92. /* NOTE: XMLHttpRequest actions happen in this sequence: at first "open"[readyState=1] happens, then "setRequestHeader", then "send", then "open"[readyState=2] */
  93.  
  94. var XHR_SendOriginal = XMLHttpRequest.prototype.send
  95. XMLHttpRequest.prototype.send = function () {
  96. XHR_SendOriginal.apply(this, arguments)
  97. }
  98.  
  99. var XHR_OpenOriginal = XMLHttpRequest.prototype.open
  100. XMLHttpRequest.prototype.open = function () {
  101. if (arguments[1] && arguments[1].includes("DeleteTweet")) {
  102. // POST /DeleteTweet
  103. TweetsXer.deleteURL = arguments[1]
  104. }
  105. XHR_OpenOriginal.apply(this, arguments)
  106. }
  107.  
  108. var XHR_SetRequestHeaderOriginal = XMLHttpRequest.prototype.setRequestHeader
  109. XMLHttpRequest.prototype.setRequestHeader = function (a, b) {
  110. TweetsXer.lastHeaders[a] = b
  111. XHR_SetRequestHeaderOriginal.apply(this, arguments)
  112. }
  113. }
  114. },
  115.  
  116. updateProgressBar() {
  117. document.getElementById('progressbar').setAttribute('value', this.dCount)
  118. document.getElementById("info").textContent = `${this.dCount} Tweets deleted`
  119. },
  120.  
  121. async waitForFile(dId) {
  122. let file = false
  123. while (file == false) {
  124. console.log("waiting for tweets.js from your data export")
  125. await TweetsXer.sleep(2000)
  126. let tn = document.getElementById(`${dId}_file`)
  127. if (tn.files && tn.files[0]) {
  128. file = true
  129. let fr = new FileReader()
  130. fr.onloadend = function (evt) {
  131. TweetsXer.skip = document.getElementById('skipCount').value
  132. console.log(`Skipping oldest ${TweetsXer.skip} Tweets`)
  133. let json = JSON.parse(evt.target.result.slice(26)) //24 chars was valid around year 2020
  134. TweetsXer.tIds = json.map((x) => x.tweet.id_str)
  135. TweetsXer.total = TweetsXer.tIds.length
  136. TweetsXer.tIds.reverse()
  137. TweetsXer.tIds = TweetsXer.tIds.slice(TweetsXer.skip)
  138. TweetsXer.dCount = TweetsXer.skip
  139. TweetsXer.tIds.reverse()
  140. document.getElementById('start').remove()
  141. document.getElementById(
  142. `${dId}_title`
  143. ).textContent = `Deleting ${TweetsXer.total} Tweets`
  144. TweetsXer.createProgressBar()
  145. document
  146. .querySelector('[data-testid="AppTabBar_Profile_Link"]')
  147. .click()
  148. TweetsXer.initXHR()
  149. TweetsXer.deleteTweets()
  150. }
  151. fr.readAsText(tn.files[0])
  152. }
  153. }
  154. },
  155.  
  156. createUploadForm() {
  157. var h2_class = document.querySelectorAll("h2")[1].getAttribute("class")
  158. var div = document.createElement("div")
  159. div.id = this.dId
  160. if (document.getElementById(this.dId))
  161. document.getElementById(this.dId).remove()
  162. div.innerHTML = `<style>#${this.dId}{ z-index:99999; position: sticky; top:0px; left:0px; width:auto; margin:0 auto; padding: 20px 10%; background:#87CEFA; opacity:0.9; } #${this.dId} > *{padding:5px;}</style>
  163. <div>
  164. <h2 class="${h2_class}" id="${this.dId}_title">TweetXer</h2>
  165. <p id="info">Enter how many Tweets to skip (useful for reruns) and select your tweets.js from your Twitter Data Export to start. </p>
  166. <p id="start">
  167. <input id="skipCount" type="number" value="0" />
  168. <input type="file" value="" id="${this.dId}_file" />
  169. </p>
  170. </div>`
  171. document.body.insertBefore(div, document.body.firstChild)
  172. },
  173.  
  174. createProgressBar() {
  175. let progressbar = document.createElement("progress")
  176. progressbar.setAttribute('id', "progressbar")
  177. progressbar.setAttribute('value', this.dCount)
  178. progressbar.setAttribute('max', this.total)
  179. progressbar.setAttribute('style', 'width:100%')
  180. document.getElementById(this.dId).appendChild(progressbar)
  181. },
  182.  
  183. async deleteTweets() {
  184. while (!('authorization' in this.lastHeaders)) {
  185. await this.sleep(2000)
  186. }
  187. while (this.tIds.length > 0) {
  188. this.tId = this.tIds.pop()
  189. let response = await fetch(this.deleteURL, {
  190. "headers": {
  191. "accept": "*/*",
  192. "accept-language": 'en-US,en;q=0.5',
  193. "authorization": this.lastHeaders.authorization,
  194. "content-type": "application/json",
  195. "sec-fetch-dest": "empty",
  196. "sec-fetch-mode": "cors",
  197. "sec-fetch-site": "same-origin",
  198. "x-client-transaction-id": this.lastHeaders['X-Client-Transaction-Id'],
  199. "x-client-uuid": this.lastHeaders['x-client-uuid'],
  200. "x-csrf-token": this.lastHeaders['x-csrf-token'],
  201. "x-twitter-active-user": "yes",
  202. "x-twitter-auth-type": "OAuth2Session",
  203. "x-twitter-client-language": 'en'
  204. },
  205. "referrer": `https://twitter.com/${this.username}/with_replies`,
  206. "referrerPolicy": "strict-origin-when-cross-origin",
  207. "body": `{\"variables\":{\"tweet_id\":\"${this.tId}\",\"dark_request\":false},\"queryId\":\"${this.deleteURL.split('/')[6]}\"}`,
  208. "method": "POST",
  209. "mode": "cors",
  210. "credentials": "include"
  211. })
  212. if (response.status == 200) {
  213. TweetsXer.dCount++
  214. TweetsXer.updateProgressBar()
  215. }
  216. else {
  217. console.log(response)
  218.  
  219. }
  220. }
  221. },
  222.  
  223. }
  224.  
  225. window.addEventListener('load', function () {
  226. // necessary when used as a userscript
  227. TweetsXer.init()
  228. }, false)
  229.  
  230. TweetsXer.init()