Skip to content

Instantly share code, notes, and snippets.

@apple502j
Created June 12, 2020 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apple502j/a2806b4b6d111a911619b8936b0b93ac to your computer and use it in GitHub Desktop.
Save apple502j/a2806b4b6d111a911619b8936b0b93ac to your computer and use it in GitHub Desktop.
Leave studios easily
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