
Inject scripts into the active tab
单击扩展工具栏图标,简化当前页面的样式
主要是实现在指定的页面,匹配对应的页面,执行注入css和设置插件图标的Badge
效果如下:
配置 manifest.json
{
"name": "Focus Mode",
"description": "Enable focus mode on Chrome's official Extensions and Chrome Web Store documentation.","version":"1.0",
//浏览器插件图标
"action": {
"default_icon": "img/logo.png"
},
//插件图标
"icons": {
"16": "img/logo.png",
"32": "img/logo.png",
"48": "img/logo.png",
"128": "img/logo.png"
},
"content_scripts": [
{
"js": ["content.js"],
"matches": [
"https://developer.chrome.com/docs/extensions/*",
"https://developer.chrome.com/docs/webstore/*"
]
}
],
"background": {
"service_worker": "background.js"
},
"permissions": ["activeTab","scripting"],
//快捷命令行
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+B",
"mac": "Command+B"
}
}
}
}
background.js
//插件安装完成设置插件图标的Badge为OFF
chrome.runtime.onInstalled.addListener(() => {
chrome.action.setBadgeText({
text: "OFF",
});
});
const extensions = 'https://developer.chrome.com/docs/extensions'
const webstore = 'https://developer.chrome.com/docs/webstore'
chrome.action.onClicked.addListener(async (tab) => {
console.log('onClicked点击了',tab);
if (tab.url.startsWith(extensions) || tab.url.startsWith(webstore)) {
// Retrieve the action badge to check if the extension is 'ON' or 'OFF'
const prevState = await chrome.action.getBadgeText({tabId: tab.id});
// Next state will always be the opposite
const nextState = prevState === 'ON' ? 'OFF' : 'ON'
if (nextState === "ON") {
// Insert the CSS file when the user turns the extension on
await chrome.scripting.insertCSS({
files: ["css/focus-mode.css"],
target: { tabId: tab.id },
});
} else if (nextState === "OFF") {
// Remove the CSS file when the user turns the extension off
await chrome.scripting.removeCSS({
files: ["css/focus-mode.css"],
target: { tabId: tab.id },
});
}
// Set the action badge to the next state
await chrome.action.setBadgeText({
tabId: tab.id,
text: nextState,
});
}
})
其中chrome.action.onClicked.addListener(async (tab) ....) 的 tab参数如下
{
active: true
audible: false
autoDiscardable: true
discarded: false
favIconUrl: "https://developer.chrome.com/images/meta/favicon-32x32.png"
groupId: -1
height: 937
highlighted: true
id: 193
incognito: false
index: 2
mutedInfo:
muted: false
[[Prototype]]: Object
pinned: false
selected: true
status: "complete"
title: "Chrome Extensions Tutorial: Focus Mode - Chrome Developers"
url: "https://developer.chrome.com/docs/extensions/mv3/getstarted/tut-focus-mode/"
width: 1920
windowId: 8
}
其中 id
通常就是我们说的tabId
focus-mode.css
body > .scaffold > :is(top-nav, navigation-rail, side-nav, footer),
main > :not(:last-child),
main > :last-child > navigation-tree,
main .toc-container {
display: none;
}
main > :last-child {
margin-top: min(10vmax, 10rem);
margin-bottom: min(10vmax, 10rem);
}
版权提示
1.除了标识原创之外,其他可能来源于网友的分享,仅供学习使用2.如您发现侵犯了您的权利,请联系我们删除
3.转载必须带本文链接,否则你将侵权
4.关于会员或其发布的相关内容均由会员自行提供,会员依法应对其提供的任何信息承担全部责任,本站不对此承担任何法律责任
0 人点赞
评论区 (0)
最新视频教程
-
-
黑白课堂
- Laravel6 全套入门实战
- 4651 2
-
-
-
黑白课堂
- Laravel6 全套入门实战
- 4572 5
-
-
-
黑白课堂
- Laravel6 全套入门实战
- 4457 0
-
-
-
黑白课堂
- Laravel6 全套入门实战
- 4077 0
-
-
-
黑白课堂
- Laravel6 全套入门实战
- 4079 0
-
最新视频课程
-
Laravel 消息通知使用
黑白课堂
285527 03年前
-
ace.js 打造一款属于你的 Web 编辑器,入门文档。
黑白课堂
15296 04年前
-
Laravel 实现 RBAC 权限管理功能 Permission 包操作
黑白课堂
14136 05年前
-
微信小程序等比例图片压缩上传,100%可用,非官方压缩方法
黑白课堂
12180 14年前
-
宝塔面板强制绑定账号修改为不强制绑定方案
黑白课堂
11366 03年前
-
wap2App 入门讲解,100%速成,全面为你讲解。
黑白课堂
9211 04年前
-
Laravel 设置请求头 header 参数
黑白课堂
8328 03年前
-
Laravel 的 PhpSpreadsheet 包入门
黑白课堂
8325 04年前
-
Laravel 表格操作 Maatwebsite/Laravel-Excel 3.1 最新版本的操作指南
黑白课堂
8297 04年前
-
Linux 下如何监听我们的脚本是否掉线了
黑白课堂
7565 03年前
-
KongQi Laravel Admin2.0 文档安装
黑白课堂
3914 23年前
-
KongQi Laravel admin2.0 layui admin 版本序言
黑白课堂
3342 03年前
-
易语言入门拖入文件导入到超级列表框表格内
哪吒
8406 13年前
-
易语言入门易语言定时任务模块
哪吒
6491 03年前
-
postman 使用手册cookie 使用
哪吒
4975 03年前
-
谷歌浏览器插件教程proxy 代理
哪吒
4534 01年前
-
易语言入门判断文件夹是否存在的方法
哪吒
4255 02年前
-
易语言入门TAB 标签页制作
哪吒
4204 03年前
-
Visual Studio Code 入门和使用教程插件安装使用
哪吒
4135 13年前
-
Visual Studio Code 入门和使用教程安装下载
哪吒
4105 03年前
