feat: add enhance-content web component
This commit is contained in:
parent
56c14cb879
commit
165a00570e
1 changed files with 37 additions and 0 deletions
37
static/enhance-content.js
Normal file
37
static/enhance-content.js
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
class EnhanceContent extends HTMLElement {
|
||||||
|
connectedCallback() {
|
||||||
|
const el = this.getElementsByClassName("hashtag");
|
||||||
|
const server = this.dataset.server;
|
||||||
|
const tagUrl = this.dataset.tagUrl;
|
||||||
|
|
||||||
|
for (let i = 0; i < el.length; i++) {
|
||||||
|
const tagEl = el.item(i);
|
||||||
|
const currentHref = tagEl.getAttribute("href");
|
||||||
|
const tagName = currentHref.replace(`${server}/tags/`, "");
|
||||||
|
tagEl.setAttribute("href", tagUrl + tagName);
|
||||||
|
|
||||||
|
const parentEl = tagEl.parentElement;
|
||||||
|
const siblings = parentEl.childNodes;
|
||||||
|
let validSiblingsCount = 0;
|
||||||
|
|
||||||
|
for (const sibling of siblings) {
|
||||||
|
if (!(sibling.nodeType === 3 && sibling.textContent === " ")) {
|
||||||
|
validSiblingsCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const childrenTags = parentEl.getElementsByClassName("hashtag");
|
||||||
|
|
||||||
|
console.log({ childrenTags, siblings, validSiblingsCount });
|
||||||
|
|
||||||
|
const isTagBar = validSiblingsCount === childrenTags.length;
|
||||||
|
|
||||||
|
if (isTagBar) {
|
||||||
|
tagEl.textContent = tagName;
|
||||||
|
tagEl.classList.add("pill");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
customElements.define("enhance-content", EnhanceContent);
|
Loading…
Reference in a new issue