Skip to content

Commit

Permalink
feat: add configuration to include link
Browse files Browse the repository at this point in the history
- add new setting to add-on preference
- update README
- use async/await for code clarification
  • Loading branch information
0x6b committed Sep 25, 2018
1 parent 5b555a9 commit 2ad6c9e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Right click and select:

## Setting

You can configure **Append quote (<code>&gt;</code>) to selection**.
You can configure **Append quote (<code>&gt;</code>) to selection** and **Include link to source web page in the beginning of copied text**.

1. Click the menu button ![](https://prod-cdn.sumo.mozilla.net/uploads/gallery/images/2017-10-22-15-37-15-18c775.png) and choose ![](https://prod-cdn.sumo.mozilla.net/uploads/gallery/images/2017-10-30-08-25-40-b7327f.png) Add-ons. The Add-ons Manager tab will open
2. In the Add-ons Manager tab, select the Extensions panel
Expand Down
41 changes: 25 additions & 16 deletions src/copy.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { getSelectionAsMarkdown, doCopy } from "./lib/util";

let text = `[${document.title}](${document.URL})`;
let selection = getSelectionAsMarkdown();
async function main() {
try {
const quote = (await browser.storage.local.get("use-quote"))["use-quote"];
const link = (await browser.storage.local.get("link-to-source"))["link-to-source"];

browser.storage.local.get("use-quote").then(
result => {
if (typeof result["use-quote"] === "undefined" || result["use-quote"]) {
selection = selection
.split("\n")
.map(line => `> ${line}`)
.join("\n");
if (selection !== "> ") text += `\n\n${selection}`;
} else {
if (selection !== "") text += `\n\n${selection}`;
let text = `[${document.title}](${document.URL})`;
let selection = getSelectionAsMarkdown();

if (selection !== "") {
if (typeof quote === "undefined" || quote) {
selection = selection
.split("\n")
.map(line => `> ${line}`)
.join("\n");
}
if (link) {
text += `\n\n${selection}`;
} else {
text = selection;
}
}

doCopy(text);
},
error => {
console.error(`Error: ${error}`);
} catch (e) {
console.error(e);
}
);
}

main();
15 changes: 15 additions & 0 deletions src/settings/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@ document.addEventListener("DOMContentLoaded", () => {
console.log(`Error: ${error}`);
}
);
browser.storage.local.get("link-to-source").then(
result => {
if (typeof result["link-to-source"] === "undefined") {
document.querySelector("#link").checked = true;
} else {
document.querySelector("#link").checked = result["link-to-source"];
}
},
error => {
console.log(`Error: ${error}`);
}
);
});

document.querySelector("form").addEventListener("submit", e => {
e.preventDefault();
browser.storage.local.set({
"use-quote": document.querySelector("#quote").checked
});
browser.storage.local.set({
"link-to-source": document.querySelector("#link").checked
});
});
10 changes: 8 additions & 2 deletions src/settings/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@

<body>
<form>
<input type="checkbox" id="quote">
<label for="quote">Append quote (<code>&gt;</code>) to selection</label>
<div>
<input type="checkbox" id="quote">
<label for="quote">Append quote (<code>&gt;</code>) to selection</label>
</div>
<div>
<input type="checkbox" id="link">
<label for="link">Include link to source web page in the beginning of copied text</label>
</div>
<button type="submit">Save</button>
</form>
<script src="options.js"></script>
Expand Down

0 comments on commit 2ad6c9e

Please sign in to comment.