Skip to content

Commit

Permalink
move callbacks into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
usyless committed Sep 18, 2024
1 parent bbe1308 commit 806434e
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions twitter_improvements.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,17 @@
try {
article.setAttribute('usy', '');
const a = Tweet.anchor(article);
a.after(Button.newButton(a, vx_button_path, () => {
try {navigator.clipboard.writeText(URL.vxIfy(Tweet.url(article))).then(() => {Notification.create('Copied URL to clipboard');});}
catch {Notification.create('Failed to copy url, please report the issue along with the current url to twitter improvements');}
}));
} catch {article.removeAttribute('usy')}
a.after(Button.newButton(a, vx_button_path, () => Tweet.vxButtonCallback(article)));
} catch {article.removeAttribute('usy');}
}

static addVideoButton(videoComponent) {
try {
videoComponent.setAttribute('usy', '');
const article = Tweet.nearestTweet(videoComponent), a = Tweet.anchor(article), quote_tweet = article.querySelector('div[id] > div[id]');
if (quote_tweet != null && quote_tweet.contains(videoComponent)) return;
if (!article.querySelector('.usybuttonclickdiv[usy-video]')) a.after(Button.newButton(a, download_button_path, () => {
Notification.create('Saving Tweet Video(s)');
chrome.runtime.sendMessage({type: 'video', url: Tweet.url(article)}).then((r) => {
if (r) Notification.create('Successfully Downloaded Video(s)');
else Notification.create('Failed to download video(s), please try again (cobalt error)');
});
}, "usy-video"));
if (!(quote_tweet != null && quote_tweet.contains(videoComponent)) && !article.querySelector('.usybuttonclickdiv[usy-video]')) {
a.after(Button.newButton(a, download_button_path, () => Tweet.videoButtonCallback(article), "usy-video"));
}
} catch {videoComponent.removeAttribute('usy')}
}

Expand All @@ -58,17 +50,26 @@
elem = elem.parentElement;
}
}

static vxButtonCallback(article) {
try {navigator.clipboard.writeText(URL.vxIfy(Tweet.url(article))).then(() => {Notification.create('Copied URL to clipboard');});}
catch {Notification.create('Failed to copy url, please report the issue along with the current url to twitter improvements');}
}

static videoButtonCallback(article) {
Notification.create('Saving Tweet Video(s)');
chrome.runtime.sendMessage({type: 'video', url: Tweet.url(article)}).then((r) => {
if (r) Notification.create('Successfully Downloaded Video(s)');
else Notification.create('Failed to download video(s), please try again (cobalt error)');
});
}
}

class Image { // Image element functions
static addImageButton(image) {
try {
image.setAttribute('usy', '');
image.after(Button.newButton(Tweet.anchorWithFallback(Tweet.nearestTweet(image)), download_button_path, (e) => {
e.preventDefault();
Notification.create('Saving Image');
chrome.runtime.sendMessage({type: 'image', url: Image.respectiveURL(image), sourceURL: image.src});
}));
image.after(Button.newButton(Tweet.anchorWithFallback(Tweet.nearestTweet(image)), download_button_path, (e) => Image.imageButtonCallback(e, image)));
} catch {image.removeAttribute('usy')}
}

Expand All @@ -82,6 +83,12 @@
url = window.location.href;
if (url.includes('/photo/')) return url;
}

static imageButtonCallback(e, image) {
e.preventDefault();
Notification.create('Saving Image');
chrome.runtime.sendMessage({type: 'image', url: Image.respectiveURL(image), sourceURL: image.src});
}
}

class URL { // URL modification functions
Expand Down

0 comments on commit 806434e

Please sign in to comment.