【问题标题】:JS Bookmarklets: Increase/decrease site's font sizeJS Bookmarklets:增加/减少网站的字体大小
【发布时间】:2020-05-30 20:47:51
【问题描述】:

我使用here 的代码来扩大网站:


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"

但这最近在我的 Chrome (Version 81.0.4044.138 (Official Build) (64-bit)) 上停止工作。我很好奇为什么,以及任何可行的替代方案。

ٍٍ解释“停止工作”:使用后页面空白,有时显示“14px”:

【问题讨论】:

    标签: javascript font-size bookmarklet


    【解决方案1】:

    This article from 2ality

    Finish with undefined:如果你不返回(或finish with!)undefined,结果将替换当前网页。 [注意:Chrome 和 Safari 等 Webkit 浏览器永远不会替换页面,只有 Firefox 等非 Webkit 浏览器才会这样做。]

    但 Chrome 已经改变了这种行为。

    Chrome 现在运行类似

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

    书签的最小可重现示例是javascript: "14px"

    最简单的解决方案是在脚本末尾添加undefined


    Firefox 执行类似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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 2021-03-13
      • 2012-01-30
      • 2014-11-15
      • 2020-01-21
      • 1970-01-01
      相关资源
      最近更新 更多