Skip to content

Instantly share code, notes, and snippets.

@rileyrichter
Created October 23, 2024 17:25
Show Gist options
  • Save rileyrichter/cd9c74caf4306dc16ebba2d24f5ff131 to your computer and use it in GitHub Desktop.
Save rileyrichter/cd9c74caf4306dc16ebba2d24f5ff131 to your computer and use it in GitHub Desktop.
/*
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// get the empty list element
const emptyList = document.querySelector(".w-dyn-empty");
// get the general list element
const genList = document.querySelector("#genList");
// add an event listener to the DOMContentLoaded event
document.addEventListener("DOMContentLoaded", function () {
// if the empty list element exists
if (emptyList) {
// get the parent element of the empty list with a class of article_list-wrapper
const articleListWrapper = emptyList.closest(".article_list-wrapper");
// remove the article list wrapper
articleListWrapper.remove();
// make the general list element visible
genList.style.display = "block";
// log to the console
console.log("Removed empty list, made general list visible");
} else {
// log to the console
console.log("No empty list found, removed general list");
// remove the general list element
genList.remove();
}
fetchDataFromLastLink();
});
// get the last link that is not empty element in the element with an id of breadCrumb
const lastLink = Array.from(
document.querySelectorAll("#breadCrumb a:not(:empty)")
).pop();
// build a URL to fetch the data from the last link
// use this base url https://myapi.com/api/v1/
// slugify the text of the last link and append it to the base url
// use the fetch api to get the data from the url
// Function to slugify text
function slugify(text) {
return text
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^\w\-]+/g, "")
.replace(/\-\-+/g, "-")
.trim();
}
// Build URL and fetch data
function fetchDataFromLastLink() {
// build the base url
const baseUrl = "https://myapi.com/api/v1/";
// slugify the text of the last link
const slug = slugify(lastLink.textContent);
// build the full url
const url = `${baseUrl}${slug}`;
// console log the url
console.log("URL:", url);
/* fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log("Data fetched:", data);
// Handle the data as needed
})
.catch((error) => {
console.error("Error fetching data:", error);
}); */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment