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 分钟前
微信小程序实现转盘抽奖,可以自定义编辑奖项列表
javascript·微信小程序·小程序
徐同保3 分钟前
el-table的树形结构结合多选框使用,实现单选父子联动,全选,反选功能
javascript
J总裁的小芒果4 分钟前
vue3-print打印eletable某一行的数据
javascript·vue.js·elementui
MessiGo8 分钟前
Python 入门教程(3)基础知识 | 3.3、标识符
java·开发语言·python
二十雨辰19 分钟前
[Java]maven从入门到进阶
java·开发语言·maven
解孔明35 分钟前
数据类型自动转换的解决方案
java·开发语言
时间会证明一切.40 分钟前
【Java面试】第十天
java·开发语言·spring·面试
大大。1 小时前
el-input 只能输入数字和一个小数点,或者只能输入正整数
前端·javascript·vue.js
学习使我变快乐1 小时前
C++:初始化列表
开发语言·c++·算法
破刺不会编程1 小时前
【C++】透析string类
c语言·开发语言·数据结构·c++