From 61d3ed891afca632df9bd265adb801075825ddb7 Mon Sep 17 00:00:00 2001
From: chylex <contact@chylex.com>
Date: Tue, 5 Sep 2017 21:47:22 +0200
Subject: [PATCH] Update t.co bypass to work for media and bio urls

---
 Resources/Scripts/code.js | 42 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)

diff --git a/Resources/Scripts/code.js b/Resources/Scripts/code.js
index 2db9438c..56a4d5b8 100644
--- a/Resources/Scripts/code.js
+++ b/Resources/Scripts/code.js
@@ -407,15 +407,49 @@
   })();
   
   //
-  // Block: Bypass t.co when clicking links.
+  // Block: Bypass t.co when clicking links and media.
   //
   $(document.body).delegate("a[data-full-url]", "click", function(e){
     $TD.openBrowser($(this).attr("data-full-url"));
     e.preventDefault();
   });
   
+  if (ensurePropertyExists(TD, "services", "TwitterUser", "prototype", "fromJSONObject")){
+    let prevFunc = TD.services.TwitterUser.prototype.fromJSONObject;
+    
+    TD.services.TwitterUser.prototype.fromJSONObject = function(){
+      let obj = prevFunc.apply(this, arguments);
+      let e = arguments[0].entities;
+      
+      if (e && e.url && e.url.urls && e.url.urls.length && e.url.urls[0].expanded_url){
+        obj.url = e.url.urls[0].expanded_url;
+      }
+      
+      return obj;
+    };
+  }
+  
+  if (ensurePropertyExists(TD, "services", "TwitterMedia", "prototype", "fromMediaEntity")){
+    let prevFunc = TD.services.TwitterMedia.prototype.fromMediaEntity;
+    
+    TD.services.TwitterMedia.prototype.fromMediaEntity = function(){
+      let obj = prevFunc.apply(this, arguments);
+      let e = arguments[0];
+      
+      if (e.expanded_url){
+        if (obj.url === obj.shortUrl){
+          obj.shortUrl = e.expanded_url;
+        }
+        
+        obj.url = e.expanded_url;
+      }
+      
+      return obj;
+    };
+  }
+  
   //
-  // Block: Bypass t.co and include additional information in context menus.
+  // Block: Include additional information in context menus.
   //
   $(document.body).delegate("a", "contextmenu", function(){
     let me = $(this)[0];
@@ -716,9 +750,7 @@
     
     $(".js-drawer[data-drawer='compose']").delegate(".js-account-list > .js-account-item", "click", onAccountClick);
     
-    if (!ensurePropertyExists(TD, "components", "AccountSelector", "prototype", "refreshPostingAccounts")){
-      return;
-    }
+    return if !ensurePropertyExists(TD, "components", "AccountSelector", "prototype", "refreshPostingAccounts");
     
     TD.components.AccountSelector.prototype.refreshPostingAccounts = appendToFunction(TD.components.AccountSelector.prototype.refreshPostingAccounts, function(){
       if (!this.$node.attr("td-account-selector-hook")){