Bookmarklet

Post Reply
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Bookmarklet

Post by tong »

Introduction

A bookmarklet is a bookmark stored in a web browser that contains JavaScript commands that add new features to the browser. When clicked, a bookmarklet performs one of a wide variety of operations, such as running a search query or extracting data from a table. Another name for bookmarklet is favelet or favlet, derived from favorite.

Code: Select all

javascript:(function(){
  //Statements returning a non-undefined type, e.g. assignments
})();

Code: Select all

javascript:{arbitrary script};void(0);
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Basic

Code: Select all

javascript:location.href='http://web.archive.org/save/'+document.location.href;

javascript:void(window.open('view-source:'+location));

javascript:alert('\tdocument.URL\n'+document.URL+'\n\tdocument.lastModified\n'+document.lastModified+'\n\tlocation\n'+location);
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

My Bookmarklet
javascript: open('https://www.siambit.me/viewno18.php?c92=1&search=' + window.getSelection());

javascript: open('https://www.siambit.me/viewno18.php?c92 ... ch=netflix ' + window.getSelection());

javascript: open('https://www.google.co.th/search?q=' + window.getSelection(), '_blank');

javascript: open('https://www.google.co.th/maps/search/' + window.getSelection());

javascript: open('https://translate.google.com/#en/th/' + window.getSelection());

javascript: open('https://rarbgenter.org/torrents.php?cat ... arch=1080p+' + window.getSelection() + '+' + location.hostname.split('.').reverse()[1] );

javascript: open('https://rarbgenter.org/torrents.php?cat ... ]=54&imdb=' + location.pathname.split('/')[2] );

javascript: open('http://www.imdb.com/find?q=' + window.getSelection());

javascript: open(window.getSelection().toString().replace(/[\s\n]/g, '').replace(/\[\?\]/g, '').replace(/^.+?http/i, 'http').replace(/(Site|Sharecode):/ig, ''));

javascript: open('https://www.youtube.com/results?sp=CAM% ... ry=Trailer ' + window.getSelection());

javascript: (function(){ var text = document.createElement('textarea'); document.body.appendChild(text); text.value = window.getSelection(); text.select(); document.execCommand('copy'); document.body.removeChild(text); })();

javascript: (function(){ var text = document.createElement('textarea'); document.body.appendChild(text); text.value = location.href; text.select(); document.execCommand('copy'); document.body.removeChild(text); })();

javascript: (function(){ var text = document.createElement('textarea'); document.body.appendChild(text); text.value = window.getSelection().toString().replace(/[\n\t\s]/g, ''); text.select(); document.execCommand('copy'); document.body.removeChild(text); })();

javascript: document.location = 'magnet:?xt=urn:btih:' + window.getSelection();

javascript: document.location = 'http://www.google.com/search?q=site%3As ... +-archiver+' + window.getSelection().toString().replace(/([a-z]+)\W?(\d+)/i, "$1-$2");

javascript: windows.open(location.href.split("/").slice(0,5).join("/") + '/search/?filters_rp_author={"name":"author_me","args":""}&filters_rp_chrono_sort={"name":"chronosort","args":""}&query=' + window.getSelection());

/search/?filters_rp_author={"name":"author","args":"100009339021932"}
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

window.location is object.
window.location.toString() will return window.location.href and it's text.
We can shorter as location.href.

location.href
location.hostname
location.pathname
location.origin

Code: Select all

window.location = {
	hash: '#myAnchor',
	host: 'davidwalsh.name:80',
	hostname: 'davidwalsh.name',
	href: 'https://davidwalsh.name:80/article.php?s=david+walsh+blog#myAnchor',
	origin: 'https://davidwalsh.name:80',
	pathname: '/article.php',
	port: '80',
	protocol: 'http:',
	search: '?s=david+walsh+blog',
	assign: 'function assign() { [native code] }',
	reload: 'function reload() { [native code] }'
	replace: 'function replace() { [native code] }',
}

window.alert('text'); or alert('text');
window.open('text'); or open('text');
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Copy selection text to clipboard

Code: Select all

javascript: (function(){
	let text = document.getSelection().toString();

	if (text)
	{
		let textarea = document.createElement('textarea');
		document.body.appendChild(textarea);
		textarea.value = text;
		textarea.select();
		document.execCommand('copy');
		document.body.removeChild(textarea);
	}
})();
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

New standard use document.location
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Copy text V2
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Problem 1 : No icon on bookmarket

Since the bookmark URL is javascript code which don't point to any webpage, so Chrome can't get the favicon.
The easy way to fix is use export/import bookmark method.
When you exported the bookmark, the output data is just simple HTML code.
Just add the ICON attribute into an Anchor tag. The value has to be data URI scheme data:[<media type>][;base64],<data>.
You can generate at https://base64.guru/converter/encode/image or just bookmark your target webpage and then export the bookmark.

Code: Select all

