Skip to content

Commit 2696228

Browse files
authored
Merge pull request #2816 from oixel/master
Add optional password protection to pop-up.
2 parents 24f7183 + 21587bb commit 2696228

File tree

3 files changed

+159
-20
lines changed

3 files changed

+159
-20
lines changed

_locales/en/messages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{
2+
"requirePassword": { "message": "Require password to change ImprovedTube settings" },
3+
"passwordOptions": { "message": "Password options"},
4+
"enterPassword": { "message": "Please enter your 4-digit code."},
5+
26
"qualityWhenRunningOnBattery":{
37
"message": "Quality, when running on battery"
48
},

menu/index.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,70 @@
77
--------------------------------------------------------------*/
88
satus.storage.import(function (items) {
99
var language = items.language;
10-
if (!language || language === 'default') { language = false;}
10+
if (!language || language === 'default') { language = false; }
1111
satus.locale.import(language, function () {
12-
satus.render(extension.skeleton);
13-
14-
extension.exportSettings();
15-
extension.importSettings();
16-
17-
satus.parentify(extension.skeleton, [
18-
'attr',
19-
'baseProvider',
20-
'layersProvider',
21-
'rendered',
22-
'storage',
23-
'parentObject',
24-
'parentSkeleton',
25-
'childrenContainer',
26-
'value'
27-
]);
28-
29-
extension.attributes();
30-
satus.events.on('storage-set', extension.attributes);
12+
// Place normal skeleton into seperate function to only render if password is disabled or not required
13+
const renderSkeleton = () => {
14+
satus.render(extension.skeleton);
15+
16+
extension.exportSettings();
17+
extension.importSettings();
18+
19+
satus.parentify(extension.skeleton, [
20+
'attr',
21+
'baseProvider',
22+
'layersProvider',
23+
'rendered',
24+
'storage',
25+
'parentObject',
26+
'parentSkeleton',
27+
'childrenContainer',
28+
'value'
29+
]);
30+
31+
extension.attributes();
32+
satus.events.on('storage-set', extension.attributes);
33+
}
34+
35+
// Shows login page if password exists and is required
36+
if (satus.storage.get("require_password") === true && satus.storage.get("password")) {
37+
satus.render({
38+
component: 'modal',
39+
textField: {
40+
component: "text-field",
41+
id: "login-input",
42+
placeholder: "Enter password",
43+
lineNumbers: false,
44+
rows: 1,
45+
46+
// Focuses text box when extension is opened
47+
function() {
48+
document.getElementById('login-input').focus();
49+
},
50+
51+
on: {
52+
input: function () {
53+
// Grabs password from storage for comparison
54+
const password = satus.storage.get("password");
55+
56+
// If correct password is inputted, render regular menu
57+
if (this.value == password) {
58+
var modal = this.skeleton.parentSkeleton;
59+
modal.rendered.close();
60+
renderSkeleton();
61+
}
62+
},
63+
64+
// Refocuses on password input box if clicked away
65+
blur: function () {
66+
const loginInput = document.getElementById('login-input');
67+
if (loginInput) loginInput.focus();
68+
}
69+
}
70+
}
71+
}, extension.skeleton.rendered);
72+
}
73+
else renderSkeleton(); // Render regular menu if password is not required
3174
}, '_locales/');
3275
});
3376

menu/skeleton-parts/settings.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,95 @@ extension.skeleton.header.sectionEnd.menu.on.click.settings.on.click.secondSecti
769769
}
770770
}
771771
};
772+
773+
/*--------------------------------------------------------------
774+
# PASSWORD OPTIONS
775+
--------------------------------------------------------------*/
776+
777+
function togglePasswordInput() {
778+
// Sets the visibility of the password input field depending on the state of the requirement switch
779+
let requirePassword = satus.storage.get("require_password");
780+
const display = satus.storage.get("require_password") ? "" : "none";
781+
document.getElementById('password-input-card').style.display = display;
782+
783+
// Avoids changing input field if input is not visible
784+
if (!requirePassword) return;
785+
786+
// Sets input field to display current password
787+
const passwordInput = document.getElementById('password-input');
788+
passwordInput.placeholder = "Enter password";
789+
790+
// If no password exists (undefined), set input value to empty string instead
791+
passwordInput.value = satus.storage.get("password") || '';
792+
}
793+
794+
extension.skeleton.header.sectionEnd.menu.on.click.settings.on.click.secondSection.usePassword = {
795+
component: 'button',
796+
text: 'passwordOptions',
797+
before: {
798+
svg: {
799+
component: 'svg',
800+
attr: {
801+
'viewBox': '0 0 24 24',
802+
'fill': 'currentColor',
803+
},
804+
path: {
805+
component: 'path',
806+
attr: {
807+
'd': 'M 7 5 C 3.1545455 5 0 8.1545455 0 12 C 0 15.845455 3.1545455 19 7 19 C 9.7749912 19 12.089412 17.314701 13.271484 15 L 16 15 L 16 18 L 22 18 L 22 15 L 24 15 L 24 9 L 23 9 L 13.287109 9 C 12.172597 6.6755615 9.8391582 5 7 5 z M 7 7 C 9.2802469 7 11.092512 8.4210017 11.755859 10.328125 L 11.988281 11 L 22 11 L 22 13 L 20 13 L 20 16 L 18 16 L 18 13 L 12.017578 13 L 11.769531 13.634766 C 11.010114 15.575499 9.1641026 17 7 17 C 4.2454545 17 2 14.754545 2 12 C 2 9.2454545 4.2454545 7 7 7 z M 7 9 C 5.3549904 9 4 10.35499 4 12 C 4 13.64501 5.3549904 15 7 15 C 8.6450096 15 10 13.64501 10 12 C 10 10.35499 8.6450096 9 7 9 z M 7 11 C 7.5641294 11 8 11.435871 8 12 C 8 12.564129 7.5641294 13 7 13 C 6.4358706 13 6 12.564129 6 12 C 6 11.435871 6.4358706 11 7 11 z'
808+
}
809+
}
810+
}
811+
},
812+
on: {
813+
click: {
814+
component: 'section',
815+
variant: 'card',
816+
817+
require_password: {
818+
component: 'switch',
819+
text: 'requirePassword',
820+
value: false,
821+
822+
on: {
823+
click: function (event) {
824+
// Prevents clicking the toggle switch does not load another section
825+
event.preventDefault();
826+
827+
// Toggle the visibility of password input field
828+
togglePasswordInput();
829+
}
830+
}
831+
},
832+
833+
password_input: {
834+
component: 'section',
835+
variant: 'card',
836+
id: "password-input-card",
837+
style: {
838+
"padding": "0"
839+
},
840+
children: {
841+
component: 'input',
842+
id: "password-input",
843+
type: 'text',
844+
style: {
845+
"padding": "0 10px 0 10px"
846+
},
847+
848+
// Sets password field's visibility when modal menu is rendered
849+
function() {
850+
togglePasswordInput();
851+
},
852+
853+
// Updates the stored password value on keyboard input
854+
on: {
855+
input: function (event) {
856+
satus.storage.set("password", event.target.value);
857+
}
858+
}
859+
}
860+
}
861+
}
862+
}
863+
};

0 commit comments

Comments
 (0)