Skip to content

Instantly share code, notes, and snippets.

@mu373
Last active March 18, 2023 16:26
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 mu373/c460f574b421ce4c55d722127dea9bf2 to your computer and use it in GitHub Desktop.
Save mu373/c460f574b421ce4c55d722127dea9bf2 to your computer and use it in GitHub Desktop.
ツイートをScrapboxに貼るためのブックマークレット

ツイートをScrapboxに貼るためのブックマークレット

概要

ツイートページでこのブックマークレットを実行すると、Scrapbox貼り付け用のテキストがクリップボードにコピーされます。 例:

>[https://twitter.com/github/status/1634195404173524993 @github]:
>Our previous in-memory solution for site-search on GitHub Docs couldn't scale with us, so we recently implemented Elasticsearch. Here's how.

経緯

javascript:void%20function(){javascript:(function(){function%20a(){return%20window.location.href}function%20b(){var%20a=document.querySelectorAll(%22[data-testid='tweet'][tabindex='-1']%22)[0],b=a.querySelectorAll(%22[data-testid='User-Name']%20a%22)[0].href,c=%22%40%22+b.replace(/https:\/\/(mobile\.)*twitter\.com\//,%22%22);return%20c}function%20c(a){var%20b=document.querySelectorAll(%22[data-testid='tweet'][tabindex='-1']%22)[0],c=b.querySelectorAll(%22[data-testid='tweetText']%22)[0],d=b.querySelectorAll(%22[data-testid='tweetPhoto']%20img%22),e=%22%22;for(const%20b%20of%20Array.from(c.children))e+=%22IMG%22==b.tagName%3Fb.getAttribute(%22alt%22):b.textContent;return%20d.forEach(b=%3E{e+=%22\n%22;var%20c=b.getAttribute(%22src%22);c=c.replace(%22%3Fformat=%22,%22.%22),c=c.replace(/%26name=.*/,%22%22),%22scrapbox%22==a%3Fe=e+%22[%22+c+%22]%22:e+=c}),e}function%20d(a){var%20b=document.createElement(%22textarea%22);b.textContent=a,document.body.appendChild(b),b.select(),document.execCommand(%22copy%22),document.body.removeChild(b)}(function(){var%20e=b(),f=a(),g=c(%22scrapbox%22),h=%22%22;h=%22[%22+f+%22%20%22+e+%22]:\n%22,h+=g,h=h.replace(/^/gm,%22%3E%22),d(h),console.log(h)})()})()}();
javascript: (
function() {
function getURL() {
return(window.location.href);
}
function getUserName() {
var tweet = document.querySelectorAll("[data-testid='tweet'][tabindex='-1']")[0];
var userNameURL = tweet.querySelectorAll("[data-testid='User-Name'] a")[0].href;
var userName = "@" + userNameURL.replace(/https:\/\/(mobile\.)*twitter\.com\//, "");
return(userName);
}
function getTweetText(style) {
var tweet = document.querySelectorAll("[data-testid='tweet'][tabindex='-1']")[0];
var tweetTextElement = tweet.querySelectorAll("[data-testid='tweetText']")[0];
var tweetPhotoElement = tweet.querySelectorAll("[data-testid='tweetPhoto'] img");
var text = "";
for (const child of Array.from(tweetTextElement.children)) {
if(child.tagName == 'IMG') {
text = text + child.getAttribute("alt");
} else {
text = text + child.textContent;
}
}
tweetPhotoElement.forEach(img => {
text = text + "\n";
var imgURL = img.getAttribute("src");
imgURL = imgURL.replace("?format=",".");
imgURL = imgURL.replace(/&name=.*/,"");
if(style=="scrapbox") {
text = text + "[" + imgURL + "]";
} else {
text = text + imgURL;
}
});
return(text);
}
function copyText(text){
var textarea = document.createElement('textarea');
textarea.textContent = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
}
function generateTweetQuote() {
var userName = getUserName();
var url = getURL();
var tweetText = getTweetText("scrapbox");
var sbText = "";
sbText = "[" + url + " " + userName + "]:\n";
sbText = sbText + tweetText;
sbText = sbText.replace(/^/gm, '>');
copyText(sbText);
console.log(sbText);
}
generateTweetQuote();
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment