Skip to content

Instantly share code, notes, and snippets.

@ryuya0124
Last active December 30, 2023 13:02
Show Gist options
  • Save ryuya0124/e6e59ceba8f03bf40f3b5accedc52e44 to your computer and use it in GitHub Desktop.
Save ryuya0124/e6e59ceba8f03bf40f3b5accedc52e44 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Open X(Twitter) App
// @version 1.0.0
// @author ryuya
// @match https://twitter.com/*
// @downloadURL https://gist.github.com/ryuya0124/e6e59ceba8f03bf40f3b5accedc52e44/raw/5b5dbfa9861db9d793bbe1c12dbbb79dc3a4f1af/Open%2520X(Twitter)%2520App.user.js
// @updateURL https://gist.github.com/ryuya0124/e6e59ceba8f03bf40f3b5accedc52e44/raw/5b5dbfa9861db9d793bbe1c12dbbb79dc3a4f1af/Open%2520X(Twitter)%2520App.user.js
// @homepage https://gist.github.com/ryuya0124/e6e59ceba8f03bf40f3b5accedc52e44
// ==/UserScript==
const currentPath = window.location.pathname;
const currentSearch = window.location.search;
const timelinePaths = [
'/home',
'/i/communitynotes',
'/i/verified-orgs-signup',
];
const includePaths = ['/list'];
if (currentPath === '/i/timeline' || currentPath.startsWith('/notifications')) {
window.location.href = 'twitter://mentions';
} else if (timelinePaths.some(path => currentPath.startsWith(path)) || includePaths.some(path => currentPath.includes(path))) {
window.location.href = 'twitter://timeline';
} else if (currentPath === '/messages') {
window.location.href = 'twitter://messages';
} else if (currentPath.startsWith('/messages/')) {
const conversationId = currentPath.match(/\/messages\/([^/]+)/)[1];
window.location.href = `twitter://messages?id=${conversationId}`;
} else if (currentPath.match(/\/status\/\d+$/)) {
const statusId = currentPath.match(/\/status\/(\d+)/)[1];
window.location.href = `twitter://status?id=${statusId}`;
} else if (currentPath === '/compose/tweet') {
window.location.href = 'twitter://post?message';
} else if (currentPath === '/explore') {
window.location.href = 'twitter://explore';
} else if (currentPath === '/search' && currentSearch.includes('?q=')) {
const searchTerm = currentSearch.match(/q=(.*?)&/)[1];
window.location.href = `twitter://search?query=${searchTerm}`;
} else if (currentPath.startsWith('/i/bookmarks')) {
window.location.href = 'twitter://bookmarks';
} else if (currentPath.includes('/communities')) {
const communityIdMatch = currentPath.match(/\/communities\/([^/]+)/);
const communityId = communityIdMatch ? communityIdMatch[1] : '';
window.location.href = `twitter://communities/${communityId}`;
} else if (currentPath.startsWith('/')) {
const screenName = currentPath.match(/\/([^/]+)/)[1];
window.location.href = `twitter://user?screen_name=${encodeURIComponent(screenName)}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment