summaryrefslogtreecommitdiff
path: root/readable.js
diff options
context:
space:
mode:
authorNick White <git@njw.me.uk>2010-10-23 22:49:25 +0100
committerNick White <git@njw.me.uk>2010-10-23 22:49:25 +0100
commitb5fdff68dec57116a092b08a99f1f4504da8881c (patch)
tree0febe8965ce4063ba7ddfcbbc3c5bd617753be81 /readable.js
Initial checkin
Diffstat (limited to 'readable.js')
-rwxr-xr-xreadable.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/readable.js b/readable.js
new file mode 100755
index 0000000..3d31c4f
--- /dev/null
+++ b/readable.js
@@ -0,0 +1,67 @@
+/*
+ * Readable - makes webpages more readable
+ *
+ * See COPYING file for copyright, license and warranty details.
+ *
+ */
+
+/* NOTE: if you're using this code in another function, move this
+ * line out of it, into the global scope */
+if(original === undefined) var original = false;
+
+/* count the number of <p> tags that are direct children of parenttag */
+function count_p(parenttag)
+{
+ var n = 0;
+ var c = parenttag.childNodes;
+ for (var i = 0; i < c.length; i++) {
+ if (c[i].tagName == "p" || c[i].tagName == "P")
+ n++;
+ }
+ return n;
+}
+
+/* if original is set, then the readable version is currently active,
+ * so switch to the original html */
+if (original) {
+ document.body.innerHTML = original;
+ for (var i = 0; i < document.styleSheets.length; i++)
+ document.styleSheets[i].disabled = false;
+ original = false
+ return 0;
+}
+
+original = document.body.innerHTML;
+
+var biggest_num = 0;
+var biggest_tag;
+
+/* search for tag with most direct children <p> tags */
+var t = document.getElementsByTagName("*");
+for (var i = 0; i < t.length; i++) {
+ var p_num = count_p(t[i]);
+ if (p_num > biggest_num) {
+ biggest_num = p_num;
+ biggest_tag = t[i];
+ }
+}
+
+if (biggest_num == 0) {
+ alert("Can't find any content");
+ return 1;
+}
+
+/* save and sanitise content of chosen tag */
+var fresh = document.createElement("div");
+fresh.innerHTML = biggest_tag.innerHTML;
+fresh.innerHTML = fresh.innerHTML.replace(/<\/?font[^>]*>/g, "");
+fresh.innerHTML = fresh.innerHTML.replace(/style="[^"]*"/g, "");
+fresh.innerHTML = fresh.innerHTML.replace(/<\/?a[^>]*>/g, "");
+
+for (var i = 0; i < document.styleSheets.length; i++)
+ document.styleSheets[i].disabled = true;
+
+document.body.innerHTML =
+ "<div style=\"width: 38em; margin: auto; text-align: justify;\">" +
+ "<h1 style=\"text-align: center\">" + document.title + "</h1>" +
+ fresh.innerHTML + "</div>";