Chrome Extension 入门教程(二)例子

1、只有最基本信息

manifest.json

{
	"manifest_version":3,
	"name":"manifest",
	"version" : "1.0.0",
	"description": "demo manifest"
}

图片

2、右上角显示图标

manifest.json

{
	"manifest_version":3,
	"name":"manifest",
	"version" : "1.0.0",
	"description": "demo manifest",
	"action":{
		"default_icon":{
			"16": "icon16.png"
		},
		"default_title":"Click to open demo manifest"
	}
}

图片

3、点击图标弹出界面

manifest.json

{
	"manifest_version":3,
	"name":"manifest",
	"version" : "1.0.0",
	"description": "demo manifest",
	"action":{
		"default_icon":{
			"16": "icon16.png"
		},
		"default_title":"Click to open demo manifest",
		"default_popup": "popup.html"
	}
}

popup.html

<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="popup.css">
	</head>
	<body>
		<button id="btn"></button>
		<script src="popup.js"></script>
	</body>
</html>

popup.css

button {
	height: 30px;
	width: 30px;
	outline: none;
	margin: 10px;
	border: none;
	border-radius: 2px;
}
button.current {
	box-shadow: 0 0 0 2px white,
				0 0 0 4px black;
}

popup.js

document.addEventListener('DOMContentLoaded', 
	function(event){
		var btn = document.getElementById("btn")
		btn.onclick = function() {
			alert("Click Button");
		}
	}
);

图片

图片

4、注入内容脚本

manifest.json

{
	"manifest_version":3,
	"name":"manifest",
	"version" : "1.0.0",
	"description": "demo manifest",
	"action":{
		"default_icon":{
			"16": "icon16.png"
		},
		"default_title":"Click to open demo manifest",
		"default_popup": "popup.html"
	},
	"content_scripts": [
		{
			"matches": [
				"http://*/*",
				"https://*/*"
			],
			"css": [
				"contentScript.css"
			],
			"js": [
				"contentScript.js"
			]
		}
	]
}

contentScript.js

chrome.runtime.onMessage.addListener(
	function(request, sender, sendResponse)
	{
		if (request.action === 'hello') {
			sendResponse({ results: 'Hello World! 我是来自「' + document.title + '」content script世界的消息~' });
			return true;
		}
	}
);

popup.js

document.addEventListener('DOMContentLoaded', 
	function(event){
		var btn = document.getElementById("btn")
		btn.onclick = function() {
			sendMessageToContentScript(
				{ action: 'hello' },
				function(response){
					alert(response.results);
				}
			);
		}
	}
);
function getCurrentTabId(callback)
{
	chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
	{
		if(callback) callback(tabs.length ? tabs[0].id: null);
	});
}
function sendMessageToContentScript(message, callback)
{
	getCurrentTabId((tabId) =>
		{
			chrome.tabs.sendMessage(tabId, message, function(response)
			{
				if(callback) callback(response);
			});
		}
	);
}

图片

5、后台添加菜单

manifest.json

{
	"manifest_version":3,
	"name":"manifest",
	"version" : "1.0.0",
	"description": "demo manifest",
	"action":{
		"default_icon":{
			"16": "icon16.png"
		},
		"default_title":"Click to open demo manifest",
		"default_popup": "popup.html"
	},
	"icons":{
			"16": "icon16.png"
	},
	"content_scripts": [
		{
			"matches": [
				"http://*/*",
				"https://*/*"
			],
			"css": [
				"contentScript.css"
			],
			"js": [
				"contentScript.js"
			],
			"run_at": "document_end"
		}
	],
	"background": {
		"service_worker": "background.js"
	},
	"permissions": [
		"contextMenus"
	]
}

background.js

chrome.runtime.onInstalled.addListener(() => {
	chrome.contextMenus.create({
		id: "some-command",
		title: "some title",
		contexts: ["all"]
	});
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
	if (info.menuItemId == "some-command") {
		console.log("yay!");
	}
});

右键菜单

图片

调试窗口

图片

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值