-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (60 loc) · 1.98 KB
/
background.js
File metadata and controls
67 lines (60 loc) · 1.98 KB
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
// fallback to chrome
if (typeof browser === "undefined") {
var browser = chrome;
}
browser.runtime.onInstalled.addListener(function () {
// Create context menu items for links, images, and the page
browser.contextMenus.create({
id: "addLinkContextMenu",
title: "Add Link",
contexts: ["link"]
});
browser.contextMenus.create({
id: "addImageContextMenu",
title: "Add Image Link",
contexts: ["image"]
});
browser.contextMenus.create({
id: "addPageContextMenu",
title: "Add This Page",
contexts: ["page"]
});
});
browser.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "addLinkContextMenu" && info.linkUrl) {
// Add Link context menu item clicked
const url = encodeURIComponent(info.linkUrl);
openPopup(url);
} else if (info.menuItemId === "addImageContextMenu" && info.srcUrl && (info.srcUrl.startsWith("http://") || info.srcUrl.startsWith("https://"))) {
// Add Image Link context menu item clicked
const imageUrl = encodeURIComponent(info.srcUrl);
openPopup(imageUrl);
} else if (info.menuItemId === "addPageContextMenu") {
// Add This Page context menu item clicked for the default context menu
const pageUrl = encodeURIComponent(tab.url);
openPopup(pageUrl);
}
});
function openPopup(url) {
browser.windows.create({
url: `https://rinkoe.com/ext/additem?url=${url}`,
type: "popup",
width: 500,
height: 700
}).then(window => {
console.log('Popup window created successfully:', window);
}).catch(error => {
console.error('Error creating popup window:', error);
});
}
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.url) {
const url = encodeURIComponent(message.url);
chrome.windows.create({
url: `https://rinkoe.com/ext/additem?url=${url}`,
type: "popup",
width: 500,
height: 700
});
}
});