Skip to content

Instantly share code, notes, and snippets.

@rnjailamba
Created January 2, 2025 14:10
Show Gist options
  • Save rnjailamba/2051219927f7a22ace41d89cf51f9ee6 to your computer and use it in GitHub Desktop.
Save rnjailamba/2051219927f7a22ace41d89cf51f9ee6 to your computer and use it in GitHub Desktop.
Non functional code related to exporting twitter bookmarks, just kept as an archive
//twitter download bookmarks
var before_start, after_finished, before_loop, after_element_clicked;
before_start = 10000;
after_finished = before_start;
before_loop = 10000;
after_element_clicked = before_loop - 1000;
var scroll_count = 500;
var startTime, endTime;
start();
var elementsToResize = ['body', 'html', 'div.application-outlet', 'div.authentication-outlet', 'div.pv5', 'main.grid', 'main.grid section', 'div.scaffold-finite-scroll', 'div.workflow-results-container', 'div.workflow-results-container div:nth-child(2)', 'div.workflow-results-container div:nth-child(2) div' , 'div.workflow-results-container div:nth-child(2), div ul'];
for (var j = 0; j < elementsToResize.length; j++) {
var elementToResize = elementsToResize[j];
document.querySelector(elementToResize).style.setProperty('max-height', '100%', 'important');
document.querySelector(elementToResize).style.setProperty('height', '100%', 'important');
//document.querySelector(elementToResize).style.setProperty('max-width', '100%', 'important');
//document.querySelector(elementToResize).style.setProperty('width', '100%', 'important');
}
//var z = prompt('Set zoom level!');
document.body.style.zoom = parseFloat(1) // Set to 0.1 to get 100, Set to 0.001 to get 1000 and so on
var allBookmarks = [["Name", "Handle", "Date", "Bookmarked Tweet Text", "Link"]];
var allBookmarksAsStringArray = [];
setTimeout(function() {
var index = 0;
function loopThroughBookmarks() {
setTimeout(function() { // call a setTimeout when the loop is called
var divs = document.querySelectorAll('li.reusable-search__result-container')
console.log('Number of bookmarks: ', divs.length);
for (var i = 0; i < divs.length; i++) {
var bookmark = [];
var node = divs[i];
var divVariable = node;
var targetSpan = divVariable.innerText;
var bookmarkedText = targetSpan;
if(allBookmarksAsStringArray.indexOf(bookmarkedText) === -1){
allBookmarksAsStringArray.push(bookmarkedText);
bookmark.push(bookmarkedText);
allBookmarks.push(bookmark);
console.table("BOOKMARKED MESSAGE(INFO) -> " + bookmarkedText);
}
else {
console.log("This item already exists");
}
}
index++;
if (index < scroll_count) {
console.log('looping again ' + index + " " + allBookmarks.length);
window.scrollTo(0, document.body.scrollHeight);
loopThroughBookmarks();
}
else {
setTimeout(() => {
function arrayToCsv(data){
end();
return data.map(row =>
row
.map(String) // convert every value to String
.map(v => v.replaceAll('"', '""')) // escape double quotes
.map(v => `"${v}"`) // quote it
.join(',') // comma-separated
).join('\r\n'); // rows starting on new lines
}
console.log(arrayToCsv(allBookmarks));
}, after_finished);
}
}, before_loop)
}
loopThroughBookmarks();
}, before_start); // Adjust delay as needed
function start() {
startTime = new Date();
};
function end() {
endTime = new Date();
var timeDiff = endTime - startTime; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
console.log(seconds + " seconds");
}
var links = divVariable.querySelectorAll('a');
var hrefValue = "";
for(var j = 0; j < links.length; j++){
var node = links[j];
if(node.toString().includes("t.co")){
//console.log(node);
var hrefMatch = node.outerHTML.match(/href="([^"]*)"/);
if (hrefMatch) {
hrefValue = hrefMatch[1];
hrefValue = " " + hrefValue;
//console.log('href value:', hrefValue);
break;
}
}
}
var tweetURL = "";
for(var j = 0; j < links.length; j++){
var node = links[j];
if(node.toString().includes("/status/")){
//console.log(node);
var hrefMatch = node.outerHTML.match(/href="([^"]*)"/);
if (hrefMatch) {
tweetURL = hrefMatch[1];
//console.log('tweetURL value:', tweetURL);
break;
}
}
}
//console.log(i);
//console.log(targetSpan);
//scroll to bottom
if(allBookmarksAsStringArray.indexOf(bookmarkedText) === -1){
allBookmarksAsStringArray.push(bookmarkedText);
// seperate and clean text
for(var j = 0; j < 4; j++){
if(bookmarkedText.slice(-1) == 'K' || bookmarkedText.slice(-1) == 'M' || bookmarkedText.slice(-1) == 'B'){
bookmarkedText = bookmarkedText.substring(0, bookmarkedText.length - 1);
}
if(bookmarkedText.lastIndexOf("\n")>0) {
//console.log(bookmarkedText.substring(bookmarkedText.lastIndexOf("\n")));
if(!isNaN(bookmarkedText.substring(bookmarkedText.lastIndexOf("\n"))))
bookmarkedText = bookmarkedText.substring(0, bookmarkedText.lastIndexOf("\n"));
}
}
//console.log("last char " + bookmarkedText.slice(-1));
if(bookmarkedText.slice(-1) == '…'){
bookmarkedText = bookmarkedText.substring(0, bookmarkedText.length - 1);
}
var name = bookmarkedText.substr(0, bookmarkedText.indexOf("\n"));
bookmarkedText = bookmarkedText.substr(bookmarkedText.indexOf("\n")+1);
//console.log(name);
var handle = bookmarkedText.substr(0, bookmarkedText.indexOf("\n"));
bookmarkedText = bookmarkedText.substr(bookmarkedText.indexOf("\n")+1);
//console.log(handle);
var third_line = bookmarkedText.substr(0, bookmarkedText.indexOf("\n"));
bookmarkedText = bookmarkedText.substr(bookmarkedText.indexOf("\n")+1);
var date = bookmarkedText.substr(0, bookmarkedText.indexOf("\n"));
bookmarkedText = bookmarkedText.substr(bookmarkedText.indexOf("\n")+1);
//console.log(date);
bookmarkedText = bookmarkedText.replaceAll('…', '');
bookmarkedText = bookmarkedText + hrefValue;
bookmark.push(name);
bookmark.push("https://x.com/" + handle);
bookmark.push(date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment