-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathembed.js
More file actions
184 lines (165 loc) · 5.34 KB
/
embed.js
File metadata and controls
184 lines (165 loc) · 5.34 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Free Crypto News — Embeddable Widget Loader
*
* Lightweight script (<5KB) that creates an iframe pointing to /embed/* routes.
*
* Usage:
* <script src="https://cryptocurrency.cv/widget/embed.js"
* data-type="ticker"
* data-theme="dark"
* data-count="10"
* data-coin="bitcoin"
* data-title="true"
* data-width="100%"
* ></script>
*
* Supported data-type values: ticker, news, coin, market, fear-greed, chart
*
* @copyright 2024-2026 nirholas. All rights reserved.
* @license SPDX-License-Identifier: SEE LICENSE IN LICENSE
* @see https://cryptocurrency.cv
*/
(function () {
"use strict";
var BASE_URL = "https://cryptocurrency.cv";
var VALID_TYPES = ["ticker", "news", "coin", "market", "fear-greed", "chart"];
var VALID_THEMES = ["dark", "light", "auto"];
// Default heights per widget type
var DEFAULT_HEIGHTS = {
ticker: 48,
news: 600,
coin: 320,
market: 380,
"fear-greed": 280,
chart: 500,
};
/**
* Find the current script tag (supports multiple widgets on one page).
*/
function getCurrentScript() {
if (document.currentScript) return document.currentScript;
// Fallback for IE11
var scripts = document.querySelectorAll(
'script[src*="widget/embed.js"], script[src*="embed.js"]',
);
return scripts[scripts.length - 1] || null;
}
/**
* Read data attributes from a script element.
*/
function readConfig(script) {
var type = (script.getAttribute("data-type") || "ticker").toLowerCase();
if (VALID_TYPES.indexOf(type) === -1) type = "ticker";
var theme = (script.getAttribute("data-theme") || "dark").toLowerCase();
if (VALID_THEMES.indexOf(theme) === -1) theme = "dark";
var count = parseInt(script.getAttribute("data-count") || "10", 10);
if (isNaN(count) || count < 1 || count > 50) count = 10;
var coin = (script.getAttribute("data-coin") || "bitcoin").toLowerCase();
var title = script.getAttribute("data-title");
var width = script.getAttribute("data-width") || "100%";
var symbol = script.getAttribute("data-symbol") || "BINANCE:BTCUSDT";
var interval = script.getAttribute("data-interval") || "D";
return {
type: type,
theme: theme,
count: count,
coin: coin,
showTitle: title !== "false",
width: width,
symbol: symbol,
interval: interval,
};
}
/**
* Build the embed URL from config.
*/
function buildUrl(config) {
var params = [];
params.push("theme=" + encodeURIComponent(config.theme));
if (config.type === "news") {
params.push("count=" + config.count);
}
if (config.type === "coin") {
params.push("coin=" + encodeURIComponent(config.coin));
}
if (!config.showTitle) {
params.push("title=false");
}
if (config.type === "chart") {
params.push("symbol=" + encodeURIComponent(config.symbol));
params.push("interval=" + encodeURIComponent(config.interval));
}
return BASE_URL + "/embed/" + config.type + "?" + params.join("&");
}
/**
* Calculate iframe height based on config.
*/
function getHeight(config) {
if (config.type === "news") {
var base = 40;
var itemHeight = 76;
var titleHeight = config.showTitle ? 40 : 0;
var brandingHeight = 32;
return base + config.count * itemHeight + titleHeight + brandingHeight;
}
return DEFAULT_HEIGHTS[config.type] || 400;
}
/**
* Create and insert the iframe.
*/
function createWidget(script) {
var config = readConfig(script);
var url = buildUrl(config);
var height = getHeight(config);
// Create container
var container = document.createElement("div");
container.className = "fcn-widget-container";
container.style.cssText =
"width:" +
config.width +
";max-width:100%;margin:0;padding:0;overflow:hidden;border-radius:8px;";
// Create iframe
var iframe = document.createElement("iframe");
iframe.src = url;
iframe.width = "100%";
iframe.height = height;
iframe.style.cssText =
"border:none;display:block;width:100%;overflow:hidden;border-radius:8px;";
iframe.setAttribute("loading", "lazy");
iframe.setAttribute("title", "Free Crypto News Widget");
iframe.setAttribute(
"sandbox",
"allow-scripts allow-same-origin allow-popups allow-popups-to-escape-sandbox",
);
container.appendChild(iframe);
// Insert after the script tag
if (script.parentNode) {
script.parentNode.insertBefore(container, script.nextSibling);
}
// Listen for resize messages from the widget iframe
window.addEventListener("message", function (event) {
if (!event.data || typeof event.data !== "object") return;
if (event.data.type !== "fcn-widget-resize") return;
if (event.source === iframe.contentWindow) {
iframe.height = event.data.height;
}
});
}
// Initialize
var script = getCurrentScript();
if (script) {
// Use DOMContentLoaded if DOM is not ready, otherwise create immediately
if (
document.readyState === "complete" ||
document.readyState === "interactive"
) {
createWidget(script);
} else {
var onReady = function () {
createWidget(script);
document.removeEventListener("DOMContentLoaded", onReady);
};
document.addEventListener("DOMContentLoaded", onReady);
}
}
})();