当前位置: 首页 > web >正文

003.chromium编译进阶-禁用css动画和禁用canvas渲染

一、目标:

  • 实现传入参数--disable-css-animation,禁用css动画
  • 实现传入参数--disable-canvas,禁用canvas渲染

阅读此篇博客前,请确保你已经可以完整的编译出自己的chromium了。
官方编译文档:https://chromium.googlesource.com/chromium/src/+/main/docs/windows_build_instructions.md

二、为何禁用动画

  • 浏览器做自动化有几个硬伤,1.带宽占用高,2.cpu占用高,3.内存占用高。
  • 虽然我们是用空间换时间,但还是希望cpu占用再降一点
  • 禁止图片访问可以节约大量带宽。禁用css动画和canvas渲染,对于有动画的网页,则可以节约大量cpu.

三、css动画案例

  • 创建一个有css动画的测试页面,将下面的代码复制到随便一个html,比如111.html
<!DOCTYPE html>   
<html lang="en">   
<head><meta charset="UTF-8"><title>无限CSS动画示例</title><style>body {display: flex;justify-content: center;align-items: center;min-height: 100vh;margin: 0;background-color: #1a1a1a;}.animated-box {width: 100px;height: 100px;background-color: #ff4757;animation: rotate 3s linear infinite,color-change 5s ease-in-out infinite;}@keyframes rotate {from {transform: rotate(0deg);}to {transform: rotate(360deg);}}@keyframes color-change {0% {background-color: #ff4757;}50% {background-color: #2ed573;}100% {background-color: #5352ed;}}</style>   
</head>   
<body><div class="animated-box"></div>   
</body>   
</html>

用浏览器打开这个页面,发现一个浏览器的cpu占用率就有大概5%左右。

四、修改chromium源码

  • 打开 \third_party\blink\renderer\core\css\resolver\style_resolver.cc
1.引用:
#include "base/command_line.h"
2.找到:
bool StyleResolver::ApplyAnimatedStyle(StyleResolverState& state,StyleCascade& cascade,const StyleRecalcContext& style_recalc_context) {
3.替换为:
bool StyleResolver::ApplyAnimatedStyle(StyleResolverState& state,StyleCascade& cascade,const StyleRecalcContext& style_recalc_context) {// 开始追加 =================================base::CommandLine* cmdLine = base::CommandLine::ForCurrentProcess();if (cmdLine->HasSwitch("disable-css-animation")) {return false;}// 结束追加 =================================
4.给render进程追加参数
  • 打开:/content/browser/renderer_host/render_process_host_impl.cc
command_line->AppendSwitchASCII(switches::kProcessType,switches::kRendererProcess);// 开始追加 =====================================const base::CommandLine* base_command_line = base::CommandLine::ForCurrentProcess();if (base_command_line->HasSwitch("disable-css-animation")) {command_line->AppendSwitch("disable-css-animation");}//结束追加 ===================================

五、检测:

  • 编译完成后再次检测刚刚页面的cpu占用,提示cpu占用大约只有0.2%左右了
  • 测试页面2:https://www.python-spider.com/challenge/login, 也是css动画导致cpu占用较高

六、如何禁用canvas

这里我就不多说了,和css动画原理相同。直接提供修改地址:

  • 打开 \third_party\blink\renderer\modules\canvas\htmlcanvas\html_canvas_element_module.cc
#include "base/command_line.h"V8RenderingContext* HTMLCanvasElementModule::getContext(HTMLCanvasElement& canvas,const String& context_id,const CanvasContextCreationAttributesModule* attributes,ExceptionState& exception_state) {// 开始追加 =================================base::CommandLine* cmdLine = base::CommandLine::ForCurrentProcess();if (cmdLine->HasSwitch("disable-canvas")) {return nullptr;}// 结束追加 =================================
http://www.xdnf.cn/news/4758.html

相关文章:

  • 【最新版】likeshop连锁点餐系统-PHP版+uniapp前端全开源
  • 【LangChain基础系列】深入全面掌握文本分类
  • pyorch中tensor的理解与操作(一)
  • java后端知识点复习
  • 图表制作-基础面积图
  • 在openEuler系统下编译安装Redis数据库指南
  • 「美业疗愈服务」从“表层美”到“身心整合”的行业变革︳博弈美业疗愈系统分享
  • GoogLeNet详解
  • 如何通过grep 排除“INTEGER: 1”
  • IoT平台和AIoT平台的区别
  • 如何使用极狐GitLab 软件包仓库功能托管 ruby?
  • 基于机器学习的攻击检测与缓解,以及 SDN 环境中的多控制器布局优化
  • Spring Boot + Vue 实现在线视频教育平台
  • 实践005-Gitlab CICD全项目整合
  • git 合并分支
  • 网工实验——OSPF配置
  • 理解 WKWebView 的 handlesURLScheme: 方法:判断 URL 协议是否由 WebView 默认处理
  • 基于STM32、HAL库的CH340N USB转UART收发器 驱动程序设计
  • Chroma:一个开源的8.9B文生图模型
  • SSM框架(Spring + Spring MVC + MyBatis)整合配置的详细步骤
  • Arm核的Ubuntu系统上安装Wireshark
  • MySQL如何进行调优
  • AquaCrop 模型新视角:多技术助力农业精准水管理
  • 室内定位误差分布评估到底该用累计误差还是混淆矩阵?
  • 复刻低成本机械臂 SO-ARM100 单关节控制(附代码)
  • STM32+安信可Ai-WB2-12F连接阿里云物联网平台
  • Google AI版图:解析AI Studio, Gemini, NotebookLM与GCP
  • 15 个 Azure DevOps 场景化面试问题及解答
  • WTK6900C-48L:离线语音芯片重构玩具DNA,从“按键操控”到“声控陪伴”的交互跃迁
  • 用uniapp在微信小程序实现画板(电子签名)功能,使用canvas实现功能