Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 4.28 KB | None | 0 0
  1. $(document).ready(function () {
  2.  
  3.     //Get all <a> elements as an array
  4.     var allHrefs = document.getElementsByTagName("a");
  5.    
  6.     //Declare another array to push <a>'s that are Twitter links
  7.     var twitterHrefs = new Array();
  8.    
  9.     //This for loop cycles through all <a> elements    
  10.     for (i = 0; i < allHrefs.length; i++) {
  11.        
  12.        
  13.         //Copy the URL to a string for code clarity, and declare booleans for use in this for loop.
  14.         var twitterUrl = allHrefs[i].getAttribute("href");
  15.         var isTwitter = false;
  16.        
  17.         //Does getAttribute *not* return a null?  And if it indeed does not, does it contain "twitter"?
  18.         //We test for null because the code was breaking before I included the test.  I am not exactly sure
  19.         //why getAttribute would return a null or what the exact case was that caused it.
  20.         //This is an ad-hoc solution.
  21.         if(twitterUrl != null) {
  22.             isTwitter = twitterUrl.includes("twitter");
  23.             }
  24.        
  25.         //if it did contain "twitter", then:
  26.         if(isTwitter) {
  27.            
  28.             //Parse for an actual status link and not a link to someone's twitter page or the twitter homepage.
  29.             //First, we must determine the prefix of the link -- http://www., http://, https://www, etc.
  30.             //because this will determine the index of where we start parsing for the presence of a status URL.
  31.             //We will do this in descending order of the index of the last "/".  Since the longest possible prefix is
  32.             //https://www, we will parse for that first.  Then, http://www, then http://.
  33.            
  34.             //Declare the slash index
  35.             var iSlash = 0;
  36.            
  37.  
  38.            
  39.             //Test the URL for prefixes in descending length.
  40.             if(twitterUrl.includes("https://www.")) {
  41.                 iSlash = "https://www.twitter.com/".length;
  42.                
  43.             } else if(twitterUrl.includes("http://www.")) {
  44.                 iSlash = "http://www.twitter.com/".length;
  45.                
  46.             } else if(twitterUrl.includes("https://")) {
  47.                 iSlash = "https://twitter.com/".length;
  48.                
  49.             } else if(twitterUrl.includes("http://")) {
  50.                 iSlash = "http://twitter.com/".length;
  51.                
  52.             } else {
  53.                 iSlash = "twitter.com/".length;
  54.             }
  55.            
  56.            
  57.             //Make sure that twitterUrl.length is at least two characters longer than the value of iSlash, else
  58.             //if(twitterUrl.indexOf("/",iSlash + 2) != -1) below could give an out-of-range error.
  59.             if(twitterUrl.length > iSlash + 1) {
  60.                
  61.                 //Now we can test for the presence of a "/" beyond the twitter base URL and the username.
  62.                 //To do this, we'll search for a "/" starting at iSlash plus two.  This is basically a test assuming
  63.                 //a one-character Twitter handle, and it accomodates handles of any length.
  64.                 if(twitterUrl.indexOf("/",iSlash + 2) != -1) {
  65.                    
  66.                     //if we get here, then a slash exists.  Test for the "/" being the last character:
  67.                     if(twitterUrl.indexOf("/",iSlash + 2) != twitterUrl.length) {
  68.                        
  69.                         //if we get here, then there are characters beyond the slash, and our URL is indeed a status!  Hooray!
  70.                         alert("We have ourselves a twitter status: " + twitterUrl);
  71.                         twitterHrefs.push(allHrefs[i]);
  72.                         var twitterEmbed = document.createElement("iframe");
  73.                         twitterEmbed.setAttribute("border", 0);
  74.                         twitterEmbed.setAttribute("frameborder", 0);
  75.                         twitterEmbed.setAttribute("height", 250);
  76.                         twitterEmbed.setAttribute("width", 750);
  77.                         twitterEmbed.setAttribute("src", "http://twitframe.com/show?url=" + twitterUrl);
  78.                        
  79.  
  80.                        
  81.                         allHrefs[i].parentNode.replaceChild(twitterEmbed, allHrefs[i]);
  82.  
  83.                     }
  84.                 }
  85.             }
  86.                
  87.            
  88.            
  89.         }
  90.  
  91.     };
  92.  
  93. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement