This repository has been archived on 2024-05-13. You can view files and clone it, but cannot push or open issues or pull requests.
share2fedi/lib/main.js

72 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-08-14 11:54:32 -05:00
/*!
2023-03-16 05:47:59 -05:00
* @source: https://github.com/kytta/share2fedi/blob/main/lib/main.js
*
* @licstart The following is the entire license notice for the
* JavaScript code in this page.
*
* share2fedi - Instance-agnostic share page for the Fediverse.
* Copyright (C) 2020-2023 Nikita Karamov <me@kytta.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice
* for the JavaScript code in this page.
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
2022-11-20 10:09:11 -05:00
const LOCAL_STORAGE_KEY = "recentInstances";
2021-08-14 14:56:17 -05:00
2023-03-01 06:42:33 -05:00
const $instance = document.querySelector("#instance");
2021-08-14 14:56:17 -05:00
/**
* Adds missing "https://" and ending slash to the URL
*
* @param {string} url URL to normalize
* @return {string} normalized URL
*/
function normalizeUrl(url) {
2023-03-01 06:42:33 -05:00
if (!url.includes("http://") && !url.includes("https://")) {
url = "https://" + url;
}
2023-06-17 07:08:15 -05:00
if (url.at(-1) !== "/") {
url = url + "/";
}
return url;
}
2022-11-20 10:09:11 -05:00
function getRecentInstances() {
const storedValue = window.localStorage.getItem(LOCAL_STORAGE_KEY);
if (!storedValue) return [];
return JSON.parse(storedValue);
}
let prefillInstance = getRecentInstances()[0];
2023-03-01 06:42:33 -05:00
const URLParameters = window.location.search.slice(1).split("&");
for (const URLParameter of URLParameters) {
const URLParameterPair = URLParameter.split("=");
if (URLParameterPair[0] === "text") {
document.querySelector("#text").value = decodeURIComponent(
2023-03-17 08:06:30 -05:00
URLParameterPair[1],
2023-03-01 06:42:33 -05:00
);
} else if (URLParameterPair[0] === "instance") {
prefillInstance = decodeURIComponent(URLParameterPair[1]);
}
}
2023-03-01 06:42:33 -05:00
if (prefillInstance != undefined) {
2021-08-14 14:56:17 -05:00
$instance.value = normalizeUrl(prefillInstance);
}