-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
136 lines (115 loc) · 3.87 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// First event
// If you define default_popup in manifest, you cannot use chrome.browserAction.onClicked.addListener method,
// it will throw error
// chrome.browserAction.onClicked.addListener(buttonClicked);
// function buttonClicked() {
// console.log("button is clicked in buttonclicked handler");
// }
// You can send the message from all the tab or unique tab using
// let msg: {
// txt: "hello"
// }
// chrome.tabs.sendMessage(tab.id, msg)
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.sync.set({ color: "#3aa757" }, function () {
console.log("The color is green.");
});
chrome.declarativeContent.onPageChanged.removeRules(undefined, function () {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: "developer.chrome.com" },
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()],
},
]);
});
chrome.runtime.onMessage.addListener(function (message, sender, callback) {
console.log("sender", sender);
console.log("message", message);
// Checking communication between background and popup
if (message.type === "trigger_redirection_tosuspended_page") {
// I need to create the redirection link for the
const { tab } = sender;
// FOr not i have to give some amount of timeout to show notification
setTimeout(() => {
chrome.tabs.update(tab.id, {
url: chrome.runtime.getURL(
`pages/suspendedpage.html?url=${tab.url}&title=${tab.title}`
),
});
}, 4000);
var opt = {
iconUrl: "images/get_started32.png",
type: "list",
title: "Primary Title",
message: "Primary message to display",
priority: 1,
items: [
{ title: "Item1", message: "This is item 1." },
{ title: "Item2", message: "This is item 2." },
{ title: "Item3", message: "This is item 3." },
],
};
chrome.notifications.create(`${Math.random()}`, opt, function callback() {
console.log("created!");
console.log("Last error:", chrome.runtime.lastError);
});
}
if (message == "reload") {
chrome.tabs.executeScript({
code: 'document.body.style.backgroundColor="orange"',
});
}
});
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
if (request.msg === "something_completed") {
// To do something
console.log(request.data.subject);
console.log(request.data.content);
chrome.runtime.sendMessage({
msg: "something_completed",
data: {
subject: "Loading",
content: "Just completed!",
},
});
}
});
// chrome.runtime.onInstalled.addListener(function () {
// chrome.contextMenus.create({
// id: "sampleContextMenu",
// title: "Sample Context Menu",
// contexts: ["selection"],
// });
// });
chrome.contextMenus.create({
id: "sampleContextMenu",
title: "Sample Context Menu",
contexts: ["selection"], // only show context menu on selection
});
// You can have more options to add the context menus. check the documentation for
// chrome.contextMenus
chrome.contextMenus.onClicked.addListener(() => {
console.log("context menu clicked");
});
// This will run when a bookmark is created. need permission
// chrome.bookmarks.onCreated.addListener(function () {
// // do something
// });
});
// chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// console.log(
// sender.tab
// ? "from a content script:" + sender.tab.url
// : "from the extension"
// );
// if (request.greeting == "hello") sendResponse({ farewell: "goodbye" });
// });
// Do not register listeners asynchronously