-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
33 lines (30 loc) · 910 Bytes
/
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
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: 'redirectMenuItem',
title: 'Open in JSCAD',
contexts: ['link'],
})
})
chrome.action.onClicked.addListener(tab => {
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: redirect,
args: [tabs[0].url],
})
})
})
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'redirectMenuItem')
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: redirect,
args: [info.linkUrl],
})
})
async function redirect(link) {
let url = new URL(link)
if (!url.pathname.endsWith('.js')) return alert('Not a JavaScript link')
if (url.host == 'github.com') url = (await fetch(url + '?raw=true')).url
location.assign('https://jscad.app#' + url)
}