Created
June 12, 2020 11:08
-
-
Save apple502j/a2806b4b6d111a911619b8936b0b93ac to your computer and use it in GitHub Desktop.
Leave studios easily
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let username = ''; | |
const leaveStudioConstructor = (studioId, elem) => (e => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
if (!username) return; | |
$.ajax({type: 'PUT', url: `https://scratch.mit.edu/site-api/users/curators-in/${studioId}/remove/?usernames=${username}`}); | |
}); | |
const getStudioIdFromMediaItemContent = mediaItemContent => mediaItemContent.children[0].children[0].href.match(/\d+/g)[0]; | |
const canDelete = mediaItemContent => !!mediaItemContent.children[2].children[2]; | |
const addLeaveButton = mediaItemContent => { | |
if (canDelete(mediaItemContent)) return; | |
const link = document.createElement('a'); | |
link.textContent = 'Leave'; | |
link.class = 'leaveStudio'; | |
const studioId = getStudioIdFromMediaItemContent(mediaItemContent); | |
link.addEventListener('click', leaveStudioConstructor(studioId, mediaItemContent)); | |
link.id = `leaveStudio${studioId}`; | |
mediaItemContent.children[2].appendChild(link); | |
}; | |
const addButtons = () => { | |
username = Scratch.INIT_DATA.LOGGED_IN_USER.model.username; | |
setTimeout(() => { | |
for (const item of document.getElementsByClassName('media-item-content')) addLeaveButton(item); | |
const button = document.querySelector('div[data-control="load-more"]'); | |
if (button) { | |
try { | |
button.removeEventListener('click', addButtons); | |
} catch (e) { | |
} | |
button.addEventListener('click', addButtons); | |
} | |
}, 1500); | |
}; | |
addEventListener('hashchange', e => { | |
if (!e.newURL.includes('galleries')) return; | |
addButtons(); | |
}); | |
addEventListener('load', () => { | |
if (location.hash !== '#galleries') return; | |
addButtons(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment