-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbackground.js
43 lines (37 loc) · 1.3 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
import {HANDLERS} from './consts.js';
import {populateTemplate} from "./template.js";
function getObsidianUri(obj, options) {
let title = populateTemplate(options.note_title, obj);
let content = populateTemplate(options.note_content, obj);
let file_location = options.file_location
if (!file_location.endsWith('/')) {
file_location += '/';
}
let vault = options.vault;
let e = encodeURIComponent; // For convenience.
let obsidian_uri = `obsidian://new?vault=${e(vault)}&file=${e(file_location)}${e(title)}&content=${e(content)}`;
return obsidian_uri;
}
function getHandler(url) {
for (const site in HANDLERS) {
if (url.includes(HANDLERS[site].url)) {
return HANDLERS[site];
}
}
return null;
}
async function handleBrowserButtonClick(tab) {
const handler = getHandler(tab.url);
if (handler === null) {
return;
}
const results = await chrome.scripting.executeScript(
{
target: {tabId: tab.id},
func: handler.handler.siteAction,
});
const siteActionResult = results[0].result;
const uri = getObsidianUri(siteActionResult, await handler.handler.readOptionsFromStorage());
chrome.tabs.create({url: uri});
}
chrome.action.onClicked.addListener(handleBrowserButtonClick);