This repository has been archived on 2024-09-12. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs-website/themes/svrjswebsite/source/js/indextabs.js

85 lines
3.2 KiB
JavaScript
Raw Normal View History

function escapeHTML(source) {
return source.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/[\r\n]/g, "<br/>");
}
function decodeHTML(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
var tabs = [
{
name: "GNU/Linux",
2024-04-07 05:39:57 +02:00
command: "curl -fsSL https://downloads.svrjs.org/installer/svr.js.installer.linux.20240405.sh > /tmp/installer.sh && sudo bash /tmp/installer.sh",
uaRegex: /\b(?:GNU\/)?Linux\b/gi
}
];
if (tabs.length > 0) {
var otherTab = document.getElementById("command-tab-other");
var otherTabOuterHTML = otherTab.outerHTML;
var tabsHTML = "";
var tabToClick = tabs.length;
for (var i = 0; i <= tabs.length; i++) {
tabsHTML += "<a href=\"#\" class=\"command-tab\" id=\"command-tab-" + i + "\">";
if (i == tabs.length) {
tabsHTML += "Manually";
} else {
tabsHTML += escapeHTML(tabs[i].name);
}
tabsHTML += "</a>";
if (tabToClick == tabs.length && i != tabs.length && navigator.userAgent.match(tabs[i].uaRegex)) tabToClick = i;
}
otherTab.outerHTML = "<div id=\"command-tab-tabcontainer\">" + tabsHTML + "</div><div id=\"command-tab-container\"></div>";
var tabContainer = document.getElementById("command-tab-container");
for (var i = 0; i <= tabs.length; i++) {
document.getElementById("command-tab-" + i).onclick = createTabClickHandler(i);
}
function copyHandler(e) {
e.preventDefault();
if (navigator.clipboard) {
navigator.clipboard.writeText(decodeHTML(document.getElementById("command-tab-command").innerHTML));
} else {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById("command-tab-command"));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById("command-tab-command"));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
document.execCommand("copy");
if (document.selection) {
document.selection.empty();
} else if (window.getSelection) {
window.getSelection().removeAllRanges();
}
}
document.getElementById("command-tab-copy").innerHTML = "Copied!";
}
function createTabClickHandler(i) {
return function (e) {
e.preventDefault();
for (var j = 0; j <= tabs.length; j++) {
document.getElementById("command-tab-" + j).className = "command-tab";
}
document.getElementById("command-tab-" + i).className = "command-tab command-tab-selected";
var container = document.getElementById("command-tab-container");
if (i == tabs.length) {
container.innerHTML = otherTabOuterHTML;
} else {
container.innerHTML = "<div id=\"command-tab-h\">Install SVR.JS</div><a href=\"#\" id=\"command-tab-copy\">Copy</a><code id=\"command-tab-command\" translate=\"no\">" + escapeHTML(tabs[i].command) + "</code><div class=\"command-tab-clearfix\"></div>";
document.getElementById("command-tab-copy").onclick = copyHandler;
}
}
}
document.getElementById("command-tab-" + tabToClick).click();
}