-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
211 lines (187 loc) Β· 5.44 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Context menu item definitions
const contextMenuItems = [
{
id: 'kc_dashboard_link',
title: 'π Open Dashboard',
contexts: ['all'],
},
{
id: 'separator1',
type: 'separator',
contexts: ['all'],
},
{
id: 'kc_edit_page',
title: 'β Open Editor',
contexts: ['page'],
},
{
id: 'kc_get_page_ID',
title: 'π Copy ID',
contexts: ['page'],
},
{
id: 'kc_copy_permalink',
title: 'β Copy Permalink',
contexts: ['all'],
},
{
id: 'kc_open_link_editor',
title: 'β Edit Linked WordPress Page',
contexts: ['link'],
},
{
id: 'kc_get_linked_page_ID',
title: 'π Copy Linked Page/Post ID',
contexts: ['link'],
},
// Add more menu items here as needed
];
// Function to create context menu items
function createContextMenus() {
contextMenuItems.forEach((item) => {
chrome.contextMenus.create(item);
});
}
// Function to remove all context menu items
function removeAllContextMenus() {
chrome.contextMenus.removeAll();
}
// Handle menu item clicks using chrome.contextMenus.onClicked event
chrome.contextMenus.onClicked.addListener(function (info, tab) {
switch (info.menuItemId) {
case 'kc_dashboard_link':
chrome.tabs.sendMessage(tab.id, 'openDashboard');
break;
case 'kc_edit_page':
chrome.tabs.sendMessage(tab.id, 'openEditor');
break;
case 'kc_get_page_ID':
chrome.tabs.sendMessage(tab.id, 'getID');
break;
case 'kc_copy_permalink':
chrome.tabs.sendMessage(tab.id, 'getPermalink');
break;
case 'kc_open_link_editor':
chrome.tabs.sendMessage(tab.id, {'cmd':'openReferencedEditor', 'info':info});
break;
case 'kc_get_linked_page_ID':
chrome.tabs.sendMessage(tab.id, {'cmd':'getReferencedID', 'info':info});
break;
// Handle more menu items here as needed
}
});
// Message listener for managing context menus
chrome.runtime.onMessage.addListener(function (request) {
if (request.cmd == 'addContextMenu') {
removeAllContextMenus();
createContextMenus();
} else if (request.cmd == 'addLinkContextMenu') {
removeAllContextMenus();
createContextMenus();
} else if (request.cmd == 'removeContextMenu') {
removeAllContextMenus();
}
const context_opt = []; // Set custom post type
chrome.storage.sync.get('wp_context_opts', function (data) {
const contextOpt = data.wp_context_opts || [];
contextOpt.forEach((item) => {
chrome.contextMenus.remove(item);
});
});
});
/**
* @see https://developer.chrome.com/docs/extensions/mv3/getstarted/tut-focus-mode/
* Working test code
*
chrome.runtime.onInstalled.addListener(() => {
chrome.action.setBadgeText({
text: "OFF",
});
});
chrome.action.onClicked.addListener(async (tab) => {
if (tab.url.startsWith('https://vanilla.local') ) {
const prevState = await chrome.action.getBadgeText({ tabId: tab.id }); // Retrieve the action badge to check if the extension is 'ON' or 'OFF'
const nextState = prevState === 'ON' ? 'OFF' : 'ON' // Next state will always be the opposite
// Set the action badge to the next state
await chrome.action.setBadgeText({
tabId: tab.id,
text: nextState,
});
if (nextState === "ON") {
// Insert the CSS file when the user turns the extension on
console.log("on")
await chrome.scripting.insertCSS({
files: ["style.css"],
target: { tabId: tab.id },
});
} else if (nextState === "OFF") {
console.log("off")
// Remove the CSS file when the user turns the extension off
await chrome.scripting.removeCSS({
files: ["style.css"],
target: { tabId: tab.id },
});
}
}
});
*/
// Check the value of "hide_front_bar" in storage
// chrome.storage.sync.get("hide_front_bar", function(data) {
// if (chrome.runtime.lastError) {
// // Handle storage retrieval error
// console.error(chrome.runtime.lastError);
// return;
// }
// // Check the value and perform an action
// if (data.hide_front_bar === true) {
// console.log("The 'hide_front_bar' setting is enabled.");
// // Perform your action here
// } else {
// console.log("The 'hide_front_bar' setting is not enabled.");
// }
// });
/*
// Function to update CSS based on checkbox state
function updateCSS(tabId, isChecked) {
if (isChecked) {
chrome.scripting.insertCSS({
target: { tabId },
files: ["style.css"],
}, () => {
console.log("Style added");
});
} else {
chrome.scripting.removeCSS({
target: { tabId },
files: ["style.css"],
}, () => {
console.log("Style removed");
});
}
}
// Load the initial state of the checkbox when the extension is installed or updated
chrome.runtime.onInstalled.addListener(function () {
chrome.storage.sync.get('hide_front_bar', function (data) {
const isChecked = data.hide_front_bar;
chrome.tabs.query({}, function (tabs) {
tabs.forEach(tab => {
updateCSS(tab.id, isChecked);
});
});
});
});
// Message listener for managing the checkbox state
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action === 'toggle_hide_front_bar') {
const isChecked = request.value;
chrome.storage.sync.set({ hide_front_bar: isChecked }, function () {
chrome.tabs.query({}, function (tabs) {
tabs.forEach(tab => {
updateCSS(tab.id, isChecked);
});
});
});
}
});
*/