-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_script.js
56 lines (51 loc) · 1.54 KB
/
content_script.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
44
45
46
47
48
49
50
51
52
53
54
55
56
// Put all the javascript code here, that you want to execute after page load.
const DISCOURSE_BASE_URL = "https://forum.codeselfstudy.com";
const NEW_TOPIC_URL = `${DISCOURSE_BASE_URL}/new-topic`;
browser.runtime.onMessage.addListener((request) => {
const title = document.title.split("|")[0].trim();
const url = window.location.href;
const body = `${getSelectionText()}\n\n${url}`;
console.log("title:", title);
console.log("body:", body);
console.log("url", url);
return Promise.resolve({ response: generateSharingUrl(title, body) });
});
/**
* Generate a URL that creates a post draft in Discourse with pre-filled information.
*/
function generateSharingUrl(
title,
body,
category = "general-discussion",
tagsArr = []
) {
const tags = tagsArr.join(",");
const params = new URLSearchParams({
title,
body,
category,
tags,
}).toString();
// This URL will create a post draft
return `${NEW_TOPIC_URL}/?${params}`;
}
/**
* Capture the selected text or return an empty string.
*/
function getSelectionText() {
if (window.getSelection) {
return toMarkdownQuote(window.getSelection().toString());
} else if (document.selection && document.selection.type != "Control") {
return toMarkdownQuote(document.selection.createRange().text);
}
return "";
}
/**
* Take a string and format it as a markdown blockquote.
*/
function toMarkdownQuote(text) {
return text
.split("\n")
.map((line) => `> ${line}`)
.join("\n");
}