Idea for addon: save selected part of webpage as MHT

Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.

Moderators: FranklinDM, Lootyhoof

andres99

Idea for addon: save selected part of webpage as MHT

Unread post by andres99 » 2018-11-05, 16:35

Quite some time ago there was an addon for old Opera (up to version 12), called QSaver (probably of Russian origin). It had a very fancy option to select a part of the web page and save it in MHT. And the feature actually worked (with styles!). I have not seen anything like this elsewhere but for Opera.

Now I tried it in Pale Moon (actually the New Moon fork) and it seems to work partly, actually even without the QSaver executable and just as a JS bookmarklet. I cannot code in JavaScript but perhaps the idea is worth picking up and turning it into an addon for Pale Moon? For now, this part of code as a bookmarklet saves simpler parts of webpages but the resulting files need to be renamed manually:

Code: Select all

javascript:(function(){var selWin=function(w){if(w.getSelection()!='')return w;for(var i=0,f,r;f=w.frames[i];i++){try{if(r=arguments.callee(f))return r}catch(e){}}};var ele,pEle,clone;var doctype='';var desc='';var doc=window.document;var loc=window.location;var win=selWin(window);if(win){doc=win.document;loc=win.location;var s=win.getSelection();var r=s.getRangeAt(0);pEle=r.commonAncestorContainer;ele=r.cloneContents();desc=s.toString().slice(0,50).replace(/\s+/g,' ').replace(/^\s+|\s+$/g,'')}else{pEle=doc.documentElement;ele=(doc.body||doc.getElementsByTagName('body')[0]).cloneNode(true)};while(pEle){if(pEle.nodeType==1){clone=pEle.cloneNode(false);clone.appendChild(ele);ele=clone};pEle=pEle.parentNode};var sel=doc.createElement('div');sel.appendChild(ele);var scripts=sel.getElementsByTagName('script');for(var i=scripts.length;i--;){scripts[i].parentNode.removeChild(scripts[i])};var h=ele.insertBefore(doc.createElement('head'),ele.firstChild);var title=doc.getElementsByTagName('title')[0];title=title?title.text:'untitled';var link=loc.href;var q='\'\'';h.appendChild(doc.createComment(' QSaver settings '+'/format=phf /method=simple /url='+q+link+q+(desc?' /text='+q+title+' ('+desc+')'+q:'')+' '));var t=doc.createElement('title');t.text=title;h.appendChild(t);var meta=doc.createElement('meta');meta.httpEquiv='content-type';meta.content='text/html; charset=utf-8';h.appendChild(meta);var base=doc.getElementsByTagName('base')[0];var b=base?base.cloneNode(false):doc.createElement('base');if(!b.href)b.href=link;h.appendChild(b);var styles=doc.styleSheets;for(var i=0,si;si=styles[i];i++){var style=doc.createElement('style');style.type='text/css';if(si.media.mediaText)style.media=si.media.mediaText;try{for(var j=0,rule;rule=si.cssRules[j];j++){style.appendChild(doc.createTextNode(rule.cssText+'\n'))}}catch(e){if(si.ownerNode)style=si.ownerNode.cloneNode(false)};h.appendChild(style)};var dt=doc.doctype;if(dt&&dt.name){doctype+='<!DOCTYPE '+dt.name;if(dt.publicId)doctype+=' PUBLIC \x22'+dt.publicId+'\x22';if(dt.systemId)doctype+=' \x22'+dt.systemId+'\x22';doctype+='>\n'};loc.href='data:text/phf;charset=UTF-8,'+encodeURIComponent(doctype+sel.innerHTML+'\n<!-- This document saved from '+link+' -->')})();

kbhasi

Re: Idea for addon: save selected part of webpage as MHT

Unread post by kbhasi » 2018-11-25, 11:26

As far as I know, there's already a similar extension called "MozArchiver", which can save into that format by selecting "Web Page, MHTML".