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

js 写个 最简单的 chrome 插件,修改网页背景颜色

起因(目的):

阅读电子书的时候, 网页背景太亮了,看久了眼睛难受。
最近看的书是: 金瓶梅
估计至少需要2个星期才能看完。

操作步骤:

  1. 新建一个 manifest.json 文件, 填入一些信息。
    “manifest_version”: 3, # 2 已经被废弃了。
  2. 新建图片文件夹,准备图片
  3. 打开:chrome://extensions/, 上传 插件文件夹

代码 1 js

// document.body.style.backgroundColor = "red";

// document.body.style.backgroundColor = "#00ff00";

// document.body.style.backgroundColor = "rgb(255, 255, 0)";

// 如果当前网页的背景颜色是白色, 那么就修改背景颜色和字体颜色。否则不变。

// Function to get the current background color of the page
function getCurrentBgColor() {
    return window.getComputedStyle(document.body).getPropertyValue('background-color');
  }
  
// Function to change the background and text color
function changeColors() {
    const currentBgColor = getCurrentBgColor();

    // Check if the current background color is white
    if (currentBgColor === 'rgb(255, 255, 255)') {
        // Change the background color to #303841 and the text color to #d8dfea
        // document.body.style.backgroundColor = '#303841'; // sublime 的颜色
        
        document.body.style.backgroundColor = '#cab7e8'; // 淡紫色
        document.body.style.color = '#d8dfea';
    }
}
  
// Call the changeColors function when the page is loaded
window.addEventListener('load', changeColors);

代码 2 manifest.json, 核心配置文件!

{
    "manifest_version": 3,
    "name": "Read More!",
    "description": "Change all background color",
    "version": "1.0",

    "icons": {
        "16": "/images/icon16.png",
        "48": "/images/icon48.png",
        "128": "/images/icon128.png"
    },
    
    "action": {
        "default_icon": {
            "16": "/images/icon16_active.png",
            "48": "/images/icon48_active.png",
            "128": "/images/icon128_active.png"
        }
    },

    "content_scripts": [
        {
            "matches": ["*://*/*"],
            "css":["main.css"],
            "js": ["content.js"],
            "run_at": "document_start"
        }
    ]
     
}

代码3, py文件, 用来生成各种尺寸的缩略图。

from PIL import Image

# 目的  调整图片大小。做 Chrome 插件的时候,需要用到图标。
# 先找2张图片

def resize_image(input_name, out_name, out_width, out_height):
    img = Image.open(input_name)
    out = img.resize((out_width, out_height))
    out.save(out_name)


# 默认转态下的图标
input_image = "g1.jpg"
for i in [16, 48, 128]:
    out_image = f"icon{i}.png"
    resize_image(input_image, out_image, i, i)


# 插件激活状态的图标
input_image = "g2.png"
for i in [16, 48, 128]:
    out_image = f"icon{i}_active.png"
    resize_image(input_image, out_image, i, i)

结论 + todo

看下效果:
原始页面,默认页面

开启插件之后的页面

走过路过,支持一下啊。

zfb

wx


http://www.kler.cn/news/295068.html

相关文章:

  • 退火吗?C#/WinForm演示退火算法
  • 手写 Vue Router 中的 Hash 模式和 History 模式
  • 反弹shell
  • 【Python报错已解决】 AttributeError: ‘move_to‘ requires a WebElement
  • Redis单机安装
  • 国开(电大)2024秋《文献检索与论文写作》综合练习2
  • 云服务器部署DB-GPT项目
  • 自动驾驶---Motion Planning之轨迹拼接
  • WIN11 ESP32 IDF + VSCODE 环境搭建[教程向]
  • 不会Excel怎么制作桑基图?用什么软件绘制比较好呢?推荐2款简单好用的图表制作工具
  • c++递推
  • 拉取ros2_control_demos存储库
  • List中常见的方法和五种遍历方式
  • windows下使用vscode编写运行以及调试C/C++
  • 基于STM32设计的ECG+PPG人体参数测量系统(华为云IOT)(217)
  • ROS1 + Realsense d455 固件安装+读取rostopic数据
  • nmon服务器监控工具使用
  • 请解释Java Web应用中的前后端分离是什么?它有哪些好处?什么是Java Web中的Servlet过滤器?它有什么作用?
  • ffmpeg的安装和使用教程
  • python--基础语法(1)
  • 《Ajax 是什么?如何在 HTML5 中使用》
  • 数据结构--二叉树(C语言实现,超详细!!!)
  • 彩色成像的基础和应用 原理 Principles (论文引用目录)
  • 2024年高教社杯数学建模国赛C题超详细解题思路分析
  • 图论篇--代码随想录算法训练营第五十天打卡| 深度优先搜索理论基础,98. 所有可达路径,广度优先搜索理论基础
  • 数字化转型专家讲师培训师唐兴通中欧国际工商学院数字化转型战略与实现路径AIGC人工智能数字化战略数字商业模式创新
  • 【数据库中级】1_DBeaver操作数据库
  • 【微信小程序】搭建项目步骤 + 引入Tdesign UI
  • Flink SQL 中常见的数据类型
  • 开源网安引领AIGC+开发安全,智能防护铸就软件安全新高度