Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- //Get all <a> elements as an array
- var allHrefs = document.getElementsByTagName("a");
- //Declare another array to push <a>'s that are Twitter links
- var twitterHrefs = new Array();
- //This for loop cycles through all <a> elements
- for (i = 0; i < allHrefs.length; i++) {
- //Copy the URL to a string for code clarity, and declare booleans for use in this for loop.
- var twitterUrl = allHrefs[i].getAttribute("href");
- var isTwitter = false;
- //Does getAttribute *not* return a null? And if it indeed does not, does it contain "twitter"?
- //We test for null because the code was breaking before I included the test. I am not exactly sure
- //why getAttribute would return a null or what the exact case was that caused it.
- //This is an ad-hoc solution.
- if(twitterUrl != null) {
- isTwitter = twitterUrl.includes("twitter");
- }
- //if it did contain "twitter", then:
- if(isTwitter) {
- //Parse for an actual status link and not a link to someone's twitter page or the twitter homepage.
- //First, we must determine the prefix of the link -- http://www., http://, https://www, etc.
- //because this will determine the index of where we start parsing for the presence of a status URL.
- //We will do this in descending order of the index of the last "/". Since the longest possible prefix is
- //https://www, we will parse for that first. Then, http://www, then http://.
- //Declare the slash index
- var iSlash = 0;
- //Test the URL for prefixes in descending length.
- if(twitterUrl.includes("https://www.")) {
- iSlash = "https://www.twitter.com/".length;
- } else if(twitterUrl.includes("http://www.")) {
- iSlash = "http://www.twitter.com/".length;
- } else if(twitterUrl.includes("https://")) {
- iSlash = "https://twitter.com/".length;
- } else if(twitterUrl.includes("http://")) {
- iSlash = "http://twitter.com/".length;
- } else {
- iSlash = "twitter.com/".length;
- }
- //Make sure that twitterUrl.length is at least two characters longer than the value of iSlash, else
- //if(twitterUrl.indexOf("/",iSlash + 2) != -1) below could give an out-of-range error.
- if(twitterUrl.length > iSlash + 1) {
- //Now we can test for the presence of a "/" beyond the twitter base URL and the username.
- //To do this, we'll search for a "/" starting at iSlash plus two. This is basically a test assuming
- //a one-character Twitter handle, and it accomodates handles of any length.
- if(twitterUrl.indexOf("/",iSlash + 2) != -1) {
- //if we get here, then a slash exists. Test for the "/" being the last character:
- if(twitterUrl.indexOf("/",iSlash + 2) != twitterUrl.length) {
- //if we get here, then there are characters beyond the slash, and our URL is indeed a status! Hooray!
- alert("We have ourselves a twitter status: " + twitterUrl);
- twitterHrefs.push(allHrefs[i]);
- var twitterEmbed = document.createElement("iframe");
- twitterEmbed.setAttribute("border", 0);
- twitterEmbed.setAttribute("frameborder", 0);
- twitterEmbed.setAttribute("height", 250);
- twitterEmbed.setAttribute("width", 750);
- twitterEmbed.setAttribute("src", "http://twitframe.com/show?url=" + twitterUrl);
- allHrefs[i].parentNode.replaceChild(twitterEmbed, allHrefs[i]);
- }
- }
- }
- }
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement