From 4879b8078d4aa7339ca90e9859cfa7632543a25b Mon Sep 17 00:00:00 2001 From: Rinor Dreshaj Date: Fri, 13 May 2016 17:52:03 +0200 Subject: [PATCH 1/5] Adding autolink --- demos/php-proxy/js/bootstrap-linkpreview.js | 44 ++++++++++++++--- demos/php-proxy/js/main.js | 7 +-- library/js/bootstrap-linkpreview.js | 53 +++++++++++++++++++++ 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/demos/php-proxy/js/bootstrap-linkpreview.js b/demos/php-proxy/js/bootstrap-linkpreview.js index f852446..d4c6ce8 100644 --- a/demos/php-proxy/js/bootstrap-linkpreview.js +++ b/demos/php-proxy/js/bootstrap-linkpreview.js @@ -38,6 +38,20 @@ * limitations under the License. * ========================================================= */ + + /* AUTO LINKING */ +(function() { + var autoLink = function() { + var pattern = /(^|[\s\n]|)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi; + + return this.match(pattern)[0].trim(); + }; + + String.prototype['autoLink'] = autoLink; + +}).call(this); + + (function($) { var LinkPreview = function(element, options) { @@ -53,6 +67,7 @@ $element: null, $previewContainer: null, $refreshButton: null, + $autoRefresh: null, init: function(element, options) { @@ -77,6 +92,19 @@ }); } + + if (options && options.autoRefresh) { + this.$autoRefresh = $(options.autoRefresh); + + var that = this; + this.$element.keyup(function(event) { + that.emptyPreviewContainer(); + that.initUrlValue(); + var urli = that.url.autoLink(); + that.getSource(String(urli), that.renderPreview, that.renderError); + }); + } + this.getSource(this.url, this.renderPreview, this.renderError); }, @@ -148,7 +176,7 @@ renderPreview: function(url, data, that) { // old request - if (that.url !== url) { + if (that.url.autoLink() !== url.autoLink()) { return; } @@ -215,18 +243,21 @@ }, findTitleInDom: function($dom) { - return $dom.find("title").text() || - $dom.find("meta[name=title]").attr("content"); + return $dom.find("meta[property='og:title']").attr("content") || + $dom.find("title").text() || + $dom.find("meta[name=title]").attr("content"); }, findDescriptionInDom: function($dom) { - return $dom.find("meta[name=description]").attr("content"); + return $dom.find("meta[property='og:description']").attr("content") || + $dom.find("meta[name=description]").attr("content") || + $dom.find("div .description").text(); }, findImageInDom: function($dom) { - var imageSrc = $dom.find("meta[itemprop=image]").attr("content") || - $dom.find("link[rel=image_src]").attr("content") || + var imageSrc = $dom.find("meta[property='og:image'").attr("content") || $dom.find("meta[itemprop=image]").attr("content") || + $dom.find("link[rel=image_src]").attr("content") || this.findFirstImageInBody($dom.find("body")); // maybe the returned url is relative @@ -268,6 +299,7 @@ }, validateUrl: function(value) { + value = value.autoLink().trim(); return (/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i).test(value); } }; diff --git a/demos/php-proxy/js/main.js b/demos/php-proxy/js/main.js index 4113469..0e8d7ef 100644 --- a/demos/php-proxy/js/main.js +++ b/demos/php-proxy/js/main.js @@ -1,12 +1,13 @@ $(function() { - $("a").linkpreview({ - previewContainer: "#preview-container" - }); + // $("a").linkpreview({ + // previewContainer: "#preview-container" + // }); $("input").linkpreview({ previewContainer: "#preview-container2", refreshButton: "#refresh-button", previewContainerClass: "row-fluid", errorMessage: "Invalid URL", + autoRefresh: true, preProcess: function() { console.log("preProcess"); }, diff --git a/library/js/bootstrap-linkpreview.js b/library/js/bootstrap-linkpreview.js index d32d11b..2f8e6f1 100644 --- a/library/js/bootstrap-linkpreview.js +++ b/library/js/bootstrap-linkpreview.js @@ -38,6 +38,46 @@ * limitations under the License. * ========================================================= */ + + /* AUTO LINKING */ +(function() { + var autoLink, + __slice = [].slice; + + autoLink = function() { + var k, linkAttributes, option, options, pattern, v; + options = 1 <= arguments.length ? __slice.call(arguments, 0) : []; + + pattern = /(^|[\s\n]|)((?:https?|ftp):\/\/[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi; + if (!(options.length > 0)) { + return this.replace(pattern, "$2"); + } + option = options[0]; + linkAttributes = ((function() { + var _results; + _results = []; + for (k in option) { + v = option[k]; + if (k !== 'callback') { + _results.push(" " + k + "='" + v + "'"); + } + } + return _results; + })()).join(''); + return this.replace(pattern, function(match, space, url) { + var link; + link = (typeof option.callback === "function" ? option.callback(url) : void 0) || (url); + return "" + space + link; + }); + }; + + String.prototype['autoLink'] = autoLink; + +}).call(this); + + + + (function($) { var LinkPreview = function(element, options) { @@ -53,6 +93,7 @@ $element: null, $previewContainer: null, $refreshButton: null, + $autoRefresh: null, init: function(element, options) { @@ -77,6 +118,18 @@ }); } + + if (options && options.autoRefresh) { + this.$autoRefresh = $(options.autoRefresh); + + var that = this; + this.$element.keyup(function(event) { + that.emptyPreviewContainer(); + that.initUrlValue(); + that.getSource(that.url.autoLink().trim(), that.renderPreview, that.renderError); + }); + } + this.getSource(this.url, this.renderPreview, this.renderError); }, From a63900892f4cf0fa5c5457234f99e6055c5b1dbc Mon Sep 17 00:00:00 2001 From: Rinor Dreshaj Date: Fri, 13 May 2016 18:19:03 +0200 Subject: [PATCH 2/5] Fixed the readme --- README.md | 8 +++++- library/js/bootstrap-linkpreview.js | 42 +++++++---------------------- 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 69aefcc..2fdb63a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Call the library via javascript: $('.element').linkpreview() ``` -$('.element') can point to ``, `