Experiment with shadow DOM, unfinished

This commit is contained in:
ilyaigpetrov 2025-09-19 06:57:40 +05:00
parent de62b28674
commit aaf9544b61
4 changed files with 54 additions and 20 deletions

10
package-lock.json generated
View File

@ -1,15 +1,15 @@
{ {
"name": "copy-unicode-urls", "name": "runet-censorship-bypass",
"version": "0.0.22", "version": "2.0.0-rc0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "copy-unicode-urls", "name": "runet-censorship-bypass",
"version": "0.0.22", "version": "2.0.0-rc0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"punycode": "^2.1.1" "punycode": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"merge": "^2.1.1", "merge": "^2.1.1",

View File

@ -20,6 +20,7 @@
}, },
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"punycode": "^2.3.1"
}, },
"devDependencies": { "devDependencies": {
"merge": "^2.1.1", "merge": "^2.1.1",

View File

@ -187,10 +187,10 @@
<div> <div>
<span id="attributes" hidden> <span id="attributes" hidden>
<slot name="id">ID</slot> <slot name="id">ID</slot>
<slot name="value"><span slot="id"></span></slot> <slot name="value"></slot>
<slot name="for">FOR</slot> <slot name="for">FOR</slot>
</span> </span>
<input type="radio" name="pacScript" data-attrs="id value"> <input type="radio" name="pacScript" form="pacChooserForm" data-attrs="id value">
<label data-attrs="for"> <label data-attrs="for">
<slot name="label">LABEL</slot> <slot name="label">LABEL</slot>
</label> </label>
@ -203,8 +203,8 @@
<pac-record> <pac-record>
<span slot="label">Антизапрет</span> <span slot="label">Антизапрет</span>
<span slot="id">antizapret</span> <span slot="id">antizapret</span>
<!--span slot="value">antizapret</span> <span slot="value">antizapret</span>
<span slot="for">antizapret</span--> <span slot="for">antizapret</span>
</pac-record> </pac-record>
</li> </li>
<li> <li>
@ -215,10 +215,6 @@
<span slot="for">anticensority</span> <span slot="for">anticensority</span>
</pac-record> </pac-record>
</li> </li>
<!--li>
<input type="radio" value="anticensority" name="pacScript" id="anticensorityRadio">
<label for="anticensorityRadio">Антицензорити</label>
</li-->
<li> <li>
<input type="radio" value="own" name="pacScript" id="ownRadio" disabled> <input type="radio" value="own" name="pacScript" id="ownRadio" disabled>
<label for="ownRadio">Свой:</label> <label for="ownRadio">Свой:</label>

View File

@ -2,12 +2,17 @@ console.log('Options page is opening...');
customElements.define('pac-record', customElements.define('pac-record',
class extends HTMLElement { class extends HTMLElement {
static formAssociated = true;
// static observedAttributes = ['data-checked'];
constructor() { constructor() {
super(); super();
this.internals = this.attachInternals();
const shadow = this.attachShadow({ mode: 'open' }); const shadow = this.attachShadow({ mode: 'open' });
// const shadow = this.internals.shadowRoot;
console.log('Constructor');
const fragment = pacRecordTemplate.content.cloneNode(true); const fragment = pacRecordTemplate.content.cloneNode(true);
shadow.appendChild(fragment); shadow.appendChild(fragment);
const templateAttrs = shadow.querySelectorAll('#attributes > slot[name]'); //const templateAttrs = shadow.querySelectorAll('#attributes > slot[name]');
const dataAttrs = shadow.querySelectorAll('*[data-attrs]'); const dataAttrs = shadow.querySelectorAll('*[data-attrs]');
dataAttrs.forEach( dataAttrs.forEach(
(da) => { (da) => {
@ -16,13 +21,37 @@ customElements.define('pac-record',
console.log(shadow.querySelector(`#attributes > slot[name=${attr}]`)); console.log(shadow.querySelector(`#attributes > slot[name=${attr}]`));
da.setAttribute( da.setAttribute(
attr, attr,
shadow.querySelector(`#attributes > slot[name=${attr}]`).assignedNodes()[0].textContent, shadow.querySelector(`#attributes > slot[name=${attr}]`).assignedNodes()?.[0].textContent,
); );
}, },
); );
} }
); );
const namedInputs = shadow.querySelectorAll('input[name]');
namedInputs.forEach((ni) => {
//this.internals.setFormValue(ni.name);
ni.addEventListener('click',
(event) => {
const t = event.target;
const entries = new FormData();
//entries.set(ni.getAttribute('name'), ni.getAttribute('value'));
entries.set('pacScript', 'antizapret');
this.internals.setFormValue(t.checked ? t.value : null);
ni.toggleAttribute('checked');
//this.internals.setFormValue(ni.value);
//this.toggleAttribute('checked');
//this.toggleAttribute('data-checked');
//ni.toggleAttribute('checked');
console.log('EVENT TARGET:', event.target);
console.log('THIS', this);
console.log(this.internals);
console.log('EEEE', entries);
console.log(`FFFF:${ni.name}=${ni.value}`);
return true;
})
});
/*
const input = fragment.querySelector('div > input'); const input = fragment.querySelector('div > input');
const label = fragment.querySelector('div > label'); const label = fragment.querySelector('div > label');
/*const node = pacRecordTemplate.content.cloneNode(true); /*const node = pacRecordTemplate.content.cloneNode(true);
@ -31,9 +60,16 @@ customElements.define('pac-record',
input.id = input.value = label.htmlFor = this.dataset.id;*/ input.id = input.value = label.htmlFor = this.dataset.id;*/
} }
/*
attributeChangedCallback(name, oldValue, newValue) {
console.log('attributeChangedCallback:', oldValue, '->', newValue);
const entries = new FormData();
entries.set('pacScript', 'antizapret');
this.internals.setFormValue(entries);
}*/
} }
); );
/*
customPacUrl.addEventListener('change', function (event) { customPacUrl.addEventListener('change', function (event) {
console.log('ON CHANGE:', event); console.log('ON CHANGE:', event);
pacChooserForm.reportValidity(); pacChooserForm.reportValidity();
@ -44,7 +80,7 @@ pacChooserForm.addEventListener('formdata', (event) => {
console.log('ON FORMDATA', event); console.log('ON FORMDATA', event);
return false; // Prevent default action. return false; // Prevent default action.
}); });
*/ *//*
let LAST_LOCKED_URL = ''; let LAST_LOCKED_URL = '';
const lockUrl = () => { const lockUrl = () => {
@ -55,7 +91,7 @@ const lockUrl = () => {
ownRadio.disabled = false; ownRadio.disabled = false;
// TODO: Save to storage. // TODO: Save to storage.
} else { } else {
pacChooserForm.reportValidity(); //pacChooserForm.reportValidity();
ownRadio.disabled = true; // `ownRadio.checked` doesn't matter here. ownRadio.disabled = true; // `ownRadio.checked` doesn't matter here.
} }
}; };
@ -82,7 +118,7 @@ cancelPacUrlButton.onclick = suppressDefaultHandler(
); );
editPacUrlButton.onclick = suppressDefaultHandler(unlockUrl); editPacUrlButton.onclick = suppressDefaultHandler(unlockUrl);
/* *//*
import { storage } from '../../lib/common-apis.mjs'; import { storage } from '../../lib/common-apis.mjs';
donate.href = await storage.getAsync('donateUrl'); donate.href = await storage.getAsync('donateUrl');
@ -102,6 +138,7 @@ options.forEach(([key, value], i) => {
}; };
}); });
*/ */
/*
await chrome.storage.local.get('options'); await chrome.storage.local.get('options');
const textElements = document.querySelectorAll('[data-localize]'); const textElements = document.querySelectorAll('[data-localize]');
@ -113,4 +150,4 @@ textElements.forEach((e) => {
e.innerText = translated; e.innerText = translated;
} }
} }
}); });*/