PluginProbe
NETSENSAI Shield / trunk
NETSENSAI Shield vtrunk
1.6.1 trunk 1.1 1.2 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.6.0
← All changes | assets/script.js +85 -57 1.2trunk View file →
@@ -1,57 +1,85 @@
1 -document.addEventListener('DOMContentLoaded', function () {
2 - console.log('script.js loaded');
3 -
4 - /**
5 - * Helper function to handle the visibility of a text field/container.
6 - *
7 - * @param {string} toggleId - The ID of the toggle (checkbox) element.
8 - * @param {string} textFieldId - The ID of the text field/container.
9 - */
10 - function handleTextFieldVisibility(toggleId, textFieldId) {
11 - const toggle = document.getElementById(toggleId);
12 - const textField = document.getElementById(textFieldId);
13 -
14 - if (toggle && textField) {
15 - console.log(`Initial state for ${toggleId}: ${toggle.checked}`);
16 - textField.style.display = toggle.checked ? 'block' : 'none';
17 - toggle.addEventListener('change', function () {
18 - console.log(`Toggle ${toggleId} changed: ${this.checked}`);
19 - textField.style.display = this.checked ? 'block' : 'none';
20 - });
21 - } else {
22 - if (!toggle) console.error(`Toggle element with ID "${toggleId}" not found.`);
23 - if (!textField) console.error(`Text field element with ID "${textFieldId}" not found.`);
24 - }
25 - }
26 -
27 - // "Change Login URL"
28 - handleTextFieldVisibility('ns_shield_login_url_enabled', 'ns_shield_login_url');
29 -
30 - // "Disable Default Admin"
31 - handleTextFieldVisibility('ns_shield_default_admin', 'admin_login_field');
32 -
33 - /**
34 - * Helper function to handle tooltips.
35 - */
36 - function handleTooltip(sliderSelector, tooltipSelector) {
37 - const sliders = document.querySelectorAll(sliderSelector);
38 - sliders.forEach(function (slider) {
39 - const tooltip = slider.parentElement.querySelector(tooltipSelector);
40 - if (slider && tooltip) {
41 - slider.addEventListener('mouseenter', function () {
42 - tooltip.style.display = 'block';
43 - });
44 - slider.addEventListener('mouseleave', function () {
45 - tooltip.style.display = 'none';
46 - });
47 - slider.addEventListener('focus', function () {
48 - tooltip.style.display = 'block';
49 - });
50 - slider.addEventListener('blur', function () {
51 - tooltip.style.display = 'none';
52 - });
53 - }
54 - });
55 - }
56 - handleTooltip('.slider', '.tooltip');
57 -});
1 +document.addEventListener('DOMContentLoaded', function () {
2 + // 0) Only run on the Shield settings page
3 + if (typeof nsShieldSettings !== 'object' || !nsShieldSettings.pageSlug) {
4 + return;
5 + }
6 + var pageRegex = new RegExp('[?&]page=' + nsShieldSettings.pageSlug + '(?:&|$)');
7 + if (!pageRegex.test(window.location.search)) {
8 + return;
9 + }
10 +
11 + // 1) Hide any inline preload-modal (we handle it in modal_popup.php)
12 + var inlineModal = document.getElementById('ns-shield-preload-modal');
13 + if (inlineModal) {
14 + inlineModal.style.display = 'none';
15 + }
16 +
17 + // 2) Show/hide text fields based on toggles
18 + function handleTextFieldVisibility(toggleId, fieldId) {
19 + var toggle = document.getElementById(toggleId),
20 + field = document.getElementById(fieldId);
21 + if (!toggle || !field) return;
22 + field.style.display = toggle.checked ? 'block' : 'none';
23 + toggle.addEventListener('change', function () {
24 + field.style.display = this.checked ? 'block' : 'none';
25 + });
26 + }
27 + handleTextFieldVisibility('ns_shield_login_url_enabled', 'ns_shield_login_url');
28 + handleTextFieldVisibility('ns_shield_default_admin', 'admin_login_field');
29 +
30 + // 3) Tooltips on sliders
31 + function handleTooltip(sliderSelector, tooltipSelector) {
32 + document.querySelectorAll(sliderSelector).forEach(function (slider) {
33 + var tooltip =
34 + slider.parentElement.querySelector(tooltipSelector) ||
35 + (slider.nextElementSibling &&
36 + slider.nextElementSibling.matches(tooltipSelector) &&
37 + slider.nextElementSibling) ||
38 + (slider.parentElement.parentElement &&
39 + slider.parentElement.parentElement.querySelector(tooltipSelector));
40 + if (!tooltip) return;
41 + ['mouseenter','focus'].forEach(function (evt) {
42 + slider.addEventListener(evt, function () { tooltip.style.display = 'block'; });
43 + });
44 + ['mouseleave','blur'].forEach(function (evt) {
45 + slider.addEventListener(evt, function () { tooltip.style.display = 'none'; });
46 + });
47 + });
48 + }
49 + handleTooltip('.slider', '.tooltip');
50 +
51 + // 4) CSP header light/hard toggles
52 + var lightToggle = document.getElementById('ns_shield_csp_header_light'),
53 + hardToggle = document.getElementById('ns_shield_csp_header_hard');
54 + if (lightToggle && hardToggle) {
55 + function updateHardToggle() {
56 + hardToggle.checked = false;
57 + hardToggle.disabled = lightToggle.checked;
58 + }
59 + updateHardToggle();
60 + lightToggle.addEventListener('change', updateHardToggle);
61 + var form = lightToggle.closest('form');
62 + if (form) {
63 + form.addEventListener('submit', function () {
64 + if (lightToggle.checked) {
65 + hardToggle.checked = false;
66 + }
67 + hardToggle.disabled = false;
68 + });
69 + }
70 + }
71 +
72 + // 5) Remember popup shown state on form submit
73 + var preloadCb = document.getElementById('ns-shield-preload-checkbox');
74 + if (preloadCb) {
75 + var preloadForm = preloadCb.closest('form');
76 + if (preloadForm) {
77 + preloadForm.addEventListener('submit', function () {
78 + var key = preloadCb.checked
79 + ? 'nsShieldModalShownEnable'
80 + : 'nsShieldModalShownDisable';
81 + localStorage.setItem(key, '1');
82 + });
83 + }
84 + }
85 +});