gopherss/views/static/feed-item.js

142 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-10-17 13:30:30 +00:00
class FeedItem extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
}
2020-10-17 19:52:23 +00:00
connectedCallback() {
2020-10-17 13:30:30 +00:00
const template = document.createElement('template');
template.innerHTML = `
<style>
2020-10-17 19:52:23 +00:00
2020-10-17 18:50:13 +00:00
:host {
width: 100% !important;
overflow: scroll !important;
overflow-x: auto !important;
2020-11-08 19:16:10 +00:00
font-size: 18px;
2020-10-17 13:30:30 +00:00
}
* {
2020-10-17 18:50:13 +00:00
max-width: 100% !important;
height: auto !important;
2020-11-08 19:16:10 +00:00
float: none !important;
2020-10-17 18:02:52 +00:00
}
table {
2020-10-17 18:50:13 +00:00
width: 100% !important;
2020-10-17 13:30:30 +00:00
}
2020-11-08 19:16:10 +00:00
2020-10-17 13:30:30 +00:00
img {
2020-10-17 18:50:13 +00:00
margin: auto auto !important;
2020-10-17 13:30:30 +00:00
}
2021-02-21 09:30:11 +00:00
h1, h2, h3, h4 {
2021-05-15 10:03:19 +00:00
font-family: "Atkinson Hyperlegible Bold";
2021-02-21 09:30:11 +00:00
margin-top: 1.3em;
2021-05-15 14:27:11 +00:00
line-height: 1em;
2021-02-21 09:30:11 +00:00
}
2021-02-21 11:04:01 +00:00
:root > h1 {
2021-02-21 09:30:11 +00:00
margin-top: 0;
}
p, a {
line-height: 1.2em;
}
2021-05-15 10:03:19 +00:00
p, li, div {
font-family: "Atkinson Hyperlegible Regular";
2020-10-17 19:52:23 +00:00
font-style: normal;
font-weight: 400;
2021-05-15 10:03:19 +00:00
letter-spacing: 0.05em
}
em {
font-family: "Atkinson Hyperlegible Italic";
font-style: normal;
}
strong {
font-weight: 500;
font-family: "Atkinson Hyperlegible Bold";
}
em strong, strong em {
font-family: "Atkinson Hyperlegible BoldItalic";
}
li {
margin: 0.6em 0;
2020-10-17 18:50:13 +00:00
}
a {
color: #333;
2021-05-15 10:03:19 +00:00
font-family: "Atkinson Hyperlegible Bold";
font-weight: 500;
letter-spacing: 0.05em
2020-10-17 18:50:13 +00:00
}
:host(.dark) a {
2021-05-15 10:03:19 +00:00
color: #eee;
2020-10-17 18:50:13 +00:00
}
2020-11-08 19:16:10 +00:00
a:hover, :host(.dark) a:hover {
color: #ff2e88;
}
2020-10-17 18:50:13 +00:00
2021-02-21 09:30:11 +00:00
pre {
overflow-x: scroll;
padding: 8px;
background: #62848463;
}
pre code {
margin-right: 8px;
}
p code {
background: #62848463;
padding: 0 4px;
}
2021-02-21 09:55:33 +00:00
iframe {
display: block;
width: 100%;
min-height: 600px;
border: none;
}
2020-10-17 13:30:30 +00:00
</style>
`;
2020-10-17 19:52:23 +00:00
fetch(`/api/item/${this.getAttribute('item-id')}`)
.then(res => res.json())
.then(item => {
2021-02-21 09:30:11 +00:00
template.innerHTML += `<h1><a href="${item.URL}" target="_blank" rel="noopener">${item.Title}</a></h1>`;
2021-02-21 09:55:33 +00:00
template.innerHTML += `<div class="feedContent">${item.Content || item.Description}</div>`;
template.innerHTML += `<iframe style="display: none;" data-src="${item.URL}"></iframe>`
2020-10-17 19:52:23 +00:00
this.shadowRoot.appendChild(template.content.cloneNode(true));
2020-11-07 16:30:15 +00:00
[...this.shadowRoot.querySelectorAll('a[href^=http]')].forEach(a => {
a.setAttribute("target", "_blank");
a.setAttribute("rel", "noopener");
2020-11-09 21:19:15 +00:00
});
[...this.shadowRoot.querySelectorAll('p')].forEach(p => {
if (p.innerText.trim() == "") {
p.remove();
}
2020-11-18 19:02:31 +00:00
});
let url = new URL(item.URL);
[...this.shadowRoot.querySelectorAll('img[src^="/"]')].forEach(i => {
i.src = url.origin + i.getAttribute('src');
});
[...this.shadowRoot.querySelectorAll('a[href^="/"]')].forEach(a => {
2021-03-03 19:54:54 +00:00
a.href = url.origin + a.getAttribute('href');
2020-11-18 19:02:31 +00:00
});
[...this.shadowRoot.querySelectorAll('img:not([src^=http])')].forEach(i => {
i.src = url.origin +'/'+ i.getAttribute('src');
});
[...this.shadowRoot.querySelectorAll('a:not([href^=http])')].forEach(a => {
2021-03-03 19:54:54 +00:00
a.href = url.origin +'/'+ a.getAttribute('href');
2020-11-18 19:02:31 +00:00
});
2020-10-17 19:52:23 +00:00
})
2021-02-21 09:55:33 +00:00
}
2020-10-17 19:52:23 +00:00
2021-02-21 09:55:33 +00:00
showIframe() {
2021-03-17 09:07:46 +00:00
if (this.shadowRoot.querySelector(".feedContent").style.display != "none") {
this.shadowRoot.querySelector(".feedContent").style.display = "none";
this.shadowRoot.querySelector("iframe").src = this.shadowRoot.querySelector("iframe").dataset.src;
this.shadowRoot.querySelector("iframe").style.display = "block";
} else {
this.shadowRoot.querySelector(".feedContent").style.display = "block";
this.shadowRoot.querySelector("iframe").style.display = "none";
}
2020-10-17 13:30:30 +00:00
}
}
customElements.define('feed-item', FeedItem);