Add share link and URL settings

This commit is contained in:
Author Name ikipatang 2017-11-19 02:05:28 +01:00 committed by ikipatang
parent 77c8669407
commit cc32abf47b
5 changed files with 123 additions and 0 deletions

24
background.js Normal file
View file

@ -0,0 +1,24 @@
/**
* Share Current Tab
* @return {void} -
*/
function shareCurrentTab() {
browser.tabs.query({
active: true,
currentWindow: true,
}).then((tabs) => {
const url = tabs[0].url;
const title = tabs[0].title || url;
browser.storage.local.get('url').then((storage) => {
const shareUrl = `${storage.url}?post=${encodeURIComponent(url)}&title=${encodeURIComponent(title)}&source=bookmarklet`;
browser.windows.create({
url: shareUrl,
type: 'popup',
allowScriptsToClose: true,
});
});
});
}
browser.browserAction.onClicked.addListener(shareCurrentTab);

18
options/options.css Normal file
View file

@ -0,0 +1,18 @@
body {
width: 25em;
font-family: "Open Sans Light", sans-serif;
font-size: 0.9em;
font-weight: 300;
}
input#url {
margin: 0.5em;
width: 50em;
height: 2.5em;
}
input {
width: 20em;
height: 2.5em;
}

17
options/options.html Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="options.css"/>
</head>
<body>
<div>URL: <input id="url" type="text"/></div>
<input type="button" value="Save preferences" id="save-button"/>
<script src="options.js"></script>
</body>
</html>

38
options/options.js Normal file
View file

@ -0,0 +1,38 @@
/*
Store options UI using browser.storage.local.
*/
function storeSettings() {
const url = document.querySelector("#url");
// console.log('storeSettings url');
// console.log(url);
browser.storage.local.set({
url: url.value,
});
}
/*
Update the options UI with the settings values retrieved from storage,
or the default settings if the stored settings are empty.
*/
function updateUI(restoredSettings) {
const url = document.querySelector("#url");
// console.log('restoredSettings url');
// console.log(url);
url.value = restoredSettings.url || "";
}
function onError(e) {
console.error(e);
}
/*
On opening the options page, fetch stored settings and update the UI with them.
*/
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(updateUI, onError);
/*
On clicking the save button, save the currently selected settings.
*/
const saveButton = document.querySelector("#save-button");
saveButton.addEventListener("click", storeSettings);

26
storage.js Normal file
View file

@ -0,0 +1,26 @@
/*
Default settings. Initialize storage to these values.
*/
var storage = {
url: '',
}
/*
Generic error logger.
*/
function onError(e) {
console.error(e);
}
/*
On startup, check whether we have stored settings.
If we don't, then store the default settings.
*/
function checkStoredSettings(storedSettings) {
if (!storedSettings.storage) {
browser.storage.local.set({storage});
}
}
const gettingStoredSettings = browser.storage.local.get();
gettingStoredSettings.then(checkStoredSettings, onError);