Skip to content

Commit 5a56580

Browse files
authored
Version bump to 1.1.3
1 parent 761bba4 commit 5a56580

File tree

11 files changed

+29278
-58
lines changed

11 files changed

+29278
-58
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
.directory
2-
.idea/*
2+
.idea/*
3+
web-ext-artifacts/*
4+
temp/*
5+
test.js

background.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
browser.pageAction.onClicked.addListener(async (tab) => {
2-
const pageTitle = tab.title;
3-
const pageURL = tab.url;
4-
pasteToClipboard(await fillFormatStructure(pageURL, pageTitle, await getFormats()))
2+
const pageTitle = tab.title;
3+
const pageURL = tab.url;
4+
pasteToClipboard(await fillFormatStructure(pageURL, pageTitle, await getFormats()))
5+
});
6+
7+
browser.contextMenus.removeAll().then(() => {
8+
browser.contextMenus.create({
9+
id: "share-dis-copy-page",
10+
title: "Copy page URL as formatted link to clipboard",
11+
contexts: ["page", "selection", "link"]
12+
});
13+
browser.contextMenus.create({
14+
id: "share-dis-copy-selection",
15+
title: "Quote selected text in page as formatted link to clipboard",
16+
contexts: ["selection"]
17+
});
18+
browser.contextMenus.create({
19+
id: "share-dis-copy-link",
20+
title: "Copy highlighted link in page as formatted link to clipboard",
21+
contexts: ["link"]
22+
});
23+
});
24+
25+
browser.contextMenus.onClicked.addListener(async (info, tab) => {
26+
let pageTitle = tab.title;
27+
let pageURL = tab.url;
28+
let method = 'default';
29+
if (info.menuItemId === "share-dis-copy-link" && info.linkUrl) {
30+
pageURL = info.linkUrl;
31+
}
32+
else if (info.menuItemId === "share-dis-copy-selection" && info.selectionText) {
33+
pageTitle = `‟${info.selectionText}”`;
34+
method = 'quote';
35+
}
36+
pasteToClipboard(await fillFormatStructure(pageURL, pageTitle, await getFormats(method)));
537
});

functionality.js

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,44 @@
11
class TransformerNotFound extends Error {
2-
constructor(message = "", ...args) {
3-
super(message, ...args);
4-
this.message = message + " was not found in the transformer list";
5-
}
2+
constructor(message = "", ...args) {
3+
super(message, ...args);
4+
this.message = message + " was not found in the transformer list";
5+
}
66
}
77

8-
async function getFormats() {
9-
return new Promise((resolve, reject) => {
10-
browser.storage.local.get([`format`], (r) => {
11-
if (`format` in r) {
8+
async function getFormats(method = "default") {
9+
return new Promise((resolve) => {
10+
browser.storage.local.get(["format"], (r) => {
11+
if (r.format) {
1212
resolve(JSON.parse(r.format));
13+
return;
1314
}
14-
else {
15-
resolve([
16-
{
17-
type: `title`,
18-
value: null
19-
},
20-
{
21-
type: `string`,
22-
value: ` - `
23-
},
24-
{
25-
type: `url`,
26-
value: null
27-
}
28-
]);
29-
}
30-
})
31-
})
15+
const defaults = {
16+
quote: [
17+
{ type: "title", value: null },
18+
{ type: "string", value: " quoted from: " },
19+
{ type: "url", value: null }
20+
],
21+
default: [
22+
{ type: "title", value: null },
23+
{ type: "string", value: " - " },
24+
{ type: "url", value: null }
25+
]
26+
};
27+
resolve(defaults[method] || defaults.default);
28+
});
29+
});
30+
}
31+
32+
function defaultTransformers() {
33+
return {
34+
"www.youtube.com": { regex: /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/shorts\/([^\/?]+)$/i, output: "https://www.youtube.com/watch?v=$1" },
35+
"m.youtube.com": { regex: /^(?:https?:\/\/)?(?:m\.)?youtube\.com\/shorts\/([^\/?]+)(?:\?[^\/]*)?$/i, output: "https://www.youtube.com/watch?v=$1" }
36+
}
3237
}
3338

3439
async function getTransformers() {
3540
return new Promise((resolve, reject) => {
36-
browser.storage.local.get([`transformers`], (r) => {
41+
browser.storage.local.get([`transformers`], async (r) => {
3742
if (`transformers` in r) {
3843
let result = JSON.parse(r.transformers);
3944
Object.keys(result).forEach((domain) => {
@@ -47,10 +52,7 @@ async function getTransformers() {
4752
resolve(result);
4853
}
4954
else {
50-
resolve({
51-
"www.youtube.com": {regex: /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/shorts\/([^\/?]+)$/i, output: "https://www.youtube.com/watch?v=$1"},
52-
"m.youtube.com": {regex: /^(?:https?:\/\/)?(?:m\.)?youtube\.com\/shorts\/([^\/?]+)(?:\?[^\/]*)?$/i, output: "https://www.youtube.com/watch?v=$1"}
53-
});
55+
resolve(defaultTransformers())
5456
}
5557
})
5658
})
@@ -69,6 +71,12 @@ async function getTransformer(hostname) {
6971
async function updateTransformer(hostname, regex, output) {
7072
return new Promise(async (resolve, reject) => {
7173
let transformers = await getTransformers();
74+
if (!Object.keys(transformers).includes(hostname)) {
75+
console.log('Cocksucker')
76+
transformers[hostname] = {
77+
regex: null, output: null
78+
};
79+
}
7280
transformers[hostname].regex = regex;
7381
transformers[hostname].output = output;
7482

@@ -79,6 +87,17 @@ async function updateTransformer(hostname, regex, output) {
7987
})
8088
}
8189

90+
async function removeTransformer(hostname) {
91+
return new Promise(async (resolve, reject) => {
92+
let transformers = await getTransformers();
93+
delete transformers[hostname];
94+
Object.keys(transformers).forEach((host) => {
95+
transformers[host].regex = transformers[host].regex.source;
96+
})
97+
resolve(transformers);
98+
})
99+
}
100+
82101
function convertFormatToTagify(format) {
83102
let result = ``;
84103
for (var i = 0; i < format.length; i++) {
@@ -138,15 +157,15 @@ async function fillFormatStructure(url, title, format) {
138157
return format;
139158
}
140159

141-
function formatAsString (format) {
160+
function formatAsString(format) {
142161
let clipboardText = ``;
143162
for (var i = 0; i < format.length; i++) {
144163
clipboardText += format[i].value;
145164
}
146165
return clipboardText;
147166
}
148167

149-
function pasteToClipboard (format) {
168+
function pasteToClipboard(format) {
150169
let clipboardText = ``;
151170
for (var i = 0; i < format.length; i++) {
152171
clipboardText += format[i].value;
@@ -155,16 +174,16 @@ function pasteToClipboard (format) {
155174
}
156175

157176
function RegExpFromStr(patternString) {
158-
// Remove leading and trailing slashes
159-
let trimmedPattern = patternString.replace(/^\/|\/$/g, '');
177+
// Remove leading and trailing slashes
178+
let trimmedPattern = patternString.replace(/^\/|\/$/g, '');
160179

161-
// Extract flags from the end of the string
162-
const flagsMatch = trimmedPattern.match(/([gimy]+)$/);
163-
let flags = flagsMatch ? flagsMatch[1] : '';
180+
// Extract flags from the end of the string
181+
const flagsMatch = trimmedPattern.match(/([gimy]+)$/);
182+
let flags = flagsMatch ? flagsMatch[1] : '';
164183

165-
// Remove flags from the pattern
166-
trimmedPattern = trimmedPattern.replace(/\/?[gimy]+$/, '');
184+
// Remove flags from the pattern
185+
trimmedPattern = trimmedPattern.replace(/\/?[gimy]+$/, '');
167186

168-
// Create and return the RegExp object
169-
return new RegExp(trimmedPattern, flags);
187+
// Create and return the RegExp object
188+
return new RegExp(trimmedPattern, flags);
170189
}

jdatatable.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
async function edit(hostname) {
2+
setOverwrittenHost(hostname);
3+
await updateTransformerInput(hostname);
4+
document.getElementById("pills-url-tab").click();
5+
}
6+
7+
async function remove(hostname) {
8+
let updated_transformers = await removeTransformer(hostname);
9+
browser.storage.local.set({transformers: JSON.stringify(updated_transformers)}, async () => {
10+
await loadDataTable();
11+
showModal(`Transformer removed`, [`The transformer for ${hostname} has been ` +
12+
`removed.`], `success`);
13+
});
14+
}
15+
16+
browser._datatable = null;
17+
18+
async function loadDataTable() {
19+
if (browser._datatable !== null) {
20+
browser._datatable.destroy()
21+
browser._datatable = null;
22+
}
23+
24+
let transformers = await getTransformers();
25+
let hostnames = Object.keys(transformers);
26+
let htmlContent = ``;
27+
for (let idx in hostnames) {
28+
let hostname = hostnames[idx];
29+
let transformer = transformers[hostname];
30+
htmlContent += `<tr>
31+
<td>
32+
${hostname}
33+
</td>
34+
<td>
35+
<input class="btn btn-primary center" type="button" value="Edit">
36+
<input class="btn btn-primary center" type="button" value="Remove">
37+
</td>
38+
</tr>`;
39+
}
40+
document.getElementById(`transformer-body`).innerHTML = htmlContent;
41+
42+
const tableBody = document.getElementById('transformer-body');
43+
for (const row of tableBody.rows) {
44+
const hostname = row.cells[0].innerText.trim();
45+
const editButton = row.querySelector('input[value="Edit"]');
46+
const removeButton = row.querySelector('input[value="Remove"]');
47+
48+
if (editButton) {
49+
editButton.onclick = () => edit(hostname);
50+
}
51+
if (removeButton) {
52+
removeButton.onclick = () => remove(hostname);
53+
}
54+
}
55+
56+
browser._datatable = new DataTable(`#transformer-table`);
57+
}
58+
59+
loadDataTable();

0 commit comments

Comments
 (0)