-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
152 lines (128 loc) · 6.05 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class MarqueeFeedPlugin {
constructor(API, name, config) {
this.API = API;
this.name = name;
this.config = config;
}
addInsertions() {
this.API.addInsertion('publiiHead', this.injectCSS.bind(this), 1, this);
this.API.addInsertion('publiiHead', this.addMarqueeFeed.bind(this), 1, this);
}
injectCSS() {
let animationSpeed = this.config.animationSpeed || '100';
let backgroundColor = this.config.backgroundColor || 'black';
let textSize = this.config.textSize || '14px';
let textColor = this.config.textColor || 'white';
let linkHoverColor = this.config.linkHoverColor || 'blue';
let marqueeWidth = this.config.marqueeWidth || '100%';
let borderColor = this.config.borderColor || 'white';
let borderTop = this.config.borderTop || 'dashed';
let borderBottom = this.config.borderBottom || 'dashed';
return `
<style>
.marquee {
overflow: hidden;
position: relative;
background-color: ${backgroundColor};
z-index: 1000;
width: ${marqueeWidth};
border-top: ${borderColor} ${borderTop};
border-bottom: ${borderColor} ${borderBottom};
}
.marquee::before {
content: '\u00A0';
}
.scrolling {
animation: marquee ${animationSpeed}s linear infinite;
background-color: ${backgroundColor};
color: ${textColor};
font-size: ${textSize};
display: inline-block;
padding-right: 1px;
position: absolute;
left: 0;
top: 0;
white-space: nowrap;
}
.scrolling a{
color: ${textColor};
}
.scrolling a:hover{
color: ${linkHoverColor};
}
.scrolling:hover {
animation-play-state: paused;
}
@keyframes marquee {
from {
transform: translateX(30%);
}
to {
transform: translateX(-100%);
}
}
</style>
`;
}
addMarqueeFeed(rendererInstance, context) {
let feedUrl = this.config.feedUrl;
let marqueeSeparator = this.config.marqueeSeparator || '|';
let infoMessage = this.config.info || '';
let showOnMobile = this.config.showOnMobile;
let targetElementSelector = this.config.targetElementSelector || 'body';
let scriptContent = `
document.addEventListener("DOMContentLoaded", function() {
function loadFeedXML(url) {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(xhr.responseText, "application/xml");
let entries = xmlDoc.getElementsByTagName("entry");
let contenedor = document.createElement("div");
let scrolling = document.createElement('div');
let targetElement = document.querySelector(' ${targetElementSelector} ');
contenedor.className = "marquee";
scrolling.className = 'scrolling';
scrolling.setAttribute('aria-hidden', 'true');
let infoMessageContent = document.createTextNode('${infoMessage}' + " ");
scrolling.appendChild(infoMessageContent);
contenedor.appendChild(scrolling);
Array.from(entries).forEach((entry, index, array) => {
let title = entry.getElementsByTagName("title")[0].textContent;
let link = entry.getElementsByTagName("link")[0].getAttribute("href");
let elementoA = document.createElement("a");
elementoA.setAttribute("href", link);
elementoA.textContent = title;
scrolling.appendChild(elementoA);
if (index < array.length - 1) {
scrolling.appendChild(document.createTextNode(' ${marqueeSeparator} '));
}
});
if (targetElement) {
targetElement.appendChild(contenedor);
} else {
document.body.insertBefore(contenedor, document.body.firstChild);
}
if (!${showOnMobile} && window.innerWidth <= 768) {
if (contenedor) {
contenedor.style.display = 'none';
}
}
} else {
console.error("Marquee feed plugin: Error loading the XML feed: ", xhr.statusText);
}
};
xhr.onerror = function() {
console.error("Marquee feed plugin: Network error while fetching the XML feed.");
};
xhr.send();
}
loadFeedXML('${feedUrl}');
});
`;
return `<script type="text/javascript">${scriptContent}</script>`;
}
}
module.exports = MarqueeFeedPlugin;