mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-12-15 10:02:47 +03:00
3a859ef585
link-server-hook exposes (parts of) the link-store over eyre, on the condition that the client is authenticated as the host ship. link-webext as committed is a very minimal web extension. When its toolbar button is clicked, it saves the current webpage to /private in the link-store. In the future, this should support choosing a target to save to, highlighting already-saved pages, and many other features.
93 lines
2.3 KiB
JavaScript
93 lines
2.3 KiB
JavaScript
|
|
const attemptPost = (endpoint, path, data) => {
|
|
console.log('sending', data, JSON.stringify(data));
|
|
return new Promise((resolve, reject) => {
|
|
fetch(`http://${endpoint}/~link${path}`, {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
body: JSON.stringify(data)
|
|
})
|
|
.then(response => {
|
|
console.log('resp', response.status);
|
|
resolve(response.status === 200);
|
|
})
|
|
.catch(error => {
|
|
console.error('post failed', error);
|
|
resolve(false);
|
|
});
|
|
});
|
|
}
|
|
|
|
const attemptGet = (endpoint, path, data) => {
|
|
return new Promise((resolve, reject) => {
|
|
fetch(`http://${endpoint}/~link{path}`, {
|
|
method: 'GET',
|
|
credentials: 'include',
|
|
body: JSON.stringify(data)
|
|
})
|
|
.then(response => {
|
|
console.log('get response');
|
|
console.log('response', response);
|
|
resolve(true);
|
|
})
|
|
.catch(error => {
|
|
console.log('fetch error', error);
|
|
resolve(false);
|
|
});
|
|
});
|
|
}
|
|
|
|
const saveUrl = (endpoint, title, url) => {
|
|
return attemptPost(endpoint, '/add/private', {title, url});
|
|
}
|
|
|
|
const openOptions = () => {
|
|
browser.tabs.create({
|
|
url: browser.runtime.getURL('options/index.html')
|
|
});
|
|
}
|
|
|
|
const openLogin = (endpoint) => {
|
|
browser.tabs.create({
|
|
url: `http://${endpoint}/~/login`
|
|
});
|
|
}
|
|
|
|
const doSave = async () => {
|
|
console.log('gonna do save!');
|
|
// if no endpoint, refer to options page
|
|
const endpoint = await getEndpoint();
|
|
console.log('endpoint', endpoint);
|
|
if (endpoint === null) {
|
|
return openOptions();
|
|
}
|
|
|
|
const tab = (await browser.tabs.query({currentWindow: true, active: true}))[0];
|
|
//TODO figure out if we're viewing urbit page, turn into arvo:// url?
|
|
const success = await saveUrl(endpoint, tab.title, tab.url);
|
|
console.log('success', success);
|
|
if (!success) {
|
|
console.log('failed, opening login');
|
|
openLogin(endpoint);
|
|
} else {
|
|
console.log('success!');
|
|
}
|
|
}
|
|
|
|
|
|
// perform save action when extension button is clicked
|
|
//TODO want to do a pop-up instead of on-click action here latern
|
|
//
|
|
browser.browserAction.onClicked.addListener(doSave);
|
|
|
|
// open settings page on-install, user will need to set endpoint
|
|
//
|
|
browser.runtime.onInstalled.addListener(async ({ reason, temporary }) => {
|
|
// if (temporary) return; // skip during development
|
|
switch (reason) {
|
|
case "install":
|
|
browser.runtime.openOptionsPage();
|
|
break;
|
|
}
|
|
});
|