From 766e3225be31108d2f48d1332456c3081b39838a Mon Sep 17 00:00:00 2001 From: Wesley Kerfoot Date: Fri, 7 Feb 2020 22:54:23 -0500 Subject: [PATCH] add a bookmarklet instead --- bookmarklet.js | 34 ++++++++++++ minified_bookmarklet.js | 1 + userscript.js | 114 ---------------------------------------- 3 files changed, 35 insertions(+), 114 deletions(-) create mode 100644 bookmarklet.js create mode 100644 minified_bookmarklet.js delete mode 100644 userscript.js diff --git a/bookmarklet.js b/bookmarklet.js new file mode 100644 index 0000000..56f8604 --- /dev/null +++ b/bookmarklet.js @@ -0,0 +1,34 @@ +(function() { + function getLastXPath(xpexp) { + var iterator = document.evaluate(xpexp, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); + var result = iterator.iterateNext(); + var lastEl = result; + + while(result != null) { + result = iterator.iterateNext(); + if (result == null) { + break; + } + lastEl = result; + } + return lastEl; + } + + function getLastTweet() { + var author = window.location.pathname.split('/')[1]; + var xp = "//a[contains(@href, '"+author+"/status')]"; + + return getLastXPath(xp).pathname; + } + + function queueTweet() { + /* TODO check the current URL */ + /* TODO check the last tweet URL is valid, skip likes/images */ + var lastTweet = getLastTweet(); + var url = "http://localhost:8080/thread"+lastTweet; + window.open(url, "_blank"); + } + + queueTweet(); + +})() diff --git a/minified_bookmarklet.js b/minified_bookmarklet.js new file mode 100644 index 0000000..39529d4 --- /dev/null +++ b/minified_bookmarklet.js @@ -0,0 +1 @@ +javascript:(function(){function getLastXPath(xpexp){var iterator=document.evaluate(xpexp,document,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);var result=iterator.iterateNext();var lastEl=result;while(result!=null){result=iterator.iterateNext();if(result==null){break}lastEl=result}return lastEl}function getLastTweet(){var author=window.location.pathname.split("/")[1];var xp="//a[contains(@href, '"+author+"/status')]";return getLastXPath(xp).pathname}function queueTweet(){var lastTweet=getLastTweet();var url="http://localhost:8080/thread"+lastTweet;window.open(url,"_blank")}queueTweet()})(); diff --git a/userscript.js b/userscript.js deleted file mode 100644 index d803b27..0000000 --- a/userscript.js +++ /dev/null @@ -1,114 +0,0 @@ -// ==UserScript== -// @name Twitter Archiver -// @version 1 -// @grant none -// @namespace https://twitter.com -// @include https://twitter.com/* -// ==/UserScript== - - -// TODO guard against invalid last tweets (photos?) -// TODO "2 more replies" issue (maybe not a real issue?) auto-expand them somehow? -// TODO only run it on pages with actual threads - -var intervals = {} - -function getLastXPath(xpexp) { - let iterator = document.evaluate(xpexp, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); - - var result = iterator.iterateNext(); - var lastEl = result; - - while(result != null) { - result = iterator.iterateNext(); - - if (result == null) { - break; - } - lastEl = result; - } - return lastEl; -} - -function getLastTweet() { - let author = window.location.pathname.split('/')[1]; - let xp = `//a[contains(@href, '${author}/status')]`; - - return getLastXPath(xp).href; -} - -function getTopTweet() { - let xp = "//span[text()='Thread']"; - return getLastXPath(xp); -} - -function appendButton() { - if (!window.location.pathname.includes("/status")) { - clearInterval(intervals["appendButton"]) - return; - } - - var buttonEl = document.getElementById("twitblog"); - - if (buttonEl) { - console.log(buttonEl); - clearInterval(intervals["appendButton"]) - return; - } - - console.log("Trying to append the button"); - let topTweet = getTopTweet(); - - if (!topTweet) { - // TODO needs to work on all pages with "status" in it - return; - } - - let button = document.createElement("button"); - button.textContent = "Save Tweet"; - button.id = "twitblog"; - - button.addEventListener("click", ev => { alert(getLastTweet()); }); - topTweet.appendChild(button); - - buttonAdded = true; - clearInterval(intervals["appendButton"]) -} - -window.addEventListener('DOMContentLoaded', (event) => { - console.log('DOM fully loaded and parsed'); - - /* https://stackoverflow.com/questions/6390341/how-to-detect-url-change-in-javascript */ - history.pushState = ( f => function pushState() { - var ret = f.apply(this, arguments); - window.dispatchEvent(new Event('pushstate')); - window.dispatchEvent(new Event('locationchange')); - return ret; - })(history.pushState); - - history.replaceState = ( f => function replaceState() { - var ret = f.apply(this, arguments); - window.dispatchEvent(new Event('replacestate')); - window.dispatchEvent(new Event('locationchange')); - return ret; - })(history.replaceState); - - window.addEventListener('popstate',() => { - window.dispatchEvent(new Event('locationchange')) - }); - - window.addEventListener('locationchange', function() { - console.log("Location changed"); - if (window.location.pathname.includes("/status")) { - console.log('location changed!'); - buttonAdded = false; - intervals["appendButton"] = window.setInterval(appendButton, 5*1000); - } - else { - console.log("Not going to try and add the button"); - buttonAdded = true - clearInterval(intervals["appendButton"]); - } - }) - window.setTimeout(appendButton, 5*1000); -});