bookmarks.html search bookmarklet

a bookmarklet that enables search in exported bookmarks.html file

by Laurens Maneschijn

HTML

search bookmarks.html 

<hr>

drag this link into your bookmarks:
<a href="javascript:(function(){ if (window.bookmarks_handled) { return; } window.bookmarks_handled = true; function changeTag(el_old, newtag) { var el_new = document.createElement(newtag); el_old.replaceWith(el_new); while(el_old.firstChild) { el_new.appendChild(el_old.firstChild); } el_old.getAttributeNames().forEach(function(attr){ el_new.setAttribute(attr, el_old.attributes[attr].value); }); } document.querySelectorAll(&apos;dt&apos;).forEach(function(el){ changeTag(el, &apos;details&apos;); }); document.querySelectorAll(&apos;h3&apos;).forEach(function(el){ changeTag(el, &apos;summary&apos;); }); document.querySelectorAll(&apos;summary&apos;).forEach(function(el){ var d = el.closest(&apos;details&apos;); if(!d){return;} d.insertBefore(el, d.firstChild); }); document.querySelectorAll(&apos;a&apos;).forEach(function(el){ var p = el.parentNode; if(p.tagName === &apos;DETAILS&apos;) { p.parentNode.insertBefore(el, p); if (p.children.length === 0) { p.remove(); } } }); document.querySelectorAll(&apos;p&apos;).forEach(el =&gt; el.remove() ); document.querySelectorAll(&apos;img&apos;).forEach(el =&gt; el.remove() ); document.querySelectorAll(&apos;a[icon]&apos;).forEach(function(el){ var img = document.createElement(&apos;img&apos;); img.src = el.attributes[&apos;icon&apos;].value; el.prepend(img); }); document.querySelectorAll(&apos;a, summary&apos;).forEach(function(el){ var t = &apos;&apos;; if (el.attributes[&apos;add_date&apos;]) { var d = new Date(parseInt(el.attributes[&apos;add_date&apos;].value) * 1000); t += &apos;add_date: &apos;+ ( d.toISOString() + &apos;&apos;) + &apos;\n\n&apos;; } if (el.attributes[&apos;last_modified&apos;]) { var d = new Date(parseInt(el.attributes[&apos;last_modified&apos;].value) * 1000); t += &apos;last_modified: &apos;+ ( d.toISOString() + &apos;&apos;) + &apos;\n\n&apos;; } if (el.href) { t += el.href + &apos;\n\n&apos;; } if (el.textContent) { t +=...

JavaScript

return; // ABORT for jsfiddle

(function(){
	if (window.bookmarks_handled) {
		return;
	}
	window.bookmarks_handled = true;


	/*
	function replaceElement(el_old, el_new) {
	//	el_old.parentNode.replaceChild(el_new, el_old);
		el_old.replaceWith(el_new);
	}
	function moveChildren(el_old, el_new) {
		// NB: do not use children, it does not have texnodes
	//	el_old.childNodes.forEach(function(el){
	//		el_new.appendChild(el);
	//	});
		while(el_old.firstChild) {
			el_new.appendChild(el_old.firstChild);
		}
	}

	function wrap(toWrap, wrapper) {
		wrapper = wrapper || document.createElement('div');
		toWrap.parentNode.insertBefore(wrapper, toWrap);
		return wrapper.appendChild(toWrap);
	}

	*/

	function changeTag(el_old, newtag) {
		// replace old element with new empty element:
		var el_new = document.createElement(newtag);
		el_old.replaceWith(el_new);

		// move children (including textnodes):
		while(el_old.firstChild) {
			el_new.appendChild(el_old.firstChild);
		}

		// copy attributes:
		el_old.getAttributeNames().forEach(function(attr){
			el_new.setAttribute(attr, el_old.attributes[attr].value);
		});
	}

	// change all <dt> to <details> , to make it a collapsable tree:
	document.querySelectorAll('dt').forEach(function(el){
		changeTag(el, 'details');
	});
	document.querySelectorAll('h3').forEach(function(el){
		changeTag(el, 'summary');
	});

	document.querySelectorAll('summary').forEach(function(el){
		var d = el.closest('details');
		if(!d){return;}
		d.insertBefore(el, d.firstChild);
	});

	// Original: 'dt > a' , now: 'details > a'.
	// No need for details around a single a element, so unwrap it and remove the empty details element:
	document.querySelectorAll('a').forEach(function(el){
		var p = el.parentNode;
		if(p.tagName === 'DETAILS') {
			p.parentNode.insertBefore(el, p);
			if (p.children.length === 0) {
				p.remove();
			}
		}
	});

	document.querySelectorAll('p').forEach(el => el.remove() );

	document.querySelectorAll('img').forEach(el =>...