Skip to content

Instantly share code, notes, and snippets.

View a90100's full-sized avatar
:octocat:
Keep learning

Harry Xie a90100

:octocat:
Keep learning
View GitHub Profile
{
...
"content_scripts": [
{
"js": ["scripts/content.js"],
"matches": [
"https://developer.chrome.com/docs/extensions/*",
"https://developer.chrome.com/docs/webstore/*"
]
}
"background": {
"service_worker": "background.js"
// "scripts": ["jquery.js", "my-background.js"]
},
{
"name": "Action Extension",
...
"action": {
"default_icon": { // optional
"16": "images/icon16.png", // optional
},
"default_title": "Click Me", // optional, shown in tooltip
"default_popup": "popup.html" // optional
},
// background.js
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['content.js']
});
});
let changeColor = document.getElementById("changeColor");
changeColor.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: setPageBackgroundColor,
});
});
{
"permissions": [
"activeTab",
"tabs" // 可以 query 到所有 tab 的 url
]
}
changeColor.addEventListener("click", async () => {
// 撈出指定屬性的 tabs
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => console.log('do something...'),
});
});
chrome.storage.sync.set({ key: value }).then(() => {
console.log("Value is set to " + value);
});
chrome.storage.sync.get(["key"]).then((result) => {
console.log("Value currently is " + result.key);
});
// 清除
chrome.storage.local.clear(function() {
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return {cancel: true}; },
{urls: ["*://www.evil.com/*"]},
["blocking"]
);
"permissions": ["webRequest", "storage", "declarativeNetRequest", "declarativeNetRequestFeedback"],
"host_permissions": ["https://*.udemy.com/*"],