简体   繁体   中英

JS Bookmarklets: Increase/decrease site's font size

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": 在此处输入图像描述

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM