-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (40 loc) · 1.54 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
"use strict";
console.log("connected...");
const onInstallURL = "https://www.notion.so/Pins-Peep-3a3c25ac99844402a2b38e6f0e21d77d";
// On Chrome Install
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === "install") {
chrome.tabs.create({ url: onInstallURL });
}
});
// Runtime Message Listener
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action && request.action == "pins_peep") {
peepPins();
sendResponse();
} else if (request.action && request.action == "search_on_pinterest") {
searchOnPinterest(request.data);
sendResponse();
}
});
// Context Menu Listeners
chrome.contextMenus.removeAll(function () {
chrome.contextMenus.create({ id: 'search', title: "Search on Pinterest", contexts: ["selection"] });
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId == "search") {
info.linkUrl ? peepPins(info.linkUrl) : searchOnPinterest(info.selectionText);
}
});
// Functions
function peepPins(url_context) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const url = (url_context !== undefined) ? new URL(url_context) : new URL(tabs[0].url);
const openURL = `https://www.pinterest.com/search/pins/?q=${url.href}`;
chrome.tabs.create({ url: openURL });
});
}
function searchOnPinterest(searchTerm) {
const openURL = `https://www.pinterest.com/search/pins/?q=${searchTerm}`;
chrome.tabs.create({ url: openURL });
}