94 lines
3.4 KiB
JavaScript
94 lines
3.4 KiB
JavaScript
/**
|
|
* @name Keyboard Plus
|
|
* @description Extra shortcuts on the post focused with X's j/k: c copies its link, Shift+C copies its text, v opens its first photo or video, a opens the author's profile and q opens the quoted post. Only navigates or copies; never likes, reposts or posts.
|
|
* @version 1.1.0
|
|
* @author wayBetter
|
|
* @permissions ui, clipboard, network:none
|
|
*/
|
|
// i18n:begin
|
|
// This plugin's English words. Translations live in the app's catalogs (plugin.<file name>.*,
|
|
// docs/TRANSLATING.md); a plugin of your own can also pass { en: {...}, tr: {...} } to register.
|
|
const MESSAGES = {
|
|
"en": {
|
|
"linkCopied": "Link copied",
|
|
"textCopied": "Text copied",
|
|
"copyFailed": "Copy failed",
|
|
"noMedia": "No media on this post",
|
|
"noQuote": "No quoted post"
|
|
}
|
|
};
|
|
// i18n:end
|
|
/** The words in the app's language (api.i18n); English before start() and outside the app. */
|
|
let i18n = null;
|
|
const t = (key, params = {}) => (i18n ? i18n.t(key, params) : String(MESSAGES.en[key] ?? key)
|
|
.replace(/\{(\w+), plural, one \{([^{}]*)\} other \{([^{}]*)\}\}/g, (m, name, one, other) => (Number(params[name]) === 1 ? one : other).replace('#', String(params[name])))
|
|
.replace(/\{(\w+)\}/g, (m, name) => (name in params ? String(params[name]) : m)));
|
|
module.exports = class KeyboardPlus {
|
|
start() {
|
|
const api = window.wayBetter.api;
|
|
i18n = api.i18n || null;
|
|
if (i18n) i18n.register(MESSAGES);
|
|
this.api = api;
|
|
this.scope = api.scope();
|
|
this.scope.onKey((event, article) => {
|
|
const action = this.actions()[event.key];
|
|
if (!action || (action.needsPost && !article)) return;
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
action.run(article);
|
|
});
|
|
}
|
|
|
|
stop() {
|
|
this.scope.dispose();
|
|
}
|
|
|
|
actions() {
|
|
const api = this.api;
|
|
const click = (element) => {
|
|
if (!element) return false;
|
|
element.click();
|
|
return true;
|
|
};
|
|
return {
|
|
c: {
|
|
needsPost: true,
|
|
run: async article => {
|
|
const tweet = api.tweet(article);
|
|
if (tweet) api.toast(await api.copyText(tweet.url) ? t('linkCopied') : t('copyFailed'));
|
|
},
|
|
},
|
|
C: {
|
|
needsPost: true,
|
|
run: async article => {
|
|
const tweet = api.tweet(article);
|
|
if (tweet) api.toast(await api.copyText(tweet.text) ? t('textCopied') : t('copyFailed'));
|
|
},
|
|
},
|
|
v: {
|
|
needsPost: true,
|
|
run: article => {
|
|
const own = el => !el.closest('[role="link"]') || !article.contains(el.closest('[role="link"]'));
|
|
const photo = [...article.querySelectorAll('a[href*="/photo/"], a[href*="/video/"]')].find(own);
|
|
if (click(photo)) return;
|
|
const video = [...article.querySelectorAll('[data-testid="videoPlayer"], [data-testid="videoComponent"]')].find(own);
|
|
if (!click(video)) api.toast(t('noMedia'));
|
|
},
|
|
},
|
|
a: {
|
|
needsPost: true,
|
|
run: article => {
|
|
const tweet = api.tweet(article);
|
|
const link = article.querySelector('[data-testid="User-Name"] a[href^="/"]');
|
|
if (!click(link) && tweet) location.assign(`https://x.com/${tweet.handle}`);
|
|
},
|
|
},
|
|
q: {
|
|
needsPost: true,
|
|
run: article => {
|
|
if (!click(article.querySelector('[role="link"][tabindex]'))) api.toast(t('noQuote'));
|
|
},
|
|
},
|
|
};
|
|
}
|
|
};
|