JS Bookmarklets: Increase/decrease site's font size

Viewed 306

I was using the code from here to enlargen sites:


var p=document.getElementsByTagName('*');

for(i=0;i<p.length;i++) {
  if(p[i].style.fontSize){
    var s=parseInt(p[i].style.fontSize.replace("px",""));
}
else {
  var s=12;
}

s+=2;
p[i].style.fontSize=s+"px"

But this has recently stopped working on my Chrome (Version 81.0.4044.138 (Official Build) (64-bit)). I am curious as to why, and any working alternatives.

ٍٍExplaining "stopped working": After using it, the page goes blank, and sometimes shows "14px": enter image description here

1 Answers

This article from 2ality says

Finish with undefined: If you don’t return (or finish with!) undefined, the result replaces the current web page. [Note: Webkit browsers such as Chrome and Safari never replace a page, only non-Webkit browsers such as Firefox do.]

but Chrome has changed this behavior.

Chrome now runs a code like

if (typeof bookmark_reslt === "string") {
  document.body.innerHTML = bookmark_reslt
}

A minimal reproducible example of the bookmarklet is javascript: "14px"

The easiest solution is to add undefined in the end of the script.


Firefox executes a code like document.body.innerHTML = String(bookmark_reslt)

// examples to see how `toString` affects Bookmarklets in Firefox
javascript: a = {}; a; // <body>[object Object]</body>
javascript: a = {}; a.toString = ()=> 2; a; // <body>2</body>
Related