Twitter Direct

Remove t.co tracking links from Twitter

当前为 2021-06-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Direct
  3. // @description Remove t.co tracking links from Twitter
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @version 2.1.2
  7. // @namespace https://github.com/chocolateboy/userscripts
  8. // @license GPL
  9. // @include https://twitter.com/
  10. // @include https://twitter.com/*
  11. // @include https://mobile.twitter.com/
  12. // @include https://mobile.twitter.com/*
  13. // @require https://unpkg.com/gm-compat@1.1.0/dist/index.iife.min.js
  14. // @run-at document-start
  15. // ==/UserScript==
  16.  
  17. /*
  18. * a pattern which matches the content-type header of responses we scan for
  19. * URLs: "application/json" or "application/json; charset=utf-8"
  20. */
  21. const CONTENT_TYPE = /^application\/json\b/
  22.  
  23. /*
  24. * document keys under which t.co URL nodes can be found when the document is a
  25. * plain object. not used when the document is an array.
  26. *
  27. * some densely-populated top-level paths don't contain t.co URLs, e.g.
  28. * $.timeline.
  29. */
  30. const DOCUMENT_ROOTS = [
  31. 'data',
  32. 'globalObjects',
  33. 'inbox_initial_state',
  34. 'users',
  35. ]
  36.  
  37. /*
  38. * keys of "legacy" objects which URL data is known to be found in/under,
  39. * e.g. we're interested in legacy.user_refs.*, legacy.retweeted_status.* and
  40. * legacy.url, but not in legacy.created_at or legacy.reply_count etc.
  41. *
  42. * objects under the "legacy" key typically contain dozens of keys, but we only
  43. * need to probe/traverse a handful to find t.co URLs.
  44. *
  45. * typically this reduces the number of keys to iterate in a legacy object from
  46. * 30 on average (max 39) to 2 or 3
  47. */
  48. const LEGACY_KEYS = [
  49. 'binding_values',
  50. 'entities',
  51. 'extended_entities',
  52. 'quoted_status_permalink',
  53. 'retweeted_status',
  54. 'retweeted_status_result',
  55. 'user_refs',
  56. ]
  57.  
  58. /*
  59. * the minimum size (in bytes) of documents we deem to be "not small"
  60. *
  61. * we log (to the console) misses (i.e. no URLs ever found/replaced) in
  62. * documents whose size is greater than or equal to this value
  63. */
  64. const LOG_THRESHOLD = 1024
  65.  
  66. /*
  67. * nodes under these keys never contain t.co URLs so we can speed up traversal
  68. * by pruning (not descending) them
  69. */
  70. const PRUNE_KEYS = new Set([
  71. 'advertiser_account_service_levels',
  72. 'card_platform',
  73. 'clientEventInfo',
  74. 'ext',
  75. 'ext_media_color',
  76. 'features',
  77. 'feedbackInfo',
  78. 'hashtags',
  79. 'original_info',
  80. 'player_image_color',
  81. 'profile_banner_extensions',
  82. 'profile_banner_extensions_media_color',
  83. 'profile_image_extensions',
  84. 'profile_image_extensions_media_color',
  85. 'responseObjects',
  86. 'sizes',
  87. 'user_mentions',
  88. 'video_info',
  89. ])
  90.  
  91. /*
  92. * a map from URI paths (strings) to the replacement count for each path. used
  93. * to keep a running total of the number of replacements in each document type
  94. */
  95. const STATS = {}
  96.  
  97. /*
  98. * a pattern which matches the domain(s) we expect data (JSON) to come from.
  99. * responses which don't come from a matching domain are ignored.
  100. */
  101. const TWITTER_API = /^(?:(?:api|mobile)\.)?twitter\.com$/
  102.  
  103. /*
  104. * a list of document URIs (paths) which are known to not contain t.co URLs and
  105. * which therefore don't need to be processed
  106. */
  107. const URL_BLACKLIST = new Set([
  108. '/i/api/2/badge_count/badge_count.json',
  109. '/i/api/graphql/articleNudgeDomains',
  110. '/i/api/graphql/TopicToFollowSidebar',
  111. ])
  112.  
  113. /*
  114. * object keys whose corresponding values may be t.co URLs
  115. */
  116. const URL_KEYS = new Set(['url', 'string_value'])
  117.  
  118. /*
  119. * return a truthy value (the URL itself) if the supplied value is a valid URL
  120. * (string), falsey otherwise
  121. */
  122. const checkUrl = (function () {
  123. // this is faster than using the URL constructor (in v8), which incurs
  124. // the overhead of using a try/catch block
  125. const urlPattern = /^https?:\/\/\w/i
  126.  
  127. // no need to coerce the value to a string as RegExp#test does that
  128. // automatically
  129. //
  130. // https://tc39.es/ecma262/#sec-regexp.prototype.test
  131. return value => urlPattern.test(value) && value
  132. })()
  133.  
  134. /*
  135. * replace the built-in XHR#send method with a custom version which swaps in our
  136. * custom response handler. once done, we delegate to the original handler
  137. * (this.onreadystatechange)
  138. */
  139. const hookXHRSend = oldSend => {
  140. return /** @this {XMLHttpRequest} */ function send (body = null) {
  141. const oldOnReadyStateChange = this.onreadystatechange
  142.  
  143. this.onreadystatechange = function (event) {
  144. if (this.readyState === this.DONE && this.responseURL && this.status === 200) {
  145. onResponse(this, this.responseURL)
  146. }
  147.  
  148. if (oldOnReadyStateChange) {
  149. oldOnReadyStateChange.call(this, event)
  150. }
  151. }
  152.  
  153. oldSend.call(this, body)
  154. }
  155. }
  156.  
  157. /*
  158. * return true if the supplied value is an array or plain object, false otherwise
  159. */
  160. const isObject = value => value && (typeof value === 'object')
  161.  
  162. /*
  163. * return true if the supplied value is a plain object, false otherwise
  164. *
  165. * only used with JSON data, so doesn't need to be foolproof
  166. */
  167. const isPlainObject = (function () {
  168. const toString = {}.toString
  169. return value => toString.call(value) === '[object Object]'
  170. })()
  171.  
  172. /*
  173. * return true if the supplied value is a t.co URL (string), false otherwise
  174. */
  175. const isTrackedUrl = (function () {
  176. // this is faster (in v8) than using the URL constructor (and a try/catch
  177. // block)
  178. const urlPattern = /^https?:\/\/t\.co\/\w+$/
  179.  
  180. // no need to coerce the value to a string as RegExp#test does that
  181. // automatically
  182. return value => urlPattern.test(value)
  183. })()
  184.  
  185. /*
  186. * replacement for Twitter's default handler for XHR requests. we transform the
  187. * response if it's a) JSON and b) contains URL data; otherwise, we leave it
  188. * unchanged
  189. */
  190. const onResponse = (xhr, uri) => {
  191. const contentType = xhr.getResponseHeader('Content-Type')
  192.  
  193. if (!CONTENT_TYPE.test(contentType)) {
  194. return
  195. }
  196.  
  197. const url = new URL(uri)
  198.  
  199. // exclude e.g. the config-<date>.json file from pbs.twimg.com, which is the
  200. // second biggest document (~500K) after home_latest.json (~700K)
  201. if (!TWITTER_API.test(url.hostname)) {
  202. return
  203. }
  204.  
  205. const json = xhr.responseText
  206. const size = json.length
  207.  
  208. // fold paths which differ only in the user or query ID, e.g.:
  209. //
  210. // /2/timeline/profile/1234.json -> /2/timeline/profile.json
  211. // /i/api/graphql/abc123/UserTweets -> /i/api/graphql/UserTweets
  212. //
  213. const path = url.pathname
  214. .replace(/\/\d+\.json$/, '.json')
  215. .replace(/^(.+?\/graphql\/)[^\/]+\/(.+)$/, '$1$2')
  216.  
  217. if (URL_BLACKLIST.has(path)) {
  218. return
  219. }
  220.  
  221. let data
  222.  
  223. try {
  224. data = JSON.parse(json)
  225. } catch (e) {
  226. console.error(`Can't parse JSON for ${uri}:`, e)
  227. return
  228. }
  229.  
  230. if (!isObject(data)) {
  231. return
  232. }
  233.  
  234. const newPath = !(path in STATS)
  235. const count = transform(data, path)
  236.  
  237. STATS[path] = (STATS[path] || 0) + count
  238.  
  239. if (!count) {
  240. if (!STATS[path] && size > LOG_THRESHOLD) {
  241. console.debug(`no replacements in ${path} (${size} B)`)
  242. }
  243.  
  244. return
  245. }
  246.  
  247. const descriptor = { value: JSON.stringify(data) }
  248. const clone = GMCompat.export(descriptor)
  249.  
  250. GMCompat.unsafeWindow.Object.defineProperty(xhr, 'responseText', clone)
  251.  
  252. const replacements = 'replacement' + (count === 1 ? '' : 's')
  253.  
  254. console.debug(`${count} ${replacements} in ${path} (${size} B)`)
  255.  
  256. if (newPath) {
  257. console.log(STATS)
  258. }
  259. }
  260.  
  261. /*
  262. * JSON.stringify +replace+ function used by +transform+ to traverse documents
  263. * and update their URL nodes in place.
  264. */
  265. const replacerFor = state => /** @this {any} */ function replacer (key, value) {
  266. // exclude subtrees which never contain t.co URLs
  267. if (PRUNE_KEYS.has(key)) {
  268. return 0 // a terminal value to stop traversal
  269. }
  270.  
  271. // we only care about the "card_url" property in binding_values
  272. // objects/arrays. exclude the other 24 properties
  273. if (key === 'binding_values') {
  274. if (Array.isArray(value)) {
  275. const found = value.find(it => it?.key === 'card_url')
  276. return found ? [found] : 0
  277. } else if (isPlainObject(value)) {
  278. return { card_url: (value.card_url || 0) }
  279. }
  280. }
  281.  
  282. // expand t.co URL nodes in place
  283. //
  284. // note this comes before the "legacy" check because legacy.url is a common
  285. // location and it needs to be modified in place rather than transferred to
  286. // a new object with a subset of the keys. this doesn't apply to the other
  287. // legacy keys as they all point to objects/arrays
  288. if (URL_KEYS.has(key) && isTrackedUrl(value)) {
  289. const { seen, unresolved } = state
  290.  
  291. let expandedUrl
  292.  
  293. if ((expandedUrl = seen.get(value))) {
  294. this[key] = expandedUrl
  295. ++state.count
  296. } else if ((expandedUrl = checkUrl(this.expanded_url || this.expanded))) {
  297. seen.set(value, expandedUrl)
  298. this[key] = expandedUrl
  299. ++state.count
  300. } else {
  301. let targets = unresolved.get(value)
  302.  
  303. if (!targets) {
  304. unresolved.set(value, targets = [])
  305. }
  306.  
  307. targets.push({ target: this, key })
  308. }
  309. }
  310.  
  311. // reduce the keys under this.legacy (typically around 30) to the handful we
  312. // care about
  313. if (key === 'legacy' && isPlainObject(value)) {
  314. // we could use an array, but it doesn't appear to be faster (in v8)
  315. const filtered = {}
  316.  
  317. for (let i = 0; i < LEGACY_KEYS.length; ++i) {
  318. const key = LEGACY_KEYS[i]
  319.  
  320. if (key in value) {
  321. filtered[key] = value[key]
  322. }
  323. }
  324.  
  325. return filtered
  326. }
  327.  
  328. // shrink terminals (don't waste space/memory in the (discarded) JSON)
  329. return isObject(value) ? value : 0
  330. }
  331.  
  332. /*
  333. * replace t.co URLs with the original URL in all locations in the document
  334. * which may contain them
  335. *
  336. * returns the number of substituted URLs
  337. */
  338. const transform = (data, path) => {
  339. const seen = new Map()
  340. const unresolved = new Map()
  341. const state = { count: 0, seen, unresolved }
  342. const replacer = replacerFor(state)
  343.  
  344. // [1] top-level tweet or user data (e.g. /favorites/create.json)
  345. if (Array.isArray(data) || ('id_str' in data) /* [1] */) {
  346. JSON.stringify(data, replacer)
  347. } else {
  348. for (const key of DOCUMENT_ROOTS) {
  349. if (key in data) {
  350. JSON.stringify(data[key], replacer)
  351. }
  352. }
  353. }
  354.  
  355. for (const [url, targets] of unresolved) {
  356. const expandedUrl = seen.get(url)
  357.  
  358. if (expandedUrl) {
  359. for (const target of targets) {
  360. target.target[target.key] = expandedUrl
  361. ++state.count
  362. }
  363.  
  364. unresolved.delete(url)
  365. }
  366. }
  367.  
  368. if (unresolved.size) {
  369. console.warn(`unresolved URIs (${path}):`, Object.fromEntries(state.unresolved))
  370. }
  371.  
  372. return state.count
  373. }
  374.  
  375. /*
  376. * replace the default XHR#send with our custom version, which scans responses
  377. * for tweets and expands their URLs
  378. */
  379. const xhrProto = GMCompat.unsafeWindow.XMLHttpRequest.prototype
  380.  
  381. xhrProto.send = GMCompat.export(hookXHRSend(xhrProto.send))