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

起因(目的):

阅读电子书的时候, 网页背景太亮了,看久了眼睛难受。

最近看的书是: 金瓶梅

估计至少需要2个星期才能看完。

操作步骤:

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

代码 1 js

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, 核心配置文件!

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文件, 用来生成各种尺寸的缩略图。

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

看下效果:

走过路过,支持一下啊。

相关推荐
幼稚诠释青春3 分钟前
面试实例题
java·开发语言
JohnYan5 分钟前
Bun技术评估 - 05 SQL
javascript·后端·bun
weixin_457665395 分钟前
C++11新标准
开发语言·c++
阿蒙Amon6 分钟前
C#封装HttpClient:HTTP请求处理最佳实践
开发语言·http·c#
前端农民晨曦7 分钟前
深入浏览器事件循环与任务队列架构
前端·javascript·面试
Spider_Man22 分钟前
JavaScript对象那些坑:初学者必踩的“陷阱”与进阶秘籍
前端·javascript
大白爱琴23 分钟前
使用python进行图像处理—像素级操作与图像算术(4)
开发语言·图像处理·python
我在北京coding1 小时前
Uncaught ReferenceError: process is not defined
前端·javascript·vue.js
张成AI1 小时前
A2A JS SDK 完整教程:快速入门指南
javascript·agent·a2a
baozj1 小时前
一次表单数据复用引发的 Bug:理解 Vue 中的 data 为何是函数
前端·javascript·vue.js