-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
34 lines (32 loc) · 1.1 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
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.active) {
chrome.contextMenus.removeAll(function() {
chrome.contextMenus.create({
id: "format",
title: 'JSON Formatter',
contexts: ['selection']
});
});
}
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
console.log("context menu was clicked");
if (info.menuItemId === "format") {
if (info.selectionText) {
let json = encodeURI(info.selectionText.replace(/#/g, '%23'));
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: generateFormatUrl,
args: [json],
});
}
}else{
console.log("something went wrong");
}
})
function generateFormatUrl(json) {
// check if the url is a medium article
let new_url = "https://jsonformatter.curiousconcept.com/?data="+json+"&process=true";
console.log("new_url", new_url);
window.open(new_url, "_blank");
}