通过修改Chrome本地配置文件Preferences设置Chrome浏览器缩放比

背景: 开发时遇见有些电脑分辨率过于夸张,导致Chrome页面显示异常,很多元素出现错位、遮挡、无法在屏幕中显示等情况,如果可以反向缩放一下,让Chrome的显示接近100%缩放的状态,就会舒服很多。首先想到使用启动参数–force-device-scale-factor=1.5调整Chrome缩放,方便快捷,但是打开的Chrome总感觉怪怪的,该方法让Chrome全局都缩放了,会导致书签栏等组件挤压网页正文显示范围。那么,除了启动参数有没有什方法可以让Chrome只改变网页正文区域大小,而软件整体UI比例不变呢,下面就为大家分享通过修改Chrome本地配置文件Preferences设置Chrome浏览器缩放比的方法

文件位置: %localappdata%\Google\Chrome\User Data\Default\Preferences

搜索关键词: partition

字典结构:

// 在你配置文件中没有的字段可自行添加
"partition": {
    "default_zoom_level": {
        "x": -1.5778829311823859
    },
    "per_host_zoom_levels": {
        "x": {
            "39.99.254.217": {
                "last_modified": "13320153203764482",
                "zoom_level": -1.5778829311823859
            },
            "github.com": {
                "last_modified": "13331198885381947",
                "zoom_level": -0.5778829311823857
            },
        }
    }
}

zoom_level的求值公式为 l o g 1.2 缩放比 log_{1.2}{缩放比} log1.2缩放比,例
− 1.5778829311823859 ≈ l o g 1.2 0.75 = l o g 0.75 l o g 1.2 -1.5778829311823859 \approx log_{1.2}{0.75} = \frac{log{0.75}}{log{1.2}} 1.5778829311823859log1.20.75=log1.2log0.75

from math import log
def get_zoom_level(zoom: float) -> float:
    """
    缩放比求配置文件zoom_level的值
    
    :param zoom: 缩放比,例:0.75
    """
    return log(zoom, 1.2)

last_modified的求值方式为

from typing import Union
import datetime
import time

salt = (datetime.datetime(1970, 1, 1) - datetime.datetime(1601, 1, 1)).total_seconds()

def to_chrome_timestamp(timestamp=None) -> str:
    """
    时间戳 转 Chrome时间戳
    
    :param timestamp: 时间戳,如果不填写,则默认为当前时间
    """
    if not timestamp:
        timestamp = time.time()
    return str(int((float(timestamp) + salt) * 1000000))

def from_chrome_timestamp(chrome_timestamp, format_str=None) -> Union[float, str]:
    """
    Chrome时间戳 转 时间戳or日期字符串
    
    :param chrome_timestamp: Chrome时间戳
    :param format_str: 格式化字符串,用于格式化输出日期字符串,如不填写输出时间戳
    """
    if not format_str:
        return int(chrome_timestamp) / 1000000 - salt
    return time.strftime(format_str, time.localtime(int(chrome_timestamp) / 1000000 - salt))

timestamp = time.time()
print(f'原时间戳:{timestamp}')
chrome_timestamp = to_chrome_timestamp(timestamp)
print(f'Chrome时间戳:{chrome_timestamp}')
print(f'转回时间戳:{from_chrome_timestamp(chrome_timestamp)}')
print(f'转回日期时间字符串:{from_chrome_timestamp(chrome_timestamp, "%Y-%m-%d %H:%M:%S")}')

基础版开箱即用:

import os
import math
from time import time
from datetime import datetime
import json


def set_zoom_level(zoom: float, host: str = None) -> None:
    """
    设置Chrome浏览器缩放比
    
    :param host: 域名,如果不填写或填写default,则设置默认缩放比
    :param zoom: 缩放比,例:0.75
    """
    # 计算zoom_level
    zoom_level = math.log(float(zoom), 1.2)
    # 获取配置文件路径
    preferences_path = os.getenv("LOCALAPPDATA") + r'\Google\Chrome\User Data\Default\Preferences'
    # 读取配置文件
    with open(preferences_path, mode='r', encoding='utf8') as pf:
        preferences_file_text = pf.read()
	
    # 配置文件文本转json
    preferences_file_json = json.loads(preferences_file_text)
    # 校验创建partition字典
    preferences_file_json['partition'] = preferences_file_json.get('partition', {})
    if host == 'default' or not host:
        # 设置默认缩放比
        preferences_file_json['partition']['default_zoom_level'] = {'x':zoom_level}
    else:
        # 逐级校验创建结构
        preferences_file_json['partition']['per_host_zoom_levels'] = preferences_file_json['partition'].get('per_host_zoom_levels', {})
        preferences_file_json['partition']['per_host_zoom_levels']['x'] = preferences_file_json['partition']['per_host_zoom_levels'].get('x', {})
        # 获取指定域名缩放字典
        chrome_timestamp = str(int((time() + (datetime(1970, 1, 1) - datetime(1601, 1, 1)).total_seconds()) * 1000000))
        host_dict = {'last_modified': chrome_timestamp, 'zoom_level': zoom_level}
        # 设置指定域名缩放比
        preferences_file_json['partition']['per_host_zoom_levels']['x'][host] = host_dict
    # 打印修改结果
    print(preferences_file_json['partition'])
    
    # 配置好的json转回文本
    preferences_file_text = json.dumps(preferences_file_json, ensure_ascii=False, separators=(',', ':'))
    # 写入配置文件
    with open(preferences_path, mode='w', encoding='utf8') as pf:
        pf.write(preferences_file_text)


set_zoom_level(1)
set_zoom_level(0.75, 'cn.bing.com')

Tip: 将页面恢复为默认缩放快捷键:ctrl + )

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值