<A HREF="javascript: (function(){ alert('Testing') })();" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAACiElEQVQ4jQXBvW8URxgH4N/7zsx+3vkOfOYwhzHgSMESjlCQQFRRUiCBklBRpU2TPyFVREGJ6ClRKsoUqSJFSoHEVwEkCIHMCbAD8ge+3N3u7O7szJvnod6V26p3qm4aIiIAoCCCIJ2MEKvRRt6M7ct/bNZlnUfVx7HuHFlzw6/aslSKRSBBEi395WwwjO1ifHYd9a4rxs1gTffW4vGd33Vb2wVVEFfOcxyxSjQvdZtB/smAGqn3WrdMwzN5nHhOgjnBeu6NRyd0kjklkhqdKZNQv3QUeQnxm31FCuxCm4s6yqFR2kpUuBxiYRg1GSOHiwO7M1Mr3c/WI99i5xPFESxTEigE0vCC1jN5CpQQLdlZsTsrHQ6LaKatORZJdCnfDeRdwAsHralN2An5nm5QVlPrAylQSMVXlUxqurbqr65iJcfPM7ROdFf7Q2ldNN7bal55xcwQY+j8Cf3ay9VjYd/K0x3aWggF45wCi4hiz21R1F4xi0gScabl74+yZ/nGRRp1lGH8O6ObR3A5Fx0gezPvamFmxRCRPKa6pse78bXTNFrgXy5gUoFEeiluTUU7DwQyigTIIpQN7U/d8iBBjEVuACOCfsLBeycIgTQRiDCrQgAvpJQYcp77Kdx02szySR351htFtsUwl8+7oiHUevn2i+S/Ci+2nROlNbYmcqxvvt9I/xq3S3mwLX1zkkH06gC6asPFkbr746HIqHuPqt+et5MG178068t6tU9GiRd8mIdfn/kn792fY6dDqX76epCnAPDDpfzkUZxbQa6xPceoAwAfSkQZ7j2YPj7I4jTTunr78P4f2y8FgG1kc8dvHDeNl6kNg46a1/LuwA97qtosm30jk83/AS6eW9bnIQvRAAAAAElFTkSuQmCC">Testing</A>
Code need to be one liner.
When import the bookmarks, it will update an icon of all matched anchor href bookmarks.
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Problem 2 : Bookmarklet don't work in the new tab or chrome://

javascript bookmarklet does not work in new tab in chrome
https://stackoverflow.com/questions/56793918/javascript-bookmarklet-does-not-work-in-new-tab-in-chrome

JS bookmark does not work with the "new tab" page.
https://bugs.chromium.org/p/chromium/issues/detail?id=98871

It's security that javascript can run only in console of the new tab. This can be fix by wrap the code in data URI scheme data:[<media type>][;base64],<data>. Again use export/import bookmark method.

Code: Select all

<A HREF="data:text/html,<script> (function(){ alert('Testing 2') })(); </script>" ICON="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAACiElEQVQ4jQXBvW8URxgH4N/7zsx+3vkOfOYwhzHgSMESjlCQQFRRUiCBklBRpU2TPyFVREGJ6ClRKsoUqSJFSoHEVwEkCIHMCbAD8ge+3N3u7O7szJvnod6V26p3qm4aIiIAoCCCIJ2MEKvRRt6M7ct/bNZlnUfVx7HuHFlzw6/aslSKRSBBEi395WwwjO1ifHYd9a4rxs1gTffW4vGd33Vb2wVVEFfOcxyxSjQvdZtB/smAGqn3WrdMwzN5nHhOgjnBeu6NRyd0kjklkhqdKZNQv3QUeQnxm31FCuxCm4s6yqFR2kpUuBxiYRg1GSOHiwO7M1Mr3c/WI99i5xPFESxTEigE0vCC1jN5CpQQLdlZsTsrHQ6LaKatORZJdCnfDeRdwAsHralN2An5nm5QVlPrAylQSMVXlUxqurbqr65iJcfPM7ROdFf7Q2ldNN7bal55xcwQY+j8Cf3ay9VjYd/K0x3aWggF45wCi4hiz21R1F4xi0gScabl74+yZ/nGRRp1lGH8O6ObR3A5Fx0gezPvamFmxRCRPKa6pse78bXTNFrgXy5gUoFEeiluTUU7DwQyigTIIpQN7U/d8iBBjEVuACOCfsLBeycIgTQRiDCrQgAvpJQYcp77Kdx02szySR351htFtsUwl8+7oiHUevn2i+S/Ci+2nROlNbYmcqxvvt9I/xq3S3mwLX1zkkH06gC6asPFkbr746HIqHuPqt+et5MG178068t6tU9GiRd8mIdfn/kn792fY6dDqX76epCnAPDDpfzkUZxbQa6xPceoAwAfSkQZ7j2YPj7I4jTTunr78P4f2y8FgG1kc8dvHDeNl6kNg46a1/LuwA97qtosm30jk83/AS6eW9bnIQvRAAAAAElFTkSuQmCC">Testing 2</A>
Code need to be one liner.
tong
Site Admin
Posts: 2386
Joined: Fri 01 May 2009 8:55 pm

Re: Bookmarklet

Post by tong »

Problem 3 : Data URI scheme script don't work with document body

Forget the solution 2 and install a customize new tab extension.

Code: Select all

Custom New Tab URL
https://chrome.google.com/webstore/detail/custom-new-tab-url/mmjbdbjnoablegbkcklggeknkfcjkjia

New Tab Redirect
https://chrome.google.com/webstore/detail/new-tab-redirect/icpgjfneehieebagbmdbhnlpiopdcmna

Change New Tab
https://chrome.google.com/webstore/detail/change-new-tab/mocklpfdimiadpbgamlgehpgpodggahe
Then set to about:blank
Post Reply