Skip to main content

Canvas File Downloader

0 likes • Nov 18, 2022 • 4 views
JavaScript
Loading...

More JavaScript Posts

ESPN Web Scrape

0 likes • Oct 15, 2023 • 6 views
JavaScript
//Disclaimer: I do not condone mass web scraping of ESPN websites, and this is just theoretical code that hasn't been used.
//Scrape player stats off of https://www.espn.com/nfl/team/roster/_/name/phi/philadelphia-eagles
//Example player JS object
/*
{
"shortName": "S. Opeta",
"name": "Sua Opeta",
"href": "http://www.espn.com/nfl/player/_/id/3121009/sua-opeta",
"uid": "s:20~l:28~a:3121009",
"guid": "dec19157-e984-f981-3724-498281328c97",
"id": "3121009",
"height": "6' 4\"",
"weight": "305 lbs",
"age": 27,
"position": "G",
"jersey": "78",
"birthDate": "08/15/96",
"headshot": "https://a.espncdn.com/i/headshots/nfl/players/full/3121009.png",
"lastName": "Sua Opeta",
"experience": 4,
"college": "Weber State"
}
*/
//The page needs to be focused, so you have to put this code in a bookmarklet and click the page before clicking it. Allegedly.
navigator.clipboard.writeText(JSON.stringify(
window["__espnfitt__"].page.content.roster.groups.flatMap((group)=>{
return group.athletes.map((athlete)=>{
//We can assume all football players are above 100 lbs and below 1000 lbs.
athlete.weight = parseInt(athlete.weight.substring(0,3));
if(athlete.experience == "R") athlete.experience = 0;
else athlete.experience = parseInt(athlete.experience);
//We can assume players are at least 1 foot, or under 10 feet tall.
athlete.inches = 12 * parseInt(athlete.height.substring(0, 1)); //Add feet in inches
athlete.inches += parseInt(athlete.height.substring(2).replaceAll("\"", "").trim()); //Add remaining inches
const monthDayYear = athlete.birthDate.split("/");
athlete.birthMonth = parseInt(monthDayYear[0]);
athlete.birthDay = parseInt(monthDayYear[1]);
athlete.birthYear = parseInt(monthDayYear[2]);
//The only really useful stuff we get from this is Height, weight, left handedness, age, position, and birthday
return athlete;
});
})
, null, "\t")).then(null, ()=>{alert("That failed.")});
//Changes all the team links on this page https://www.espn.com/nfl/stats/team to the team's roster for easier scraping
$$("table > tbody > tr > td > div > div > a").forEach((elm)=>{
elm.setAttribute("href", elm.getAttribute("href").replace("team/", "team/roster/"));
});

drag and drop uploader

0 likes • Nov 18, 2022 • 0 views
JavaScript
const dragAndDropDiv = editor.container;
dragAndDropDiv.addEventListener('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
dragAndDropDiv.addEventListener("drop",function(e){
// Prevent default behavior (Prevent file from being opened)
e.stopPropagation();
e.preventDefault();
const files = e.dataTransfer.items; // Array of all files
console.assert(files.length >= 1);
if (files[0].kind === 'file') {
var file = e.dataTransfer.items[0].getAsFile();
const fileSize = file.size;
const fileName = file.name;
const fileMimeType = file.type;
reader = new FileReader();
reader.onloadend = function(){
editor.setValue(reader.result);
}
reader.readAsText(file);
}else{
//Maybe handle if text is dropped
console.log(files[0].kind);
}
});
function printHeap(heap, index, level) {
if (index >= heap.length) {
return;
}
console.log(" ".repeat(level) + heap[index]);
printHeap(heap, 2 * index + 1, level + 1);
printHeap(heap, 2 * index + 2, level + 1);
}
//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.
let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];
printHeap(heap,0,0)

count substrings

0 likes • Nov 19, 2022 • 0 views
JavaScript
const countSubstrings = (str, searchValue) => {
let count = 0,
i = 0;
while (true) {
const r = str.indexOf(searchValue, i);
if (r !== -1) [count, i] = [count + 1, r + 1];
else return count;
}
};
countSubstrings('tiktok tok tok tik tok tik', 'tik'); // 3
countSubstrings('tutut tut tut', 'tut'); // 4

UNT canvas file finder utility

0 likes • Nov 18, 2022 • 0 views
JavaScript
//use this with canvas file finder
function NewTab(testing) {
window.open(testing, "_blank");
}
for(test in results) {
var testing = 'https://unt.instructure.com/files/' + test + '/download';
NewTab(testing);
}

Date Time Testing

0 likes • Nov 18, 2022 • 1 view
JavaScript
var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);