Re: Bookmarklet to wrap the ill-formatted mails in W3C mailing list archives

Le lun 08/12/2003 à 18:08, Dominique Hazaël-Massieux a écrit :
> javascript:max_width=80; function add_newline(node,chars) { if (node.nodeType==node.ELEMENT_NODE) { cursor = chars ; for (i=0 ; i < node.childNodes.length; i++) { if (node.childNodes[i].nodeType==node.TEXT_NODE) { cursor = add_newline(node.childNodes[i],cursor) ; } } return cursor ; } else if (node.nodeType==node.TEXT_NODE) { text = node.data; text_new=''; cursor = chars; while (text.length > (max_width - cursor)) { if (text.indexOf('\n') <= (max_width - cursor) && text.indexOf('\n') >= 0) { cut = text.indexOf('\n'); } else { cut = text.substr(0,max_width-cursor).lastIndexOf(' '); } if (cut==-1) { cut = max_width - cursor ; } text_new += text.substr(0,cut) + '\n'; cursor = 0; text = text.substr(cut+1); } text_new += text; node.data = text_new; return (text.length); } else { return chars ; } } add_newline(document.getElementsByTagName('pre')[0],0)

And with the proper bookmarklet wrapping:
(function(){ max_width=80; function add_newline(node,chars) { if (node.nodeType==node.ELEMENT_NODE) { cursor = chars ; for (i=0 ; i < node.childNodes.length; i++) { if (node.childNodes[i].nodeType==node.TEXT_NODE) { cursor = add_newline(node.childNodes[i],cursor) ; } } return cursor ; } else if (node.nodeType==node.TEXT_NODE) { text = node.data; text_new=''; cursor = chars; while (text.length > (max_width - cursor)) { if (text.indexOf('\n') <= (max_width - cursor) && text.indexOf('\n') >= 0) { cut = text.indexOf('\n'); } else { cut = text.substr(0,max_width-cursor).lastIndexOf(' '); } if (cut==-1) { cut = max_width - cursor ; } text_new += text.substr(0,cut) + '\n'; cursor = 0; text = text.substr(cut+1); } text_new += text; node.data = text_new; return (text.length); } else { return chars ; } } add_newline(document.getElementsByTagName('pre')[0],0) })()

Dom
-- 
Dominique Hazaël-Massieux - http://www.w3.org/People/Dom/
W3C/ERCIM
mailto:dom@w3.org

Received on Monday, 8 December 2003 12:21:17 UTC