Unshortcut links on twitter.com

replace musky t.co links with actual links to actual web sites

  1. // ==UserScript==
  2. // @name Unshortcut links on twitter.com
  3. // @description replace musky t.co links with actual links to actual web sites
  4. // @namespace Itsnotlupus Industries
  5. // @author itsnotlupus
  6. // @license MIT
  7. // @version 1.2.1
  8. // @match https://twitter.com/*
  9. // @match https://platform.twitter.com/*
  10. // @grant none
  11. // @require https://greatest.deepsurf.us/scripts/468394-itsnotlupus-tiny-utilities/code/utils.js?version=1247001
  12. // @require https://greatest.deepsurf.us/scripts/472943-itsnotlupus-middleman/code/middleman.js?version=1239323
  13. // ==/UserScript==
  14.  
  15. /* jshint esversion:11 */
  16.  
  17. log("This script is deprecated. Please switch to https://greatest.deepsurf.us/en/scripts/474045-twitter-prime to continue getting updates.");
  18.  
  19. function unshortcut(obj) {
  20. const map = {};
  21. // 1st pass: gather associations between t.co and actual URLs
  22. (function populateURLMap(obj) {
  23. if (obj.url && obj.expanded_url) map[obj.url] = obj.expanded_url;
  24. Object.keys(obj).forEach(k => obj[k] && typeof obj[k] == "object" && populateURLMap(obj[k]));
  25. })(obj);
  26. // 2d pass: replace (almost) any string that contains a t.co string
  27. (function replaceURLs(obj) {
  28. Object.keys(obj).forEach(key => ({
  29. string() { if (map[obj[key]] && key!=='full_text') obj[key] = map[obj[key]]; },
  30. object() { if (obj[key] != null) replaceURLs(obj[key]); }
  31. }[typeof obj[key]]?.()));
  32. })(obj);
  33. return obj;
  34. }
  35.  
  36. async function responseHandler(req, res, err) {
  37. return Response.json(unshortcut(await res.json()), {
  38. status: res.status,
  39. headers: res.headers
  40. });
  41. }
  42.  
  43.  
  44. middleMan.addHook("https://twitter.com/i/api/graphql/*", { responseHandler });
  45. middleMan.addHook("https://cdn.syndication.twimg.com/tweet-result?*", { responseHandler });