rewritten replaceURLWithHTMLLinks to use entities
This commit is contained in:
parent
0190f9a28a
commit
4d49024d62
1 changed files with 41 additions and 65 deletions
106
TwittiaCore.js
106
TwittiaCore.js
|
@ -382,77 +382,52 @@ Twittia.prototype.authorizationHeader = function(method, url, params) {
|
||||||
function replaceURLWithHTMLLinks(text, entities, message_node) {
|
function replaceURLWithHTMLLinks(text, entities, message_node) {
|
||||||
var urls = entities.urls;
|
var urls = entities.urls;
|
||||||
|
|
||||||
var words = text.split(" ");
|
for(var i = 0; i<urls.length; i++) {
|
||||||
|
|
||||||
for(var word in words) {
|
|
||||||
var original = words[word] + "";
|
|
||||||
|
|
||||||
if(words[word].startsWith("http://t.co/")) {
|
var original = urls[i].url;
|
||||||
var replace = findTCOLongURL(words[word], urls);
|
var replace = urls[i].expanded_url == null ? original : urls[i].expanded_url;
|
||||||
|
|
||||||
|
if(replace.startsWith("http://bit.ly/") || replace.startsWith("http://j.mp/")) {
|
||||||
|
replaceShortened(replace, message_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
text = text.replace(original, "<a href='" + original + "'>" + replace + "</a>");
|
||||||
|
|
||||||
|
var media = null;
|
||||||
|
|
||||||
|
if(replace.startsWith("http://youtube.com/") || replace.startsWith("http://www.youtube.com/")) {
|
||||||
|
media = {
|
||||||
|
type: "twittia_youtube",
|
||||||
|
url: original,
|
||||||
|
media_url: "http://img.youtube.com/vi/" + getUrlVars(replace)["v"] + "/1.jpg"
|
||||||
|
}
|
||||||
|
|
||||||
if(replace != null) {
|
} else if (replace.startsWith("http://twitpic.com/")) {
|
||||||
if(replace.startsWith("http://bit.ly/") || replace.startsWith("http://j.mp/")) {
|
media = {
|
||||||
replaceShortened(replace, message_node);
|
type: "twittia_photo",
|
||||||
}
|
url: original,
|
||||||
|
media_url: "http://twitpic.com/show/mini/" + replace.substring("http://twitpic.com/".length)
|
||||||
words[word] = "<a href='" + words[word] + "'>" + replace + "</a>";
|
}
|
||||||
|
|
||||||
var media = null;
|
} else if (replace.startsWith("http://yfrog")) {
|
||||||
|
media = {
|
||||||
if(replace.startsWith("http://youtube.com/") || replace.startsWith("http://www.youtube.com/")) {
|
type: "twittia_photo",
|
||||||
media = {
|
url: original,
|
||||||
type: "twittia_youtube",
|
media_url: replace + ":small"
|
||||||
url: original,
|
}
|
||||||
media_url: "http://img.youtube.com/vi/" + getUrlVars(replace)["v"] + "/1.jpg"
|
}
|
||||||
}
|
|
||||||
|
if(media) {
|
||||||
} else if (replace.startsWith("http://twitpic.com/")) {
|
if(entities.media) {
|
||||||
media = {
|
entities.media.push(media);
|
||||||
type: "twittia_photo",
|
|
||||||
url: original,
|
|
||||||
media_url: "http://twitpic.com/show/mini/" + replace.substring("http://twitpic.com/".length)
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (replace.startsWith("http://yfrog")) {
|
|
||||||
media = {
|
|
||||||
type: "twittia_photo",
|
|
||||||
url: original,
|
|
||||||
media_url: replace + ":small"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(media) {
|
|
||||||
if(entities.media) {
|
|
||||||
entities.media.push(media);
|
|
||||||
} else {
|
|
||||||
entities.media = [media];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
words[word] = "<a href='" + words[word] + "'>" + words[word] + "</a>";
|
entities.media = [media];
|
||||||
}
|
}
|
||||||
} else if(words[word].startsWith("http://") || words[word].startsWith("https://") || words[word].startsWith("file://") || words[word].startsWith("ftp://")) {
|
|
||||||
if(words[word].startsWith("http://bit.ly/") || words[word].startsWith("http://j.mp/")) {
|
|
||||||
replaceShortened(words[word], message_node);
|
|
||||||
}
|
|
||||||
|
|
||||||
words[word] = "<a href='" + words[word] + "'>" + words[word] + "</a>";
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
text = words.join(" ");
|
|
||||||
|
|
||||||
return text; // text.replace(exp,"<a href='$1'>$1</a>");
|
|
||||||
}
|
|
||||||
|
|
||||||
function findTCOLongURL(url, urls) {
|
|
||||||
for(var u in urls) {
|
|
||||||
if(urls[u].url == url) {
|
|
||||||
return urls[u].expanded_url;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceTwitterLinks(text) {
|
function replaceTwitterLinks(text) {
|
||||||
|
@ -484,6 +459,7 @@ String.prototype.endsWith = function(suffix) {
|
||||||
function getUrlVars(url)
|
function getUrlVars(url)
|
||||||
{
|
{
|
||||||
var vars = [], hash;
|
var vars = [], hash;
|
||||||
|
if(url.indexOf("#") > -1) url = url.slice(0, url.indexOf("#"));
|
||||||
var hashes = url.slice(url.indexOf('?') + 1).split('&');
|
var hashes = url.slice(url.indexOf('?') + 1).split('&');
|
||||||
for(var i = 0; i < hashes.length; i++)
|
for(var i = 0; i < hashes.length; i++)
|
||||||
{
|
{
|
||||||
|
|
Reference in a new issue