Skip to content

Instantly share code, notes, and snippets.

@lowteq
Last active October 9, 2023 09:56
Show Gist options
  • Save lowteq/4ae81327e9ed76cf0a121c52a051840b to your computer and use it in GitHub Desktop.
Save lowteq/4ae81327e9ed76cf0a121c52a051840b to your computer and use it in GitHub Desktop.
エンゲージメントリンクをtwitter(X)のポスト上に表示する
// ==UserScript==
// @name Add Engagement Links to Post on Twitter (X)
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add specific links to a Twitter tweet based on conditions
// @author lowteq
// @match https://twitter.com/*
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
GM_addStyle(`
.custom-twitter-link {
color: rgba(247,249,249,1.00);
padding-left: 30px;
}
`);
// Function to add the links based on the current URL
function addLinks() {
// If current URL is https://twitter.com/home, then exit
if (window.location.href === 'https://twitter.com/home') {
return;
}
const statusPattern = /https:\/\/twitter\.com\/.*\/status\/.*/;
if (statusPattern.test(window.location.href)) {
const firstArticle = document.querySelector('article[data-testid="tweet"]');
if (firstArticle) {
const divContainer = firstArticle.querySelector('div');
if (divContainer && !divContainer.querySelector('.custom-twitter-link')) {
const div = document.createElement('div');
const baseURL = window.location.href;
const endpoints = ['quotes', 'retweets', 'likes'];
endpoints.forEach(endpoint => {
const link = document.createElement('a');
link.href = `${baseURL}/${endpoint}`;
link.textContent = endpoint;
link.classList.add('custom-twitter-link');
div.appendChild(link);
});
divContainer.appendChild(div);
}
}
}
}
// Observe URL changes
const pushState = history.pushState;
history.pushState = function() {
pushState.apply(history, arguments);
addLinks();
};
const replaceState = history.replaceState;
history.replaceState = function() {
replaceState.apply(history, arguments);
addLinks();
};
window.addEventListener('popstate', addLinks);
// Use MutationObserver to handle dynamic content loading
const observer = new MutationObserver(addLinks);
observer.observe(document.body, {
childList: true,
subtree: true
});
// Initial call
addLinks();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment