Skip to content

Instantly share code, notes, and snippets.

@onigetoc
Last active July 23, 2016 06:27
Show Gist options
  • Save onigetoc/201f64252108b86b3af0 to your computer and use it in GitHub Desktop.
Save onigetoc/201f64252108b86b3af0 to your computer and use it in GitHub Desktop.
readme.js | jQuery Readability plugin | Show web page content with the Readability API
(function ($) {
$.fn.readme = function (options) {
var showimg = '';
var html = '';
var exclude = '<i><em><a><p><h1><h2><h3><h4><h5><h6><b><br><p><ul><ol><li><pre><code><img><hr><strong><blockquote>';
var defaults = {
url: '',
strip_tags: true,
featured_image: true
};
var plugin = this;
plugin.settings = {};
plugin.init = function () {
plugin.settings = $.fn.extend({}, defaults, options);
console.log('plug setting ' + plugin.settings);
if (options.init) options.init.call(plugin);
var opts = plugin.settings;
var url = opts.url.trim();
var token = "YOUR_READABILITY_TOKEN";
$.getJSON("https://www.readability.com/api/content/v1/parser?url=" + url + "&token=" + token + "&callback=?", function (data) {
//alert(data.content);
var lead_img = +data.lead_image_url;
if (lead_img !== 0 && opts.featured_image)
showimg = '<img src="' + data.lead_image_url + '" alt="New Year Times Square 2014" width="auto">';
html += showimg + '<h1>' + data.title + '</h1>';
//html += data.content;
if (opts.strip_tags) {
html += strip_tags(data.content, exclude);
//alert('strip_tags');
} else {
html += data.content;
//alert(opts.strip_tags)
}
if (plugin.is("textarea")) {
return plugin.val(html);
} else if (plugin.is("input")) {
return plugin.val(html);
} else {
return plugin.html(html);
}
});
};
plugin.init();
};
function strip_tags(a, b) {
b = (((b || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join("");
return a.replace(/\x3c!--[\s\S]*?--\x3e|<\?(?:php)?[\s\S]*?\?>/gi, "").replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, function (a, c) {
return -1 < b.indexOf("<" + c.toLowerCase() + ">") ? a : ""
})
};
}(jQuery));
(function(e){function g(a,d){d=(((d||"")+"").toLowerCase().match(/<[a-z][a-z0-9]*>/g)||[]).join("");return a.replace(/\x3c!--[\s\S]*?--\x3e|<\?(?:php)?[\s\S]*?\?>/gi,"").replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,function(c,a){return-1<d.indexOf("<"+a.toLowerCase()+">")?c:""})}e.fn.readme=function(a){var d="",c="",h={url:"",strip_tags:!0,featured_image:!0},b=this;b.settings={};b.init=function(){b.settings=e.fn.extend({},h,a);console.log("plug setting "+b.settings);a.init&&a.init.call(b);var f=b.settings,
k=f.url.trim();e.getJSON("https://www.readability.com/api/content/v1/parser?url="+k+"&token=YOUR_READABILITY_TOKEN&callback=?",function(a){0!==+a.lead_image_url&&f.featured_image&&(d='<img src="'+a.lead_image_url+'" alt="New Year Times Square 2014" width="auto">');c+=d+"<h1>"+a.title+"</h1>";c=f.strip_tags?c+g(a.content,"<i><em><a><p><h1><h2><h3><h4><h5><h6><b><br><p><ul><ol><li><pre><code><img><hr><strong><blockquote>"):c+a.content;return b.is("textarea")?b.val(c):b.is("input")?
b.val(c):b.html(c)})};b.init()}})(jQuery);
// HOW TO USE
$('#target').readme({
url: readurl,
strip_tags: true, // default true: OPTIONAL
featured_image: true // default true: OPTIONAL
});
// HOW TO USE: SIMPLE
$('#target').readme({ url: readurl});
// Readability api get your token: https://www.readability.com/developers/api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment