-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsettings.js
97 lines (83 loc) · 4.65 KB
/
settings.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
document.addEventListener('DOMContentLoaded', function () {
const selfPromotionCheckbox = document.getElementById('self-promotion');
const showIssueFormButton = document.getElementById('show-issue-form');
const submitIssueButton = document.getElementById('submit-issue');
const issueForm = document.getElementById('issue-form');
const currentTitleInput = document.getElementById('current-title');
const currentUrlInput = document.getElementById('current-url');
const extraNotesTextarea = document.getElementById('extra-notes');
const debugModeCheckbox = document.getElementById('debug-mode');
const sponsorStringInput = document.getElementById('sponsor-string');
const sponsorStringContainer = document.getElementById('sponsor-string-container');
// Load saved settings
chrome.storage.local.get(['blockSelfPromotion', 'debugMode', 'sponsorString'], function (result) {
selfPromotionCheckbox.checked = result.blockSelfPromotion || false;
debugModeCheckbox.checked = result.debugMode || false;
// Initialize sponsorString safely
const savedSponsorString = result.sponsorString || ''; // Default to empty string if undefined
// Show or hide the sponsor string input based on debug mode
if (result.debugMode) {
sponsorStringInput.value = savedSponsorString; // Load sponsor string
sponsorStringContainer.style.display = 'block'; // Show container
} else {
sponsorStringInput.value = ''; // Clear input if debug mode is disabled
sponsorStringContainer.style.display = 'none'; // Hide input
}
});
// Save settings when checkboxes are clicked
selfPromotionCheckbox.addEventListener('change', function () {
chrome.storage.local.set({ blockSelfPromotion: this.checked }, function () { });
});
debugModeCheckbox.addEventListener('change', function () {
chrome.storage.local.set({ debugMode: this.checked }, function () { });
// Show or hide the sponsor string input based on debug mode
if (this.checked) {
document.getElementById('sponsor-string-container').style.display = 'block'; // Show container
// Load the sponsor string if debug mode is enabled
chrome.storage.local.get(['sponsorString'], function (result) {
const savedSponsorString = result.sponsorString || ''; // Default to empty string if undefined
sponsorStringInput.value = savedSponsorString; // Set the value safely
});
} else {
sponsorStringInput.value = ''; // Clear input if debug mode is disabled
document.getElementById('sponsor-string-container').style.display = 'none'; // Hide container
}
});
// Save sponsor string when it changes
sponsorStringInput.addEventListener('change', function () {
chrome.storage.local.set({ sponsorString: this.value }, function () { });
});
showIssueFormButton.addEventListener('click', () => {
issueForm.style.display = issueForm.style.display === 'none' ? 'block' : 'none';
GetPageContext((pageTitle, pageURL) => {
currentTitleInput.value = pageTitle
currentUrlInput.value = pageURL
})
});
// Function to get page context from chrome.storage.local
function GetPageContext(callback) {
chrome.storage.local.get(["pageURL", "pageTitle"], function (result) {
const pageTitle = result.pageTitle.replace(" - YouTube", "") || ''; // Default to empty string if not found
const pageURL = result.pageURL.replace("www.", "") || ''; // Default to empty string if not found
callback(pageTitle, pageURL);
});
}
// Event listener for the submit issue button
submitIssueButton.addEventListener('click', function () {
const ISSUE_TITLE_PREFIX = 'New sponsor: ';
const GITHUB_REPO_URL = 'https://github.com/MagicJinn/Block-Sponsor-Comments/issues/new';
const LABEL = 'new sponsor';
// Prepare the issue title and body
const issueTitle = `${ISSUE_TITLE_PREFIX}${currentTitleInput.value}`;
let issueBody = `URL: ${currentUrlInput.value}`;
const notes = extraNotesTextarea.value.trim();
// Only add notes to the body if there are any
if (notes) {
issueBody += `\n\nNotes:\n${notes}`;
}
// Create the GitHub issue URL with query parameters, including the label
const issueUrl = `${GITHUB_REPO_URL}?title=${encodeURIComponent(issueTitle)}&body=${encodeURIComponent(issueBody)}`;
// Open the new issue URL in a new tab
chrome.tabs.create({ url: issueUrl });
});
});