Twitter Click'n'Save

Add buttons to download images and videos in Twitter, also does some other enhancements.

当前为 2022-08-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Click'n'Save
  3. // @version 0.8.0-2022.08.26
  4. // @namespace gh.alttiri
  5. // @description Add buttons to download images and videos in Twitter, also does some other enhancements.
  6. // @match https://twitter.com/*
  7. // @homepageURL https://github.com/AlttiRi/twitter-click-and-save
  8. // @supportURL https://github.com/AlttiRi/twitter-click-and-save/issues
  9. // @license GPL-3.0
  10. // @grant GM_registerMenuCommand
  11. // ==/UserScript==
  12. // ---------------------------------------------------------------------------------------------------------------------
  13. // ---------------------------------------------------------------------------------------------------------------------
  14.  
  15. if (globalThis.GM_registerMenuCommand /* undefined in Firefox with VM */ || typeof GM_registerMenuCommand === "function") {
  16. GM_registerMenuCommand("Show settings", showSettings);
  17. }
  18.  
  19. // --- For debug --- //
  20. const verbose = false;
  21.  
  22.  
  23. const settings = loadSettings();
  24.  
  25. function loadSettings() {
  26. const defaultSettings = {
  27. hideTrends: true,
  28. hideSignUpSection: true,
  29. hideTopicsToFollow: false,
  30. hideTopicsToFollowInstantly: false,
  31. hideSignUpBottomBarAndMessages: true,
  32. doNotPlayVideosAutomatically: false,
  33. goFromMobileToMainSite: false,
  34.  
  35. highlightVisitedLinks: true,
  36. expandSpoilers: true,
  37.  
  38. directLinks: true,
  39. handleTitle: true,
  40.  
  41. imagesHandler: true,
  42. videoHandler: true,
  43. addRequiredCSS: true,
  44. preventBlinking: true,
  45. hideLoginPopup: false,
  46. addBorder: false,
  47. };
  48.  
  49. let savedSettings;
  50. try {
  51. savedSettings = JSON.parse(localStorage.getItem("ujs-click-n-save-settings")) || {};
  52. } catch (e) {
  53. console.error("[ujs]", e);
  54. localStorage.removeItem("ujs-click-n-save-settings");
  55. savedSettings = {};
  56. }
  57. savedSettings = Object.assign(defaultSettings, savedSettings);
  58. return savedSettings;
  59. }
  60. function showSettings() {
  61. closeSetting();
  62. if (window.scrollY > 0) {
  63. document.querySelector("html").classList.add("ujs-scroll-initial");
  64. document.body.classList.add("ujs-scrollbar-width-margin-right");
  65. }
  66. document.body.classList.add("ujs-no-scroll");
  67.  
  68. const modalWrapperStyle = `
  69. width: 100%;
  70. height: 100%;
  71. position: fixed;
  72. display: flex;
  73. justify-content: center;
  74. align-items: center;
  75. z-index: 99999;
  76. backdrop-filter: blur(4px);
  77. background-color: rgba(255, 255, 255, 0.5);
  78. `;
  79. const modalSettingsStyle = `
  80. background-color: white;
  81. min-width: 320px;
  82. min-height: 320px;
  83. border: 1px solid darkgray;
  84. padding: 8px;
  85. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  86. `;
  87. const s = settings;
  88. document.body.insertAdjacentHTML("afterbegin", `
  89. <div class="ujs-modal-wrapper" style="${modalWrapperStyle}">
  90. <div class="ujs-modal-settings" style="${modalSettingsStyle}">
  91. <fieldset>
  92. <legend>Optional</legend>
  93. <label><input type="checkbox" ${s.hideTrends ? "checked" : ""} name="hideTrends">Hide <b>Trends</b> (in the right column)*<br/></label>
  94. <label><input type="checkbox" ${s.hideSignUpSection ? "checked" : ""} name="hideSignUpSection">Hide <b title='"New to Twitter?" (If yoy are not logged in)'>Sign Up</b> section (in the right column)*<br/></label>
  95. <label><input type="checkbox" ${s.hideSignUpBottomBarAndMessages ? "checked" : ""} name="hideSignUpBottomBarAndMessages">Hide <b>Sign Up Bar</b> and <b>Messages</b> (in the bottom)<br/></label>
  96. <label hidden><input type="checkbox" ${s.doNotPlayVideosAutomatically ? "checked" : ""} name="doNotPlayVideosAutomatically">Do <i>Not</i> Play Videos Automatically</b><br/></label>
  97. <label hidden><input type="checkbox" ${s.goFromMobileToMainSite ? "checked" : ""} name="goFromMobileToMainSite">Redirect from Mobile version (beta)<br/></label>
  98. <label title="Makes the button more visible"><input type="checkbox" ${s.addBorder ? "checked" : ""} name="addBorder">Add a white border to the download button<br/></label>
  99. <label title="Hides the modal login pop up. Useful if you have no account. \nWARNING: Currently it will close any popup, not only the login one."><input type="checkbox" ${s.hideLoginPopup ? "checked" : ""} name="hideLoginPopup">Hide <strike>Login</strike> Popups (beta)<br/></label>
  100. </fieldset>
  101. <fieldset>
  102. <legend>Recommended</legend>
  103. <label><input type="checkbox" ${s.highlightVisitedLinks ? "checked" : ""} name="highlightVisitedLinks">Highlight Visited Links<br/></label>
  104. <label title="Note: since the recent update the most NSFW spoilers are impossible to expand without an account"><input type="checkbox" ${s.expandSpoilers ? "checked" : ""} name="expandSpoilers">Expand Spoilers (if possible)*<br/></label>
  105. </fieldset>
  106. <fieldset>
  107. <legend>Highly Recommended</legend>
  108. <label><input type="checkbox" ${s.directLinks ? "checked" : ""} name="directLinks">Direct Links</label><br/>
  109. <label><input type="checkbox" ${s.handleTitle ? "checked" : ""} name="handleTitle">Enchance Title*<br/></label>
  110. </fieldset>
  111. <fieldset>
  112. <legend>Main</legend>
  113. <label><input type="checkbox" ${s.imagesHandler ? "checked" : ""} name="imagesHandler">Image Download Button<br/></label>
  114. <label><input type="checkbox" ${s.videoHandler ? "checked" : ""} name="videoHandler">Video Download Button<br/></label>
  115. <label title="Prevent the tweet backgroubd blinking on the button click"><input type="checkbox" ${s.preventBlinking ? "checked" : ""} name="preventBlinking">Prevent blinking on click<br/></label>
  116. <label hidden><input type="checkbox" ${s.addRequiredCSS ? "checked" : ""} name="addRequiredCSS">Add Required CSS*<br/></label><!-- * Only for the image download button in /photo/1 mode -->
  117. </fieldset>
  118. <fieldset>
  119. <legend title="Outdated due to Twitter's updates, impossible to reimplement">Outdated</legend>
  120. <strike>
  121. <label><input type="checkbox" ${s.hideTopicsToFollow ? "checked" : ""} name="hideTopicsToFollow">Hide <b>Topics To Follow</b> (in the right column)*<br/></label>
  122. <label hidden><input type="checkbox" ${s.hideTopicsToFollowInstantly ? "checked" : ""} name="hideTopicsToFollowInstantly">Hide <b>Topics To Follow</b> Instantly*<br/></label>
  123. </strike>
  124. </fieldset>
  125. <hr>
  126. <div style="display: flex; justify-content: space-around;">
  127. <button class="ujs-save-setting-button" style="padding: 5px">Save Settings</button>
  128. <button class="ujs-close-setting-button" style="padding: 5px">Close Settings</button>
  129. </div>
  130. <hr>
  131. <h4 style="margin: 0; padding-left: 8px; color: #444;">Notes:</h4>
  132. <ul style="margin: 2px; padding-left: 16px; color: #444;">
  133. <li>Click on <b>Save Settings</b> and reload the page to apply changes.</li>
  134. <li><b>*</b>-marked settings are language dependent. Currently, the follow languages are supported:<br/> "en", "ru", "es", "zh", "ja".</li>
  135. <li hidden>The extension downloads only from twitter.com, not from <b>mobile</b>.twitter.com</li>
  136. </ul>
  137. </div>
  138. </div>`);
  139.  
  140. document.querySelector("body > .ujs-modal-wrapper .ujs-save-setting-button").addEventListener("click", saveSetting);
  141. document.querySelector("body > .ujs-modal-wrapper .ujs-close-setting-button").addEventListener("click", closeSetting);
  142.  
  143. function saveSetting() {
  144. const entries = [...document.querySelectorAll("body > .ujs-modal-wrapper input[type=checkbox]")]
  145. .map(checkbox => [checkbox.name, checkbox.checked]);
  146. const settings = Object.fromEntries(entries);
  147. settings.hideTopicsToFollowInstantly = settings.hideTopicsToFollow;
  148. // console.log("[ujs]", settings);
  149. localStorage.setItem("ujs-click-n-save-settings", JSON.stringify(settings));
  150. }
  151.  
  152. function closeSetting() {
  153. document.body.classList.remove("ujs-no-scroll");
  154. document.body.classList.remove("ujs-scrollbar-width-margin-right");
  155. document.querySelector("html").classList.remove("ujs-scroll-initial");
  156. document.querySelector("body > .ujs-modal-wrapper")?.remove();
  157. }
  158. }
  159.  
  160.  
  161.  
  162. // ---------------------------------------------------------------------------------------------------------------------
  163. // ---------------------------------------------------------------------------------------------------------------------
  164.  
  165. // --- Features to execute --- //
  166. const doNotPlayVideosAutomatically = false;
  167.  
  168. function execFeaturesOnce() {
  169. settings.goFromMobileToMainSite && Features.goFromMobileToMainSite();
  170. settings.addRequiredCSS && Features.addRequiredCSS();
  171. settings.hideSignUpBottomBarAndMessages && Features.hideSignUpBottomBarAndMessages(doNotPlayVideosAutomatically);
  172. settings.hideTrends && Features.hideTrends();
  173. settings.highlightVisitedLinks && Features.highlightVisitedLinks();
  174. settings.hideTopicsToFollowInstantly && Features.hideTopicsToFollowInstantly();
  175. settings.hideLoginPopup && Features.hideLoginPopup();
  176. }
  177. function execFeaturesImmediately() {
  178. settings.expandSpoilers && Features.expandSpoilers();
  179. }
  180. function execFeatures() {
  181. settings.imagesHandler && Features.imagesHandler(settings.preventBlinking);
  182. settings.videoHandler && Features.videoHandler(settings.preventBlinking);
  183. settings.expandSpoilers && Features.expandSpoilers();
  184. settings.hideSignUpSection && Features.hideSignUpSection();
  185. settings.hideTopicsToFollow && Features.hideTopicsToFollow();
  186. settings.directLinks && Features.directLinks();
  187. settings.handleTitle && Features.handleTitle();
  188. }
  189.  
  190. // ---------------------------------------------------------------------------------------------------------------------
  191. // ---------------------------------------------------------------------------------------------------------------------
  192.  
  193. if (verbose) {
  194. console.log("[ujs][settings]", settings);
  195. // showSettings();
  196. }
  197.  
  198. // --- [VM/GM + Firefox ~90+ + Enabled "Strict Tracking Protection"] fix --- //
  199. const fetch = (globalThis.wrappedJSObject && typeof globalThis.wrappedJSObject.fetch === "function") ? function(resource, init = {}) {
  200. verbose && console.log("wrappedJSObject.fetch", resource, init);
  201.  
  202. if (init.headers instanceof Headers) {
  203. // Since `Headers` are not allowed for structured cloning.
  204. init.headers = Object.fromEntries(init.headers.entries());
  205. }
  206.  
  207. return globalThis.wrappedJSObject.fetch(cloneInto(resource, document), cloneInto(init, document));
  208. } : globalThis.fetch;
  209.  
  210.  
  211. // --- "Imports" --- //
  212. const {
  213. sleep, fetchResource, download,
  214. addCSS,
  215. getCookie,
  216. throttle,
  217. xpath, xpathAll,
  218. } = getUtils({verbose});
  219. const LS = hoistLS({verbose});
  220.  
  221. const API = hoistAPI();
  222. const Tweet = hoistTweet();
  223. const Features = hoistFeatures();
  224. const I18N = getLanguageConstants();
  225.  
  226.  
  227. // --- That to use for the image history --- //
  228. // "TWEET_ID" or "IMAGE_NAME"
  229. const imagesHistoryBy = LS.getItem("ujs-images-history-by", "IMAGE_NAME");
  230. // With "TWEET_ID" downloading of 1 image of 4 will mark all 4 images as "already downloaded"
  231. // on the next time when the tweet will appear.
  232. // "IMAGE_NAME" will count each image of a tweet, but it will take more data to store.
  233.  
  234.  
  235. // ---------------------------------------------------------------------------------------------------------------------
  236. // ---------------------------------------------------------------------------------------------------------------------
  237. // --- Script runner --- //
  238.  
  239. (function starter(feats) {
  240. const {once, onChangeImmediate, onChange} = feats;
  241.  
  242. once();
  243. onChangeImmediate();
  244. const onChangeThrottled = throttle(onChange, 250);
  245. onChangeThrottled();
  246.  
  247. const targetNode = document.querySelector("body");
  248. const observerOptions = {
  249. subtree: true,
  250. childList: true,
  251. };
  252. const observer = new MutationObserver(callback);
  253. observer.observe(targetNode, observerOptions);
  254.  
  255. function callback(mutationList, observer) {
  256. verbose && console.log(mutationList);
  257. onChangeImmediate();
  258. onChangeThrottled();
  259. }
  260. })({
  261. once: execFeaturesOnce,
  262. onChangeImmediate: execFeaturesImmediately,
  263. onChange: execFeatures
  264. });
  265.  
  266. // ---------------------------------------------------------------------------------------------------------------------
  267. // ---------------------------------------------------------------------------------------------------------------------
  268. // --- Twitter Specific code --- //
  269.  
  270.  
  271. const downloadedImages = new LS("ujs-twitter-downloaded-images-names");
  272. const downloadedImageTweetIds = new LS("ujs-twitter-downloaded-image-tweet-ids");
  273. const downloadedVideoTweetIds = new LS("ujs-twitter-downloaded-video-tweet-ids");
  274.  
  275. // --- Twitter.Features --- //
  276. function hoistFeatures() {
  277. class Features {
  278. static goFromMobileToMainSite() {
  279. if (location.href.startsWith("https://mobile.twitter.com/")) {
  280. location.href = location.href.replace("https://mobile.twitter.com/", "https://twitter.com/");
  281. }
  282. // TODO: add #redirected, remove by timer // to prevent a potential infinity loop
  283. }
  284. static _ImageHistory = class {
  285. static getImageNameFromUrl(url) {
  286. const _url = new URL(url);
  287. const {filename} = (_url.origin + _url.pathname).match(/(?<filename>[^\/]+$)/).groups;
  288. return filename.match(/^[^\.]+/)[0]; // remove extension
  289. }
  290. static isDownloaded({id, url}) {
  291. if (imagesHistoryBy === "TWEET_ID") {
  292. return downloadedImageTweetIds.hasItem(id);
  293. } else if (imagesHistoryBy === "IMAGE_NAME") {
  294. const name = Features._ImageHistory.getImageNameFromUrl(url);
  295. return downloadedImages.hasItem(name);
  296. }
  297. }
  298. static async markDownloaded({id, url}) {
  299. if (imagesHistoryBy === "TWEET_ID") {
  300. await downloadedImageTweetIds.pushItem(id);
  301. } else if (imagesHistoryBy === "IMAGE_NAME") {
  302. const name = Features._ImageHistory.getImageNameFromUrl(url);
  303. await downloadedImages.pushItem(name);
  304. }
  305. }
  306. }
  307. static async imagesHandler(preventBlinking) {
  308. verbose && console.log("[ujs-cns][imagesHandler]");
  309. const images = document.querySelectorAll("img");
  310. for (const img of images) {
  311.  
  312. if (img.width < 150 || img.dataset.handled) {
  313. continue;
  314. }
  315. verbose && console.log(img, img.width);
  316.  
  317. img.dataset.handled = "true";
  318.  
  319. const btn = document.createElement("div");
  320. btn.classList.add("ujs-btn-download");
  321. btn.dataset.url = img.src;
  322.  
  323. btn.addEventListener("click", Features._imageClickHandler);
  324.  
  325. let anchor = img.closest("a");
  326. // if an image is _opened_ "https://twitter.com/UserName/status/1234567890123456789/photo/1" [fake-url]
  327. if (!anchor) {
  328. anchor = img.parentNode;
  329. }
  330. anchor.append(btn);
  331. if (preventBlinking) {
  332. Features._preventBlinking(btn);
  333. }
  334.  
  335. const downloaded = Features._ImageHistory.isDownloaded({
  336. id: Tweet.of(btn).id,
  337. url: btn.dataset.url
  338. });
  339. if (downloaded) {
  340. btn.classList.add("ujs-already-downloaded");
  341. }
  342. }
  343. }
  344. static hasBlinkListenerWeakSet;
  345. static _preventBlinking(clickBtnElem) {
  346. const weakSet = Features.hasBlinkListenerWeakSet || (Features.hasBlinkListenerWeakSet = new WeakSet());
  347. let wrapper;
  348. clickBtnElem.addEventListener("mouseenter", () => {
  349. if (!weakSet.has(clickBtnElem)) {
  350. wrapper = Features._preventBlinkingHandler(clickBtnElem);
  351. weakSet.add(clickBtnElem);
  352. }
  353. });
  354. clickBtnElem.addEventListener("mouseleave", () => {
  355. verbose && console.log("[ujs] Btn mouseleave");
  356. if (wrapper?.observer?.disconnect) {
  357. weakSet.delete(clickBtnElem);
  358. wrapper.observer.disconnect();
  359. }
  360. });
  361. }
  362. static _preventBlinkingHandler(clickBtnElem) {
  363. let targetNode = clickBtnElem.closest("[aria-labelledby]");
  364. if (!targetNode) {
  365. return;
  366. }
  367. let config = {attributes: true, subtree: true, attributeOldValue: true};
  368. const wrapper = {};
  369. wrapper.observer = new MutationObserver(callback);
  370. wrapper.observer.observe(targetNode, config);
  371. function callback(mutationsList, observer) {
  372. for (const mutation of mutationsList) {
  373. if (mutation.type === "attributes" && mutation.attributeName === "class") {
  374. if (mutation.target.classList.contains("ujs-btn-download")) {
  375. return;
  376. }
  377. // Don't allow to change classList
  378. mutation.target.className = mutation.oldValue;
  379. // Recreate, to prevent an infinity loop
  380. wrapper.observer.disconnect();
  381. wrapper.observer = new MutationObserver(callback);
  382. wrapper.observer.observe(targetNode, config);
  383. }
  384. }
  385. }
  386. return wrapper;
  387. }
  388. // Banner/Backgroud
  389. static async _downloadBanner(url, btn) {
  390. const username = location.pathname.slice(1).split("/")[0];
  391. btn.classList.add("ujs-downloading");
  392. // https://pbs.twimg.com/profile_banners/34743251/1596331248/1500x500
  393. const {id, seconds, res} = url.match(/(?<=\/profile_banners\/)(?<id>\d+)\/(?<seconds>\d+)\/(?<res>\d+x\d+)/)?.groups || {};
  394. const {blob, lastModifiedDate, extension, name} = await fetchResource(url);
  395. const filename = `[twitter][bg] ${username}—${lastModifiedDate}—${id}—${seconds}.${extension}`;
  396. download(blob, filename, url);
  397. btn.classList.remove("ujs-downloading");
  398. btn.classList.add("ujs-downloaded");
  399. }
  400. static async _imageClickHandler(event) {
  401. event.preventDefault();
  402. event.stopImmediatePropagation();
  403.  
  404. const btn = event.currentTarget;
  405. let url = btn.dataset.url;
  406. const isBanner = url.includes("/profile_banners/");
  407. if (isBanner) {
  408. return Features._downloadBanner(url, btn);
  409. }
  410. url = handleImgUrl(url);
  411. verbose && console.log(url);
  412.  
  413. function handleImgUrl(url) {
  414. const urlObj = new URL(url);
  415. urlObj.searchParams.set("name", "orig");
  416. return urlObj.toString();
  417. }
  418.  
  419. const {id, author} = Tweet.of(btn);
  420. verbose && console.log(id, author);
  421.  
  422. async function safeFetchResource(url) {
  423. let fallbackUsed = false;
  424. retry:
  425. while (true) {
  426. try {
  427. return await fetchResource(url);
  428. } catch (e) {
  429. if (fallbackUsed) {
  430. throw "Fallback URL failed";
  431. }
  432. const _url = new URL(url);
  433. _url.searchParams.set("name", "4096x4096");
  434. url = _url.href;
  435. verbose && console.warn("[safeFetchResource] Fallback URL:", url);
  436. fallbackUsed = true;
  437. continue retry;
  438. }
  439. }
  440.  
  441. }
  442.  
  443. btn.classList.add("ujs-downloading");
  444. const {blob, lastModifiedDate, extension, name} = await safeFetchResource(url);
  445.  
  446. const filename = `[twitter] ${author}—${lastModifiedDate}—${id}—${name}.${extension}`;
  447. download(blob, filename, url);
  448.  
  449. const downloaded = btn.classList.contains("already-downloaded");
  450. if (!downloaded) {
  451. await Features._ImageHistory.markDownloaded({id, url});
  452. }
  453. btn.classList.remove("ujs-downloading");
  454. btn.classList.add("ujs-downloaded");
  455. }
  456.  
  457.  
  458. static async videoHandler(preventBlinking) {
  459. const videos = document.querySelectorAll("video");
  460.  
  461. for (const vid of videos) {
  462. if (vid.dataset.handled) {
  463. continue;
  464. }
  465. verbose && console.log(vid);
  466. vid.dataset.handled = "true";
  467.  
  468. const btn = document.createElement("div");
  469. btn.classList.add("ujs-btn-download");
  470. btn.classList.add("ujs-video");
  471. btn.addEventListener("click", Features._videoClickHandler);
  472.  
  473. let elem = vid.parentNode.parentNode.parentNode;
  474. elem.after(btn);
  475. if (preventBlinking) {
  476. Features._preventBlinking(btn);
  477. }
  478.  
  479. const id = Tweet.of(btn).id;
  480. const downloaded = downloadedVideoTweetIds.hasItem(id);
  481. if (downloaded) {
  482. btn.classList.add("ujs-already-downloaded");
  483. }
  484. }
  485. }
  486. static async _videoClickHandler(event) {
  487. event.preventDefault();
  488. event.stopImmediatePropagation();
  489.  
  490. const btn = event.currentTarget;
  491. const {id, author} = Tweet.of(btn);
  492. const video = await API.getVideoInfo(id); // {bitrate, content_type, url}
  493. verbose && console.log(video);
  494.  
  495. btn.classList.add("ujs-downloading");
  496. const url = video.url;
  497. const {blob, lastModifiedDate, extension, name} = await fetchResource(url);
  498.  
  499. const filename = `[twitter] ${author}—${lastModifiedDate}—${id}—${name}.${extension}`;
  500. download(blob, filename, url);
  501.  
  502. const downloaded = btn.classList.contains("ujs-already-downloaded");
  503. if (!downloaded) {
  504. await downloadedVideoTweetIds.pushItem(id);
  505. }
  506. btn.classList.remove("ujs-downloading");
  507. btn.classList.add("ujs-downloaded");
  508. }
  509.  
  510.  
  511. static addRequiredCSS() {
  512. addCSS(getUserScriptCSS());
  513. }
  514.  
  515. // it depends of `directLinks()` use only it after `directLinks()`
  516. static handleTitle(title) {
  517. if (!I18N.QUOTES) { // Unsupported lang, no QUOTES, ON_TWITTER, TWITTER constants
  518. return;
  519. }
  520. // if not a opened tweet
  521. if (!location.href.match(/twitter\.com\/[^\/]+\/status\/\d+/)) {
  522. return;
  523. }
  524.  
  525. let titleText = title || document.title;
  526. if (titleText === Features.lastHandledTitle) {
  527. return;
  528. }
  529. Features.originalTitle = titleText;
  530.  
  531. const [OPEN_QUOTE, CLOSE_QUOTE] = I18N.QUOTES;
  532. const urlsToReplace = [
  533. ...titleText.matchAll(new RegExp(`https:\\/\\/t\\.co\\/[^ ${CLOSE_QUOTE}]+`, "g"))
  534. ].map(el => el[0]);
  535. // the last one may be the URL to the tweet // or to an embedded shared URL
  536.  
  537. const map = new Map();
  538. const anchors = document.querySelectorAll(`a[data-redirect^="https://t.co/"]`);
  539. for (const anchor of anchors) {
  540. if (urlsToReplace.includes(anchor.dataset.redirect)) {
  541. map.set(anchor.dataset.redirect, anchor.href);
  542. }
  543. }
  544.  
  545. const lastUrl = urlsToReplace.slice(-1)[0];
  546. let lastUrlIsAttachment = false;
  547. let attachmentDescription = "";
  548. if (!map.has(lastUrl)) {
  549. const a = document.querySelector(`a[href="${lastUrl}?amp=1"]`);
  550. if (a) {
  551. lastUrlIsAttachment = true;
  552. attachmentDescription = document.querySelectorAll(`a[href="${lastUrl}?amp=1"]`)[1].innerText;
  553. attachmentDescription = attachmentDescription.replaceAll("\n", " — ");
  554. }
  555. }
  556.  
  557.  
  558. for (const [key, value] of map.entries()) {
  559. titleText = titleText.replaceAll(key, value + ` (${key})`);
  560. }
  561.  
  562. titleText = titleText.replace(new RegExp(`${I18N.ON_TWITTER}(?= ${OPEN_QUOTE})`), ":");
  563. titleText = titleText.replace(new RegExp(`(?<=${CLOSE_QUOTE}) \\\/ ${I18N.TWITTER}$`), "");
  564. if (!lastUrlIsAttachment) {
  565. const regExp = new RegExp(`(?<short> https:\\/\\/t\\.co\\/.{6,14})${CLOSE_QUOTE}$`);
  566. titleText = titleText.replace(regExp, (match, p1, p2, offset, string) => `${CLOSE_QUOTE} ${p1}`);
  567. } else {
  568. titleText = titleText.replace(lastUrl, `${lastUrl} (${attachmentDescription})`);
  569. }
  570. document.title = titleText; // Note: some characters will be removed automatically (`\n`, extra spaces)
  571. Features.lastHandledTitle = document.title;
  572. }
  573. static lastHandledTitle = "";
  574. static originalTitle = "";
  575.  
  576. static profileUrlCache = new Map();
  577. static async directLinks() {
  578. verbose && console.log("[ujs][directLinks]");
  579. const hasHttp = url => Boolean(url.match(/^https?:\/\//));
  580. const anchors = xpathAll(`.//a[@dir="ltr" and child::span and not(@data-handled)]`);
  581. for (const anchor of anchors) {
  582. const redirectUrl = new URL(anchor.href);
  583. const shortUrl = redirectUrl.origin + redirectUrl.pathname; // remove "?amp=1"
  584. anchor.dataset.redirect = shortUrl;
  585. anchor.dataset.handled = "true";
  586. anchor.rel = "nofollow noopener noreferrer";
  587.  
  588. if (Features.profileUrlCache.has(shortUrl)) {
  589. anchor.href = Features.profileUrlCache.get(shortUrl);
  590. continue;
  591. }
  592.  
  593. const nodes = xpathAll(`./span[text() != "…"]|./text()`, anchor);
  594. let url = nodes.map(node => node.textContent).join("");
  595. const doubleProtocolPrefix = url.match(/(?<dup>^https?:\/\/)(?=https?:)/)?.groups.dup;
  596. if (doubleProtocolPrefix) {
  597. url = url.slice(doubleProtocolPrefix.length);
  598. const span = anchor.querySelector(`[aria-hidden="true"]`);
  599. if (hasHttp(span.textContent)) { // Fix Twitter's bug related to text copying
  600. span.style = "display: none;";
  601. }
  602. }
  603. anchor.href = url;
  604. if (anchor.dataset?.testid === "UserUrl") {
  605. const href = anchor.getAttribute("href");
  606. const profileUrl = hasHttp(href) ? href : "https://" + href;
  607. anchor.href = profileUrl;
  608. verbose && console.log("[ujs][directLinks][UserUrl]", profileUrl);
  609. // Restore if URL's text content is too long
  610. if (anchor.textContent.endsWith("…")) {
  611. anchor.href = shortUrl;
  612. try {
  613. const author = location.pathname.slice(1).match(/[^\/]+/)[0];
  614. const expanded_url = await API.getUserInfo(author); // todo: make lazy
  615. anchor.href = expanded_url;
  616. Features.profileUrlCache.set(shortUrl, expanded_url);
  617. } catch (e) {
  618. verbose && console.error(e);
  619. }
  620. }
  621. }
  622. }
  623. if (anchors.length) {
  624. Features.handleTitle(Features.originalTitle);
  625. }
  626. }
  627.  
  628. // Do NOT throttle it
  629. static expandSpoilers() {
  630. const main = document.querySelector("main[role=main]");
  631. if (!main) {
  632. return;
  633. }
  634. if (!I18N.YES_VIEW_PROFILE) { // Unsupported lang, no YES_VIEW_PROFILE, SHOW_NUDITY, VIEW constants
  635. return;
  636. }
  637.  
  638. const a = main.querySelectorAll("[data-testid=primaryColumn] [role=button]");
  639. if (a) {
  640. const elems = [...a];
  641. const button = elems.find(el => el.textContent === I18N.YES_VIEW_PROFILE);
  642. if (button) {
  643. button.click();
  644. }
  645. // "Content warning: Nudity"
  646. // "The Tweet author flagged this Tweet as showing sensitive content.""
  647. // "Show"
  648. const buttonShow = elems.find(el => el.textContent === I18N.SHOW_NUDITY);
  649. if (buttonShow) {
  650. //const verifing = a.previousSibling.textContent.includes("Nudity"); // todo?
  651. //if (verifing) {
  652. buttonShow.click();
  653. //}
  654. }
  655. }
  656. // todo: expand spoiler commentary in photo view mode (.../photo/1)
  657. const b = main.querySelectorAll("article [role=presentation] div[role=button]");
  658. if (b) {
  659. const elems = [...b];
  660. const buttons = elems.filter(el => el.textContent === I18N.VIEW);
  661. if (buttons.length) {
  662. buttons.forEach(el => el.click());
  663. }
  664. }
  665. }
  666.  
  667. static hideSignUpSection() { // "New to Twitter?"
  668. if (!I18N.SIGNUP) {// Unsupported lang, no SIGNUP constant
  669. return;
  670. }
  671. const elem = document.querySelector(`section[aria-label="${I18N.SIGNUP}"][role=region]`);
  672. if (elem) {
  673. elem.parentNode.classList.add("ujs-hidden");
  674. }
  675. }
  676.  
  677. // Call it once.
  678. // "Don’t miss what’s happening" if you are not logged in.
  679. // It looks that `#layers` is used only for this bar.
  680. static hideSignUpBottomBarAndMessages(doNotPlayVideosAutomatically) {
  681. if (doNotPlayVideosAutomatically) {
  682. addCSS(`
  683. #layers > div:nth-child(1) {
  684. display: none;
  685. }
  686. `);
  687. } else {
  688. addCSS(`
  689. #layers > div:nth-child(1) {
  690. height: 1px;
  691. opacity: 0;
  692. }
  693. `);
  694. }
  695. }
  696.  
  697. // "Trends for you"
  698. static hideTrends() {
  699. if (!I18N.TRENDS) { // Unsupported lang, no TRENDS constant
  700. return;
  701. }
  702. addCSS(`
  703. [aria-label="${I18N.TRENDS}"]
  704. {
  705. display: none;
  706. }
  707. `);
  708. }
  709. static highlightVisitedLinks() {
  710. addCSS(`
  711. a:visited {
  712. color: darkorange;
  713. }
  714. `);
  715. }
  716.  
  717. // Hides "TOPICS TO FOLLOW" only in the right column, NOT in timeline.
  718. // Use it once. To prevent blinking.
  719. static hideTopicsToFollowInstantly() {
  720. if (!I18N.TOPICS_TO_FOLLOW) { // Unsupported lang, no TOPICS_TO_FOLLOW constant
  721. return;
  722. }
  723. addCSS(`
  724. div[aria-label="${I18N.TOPICS_TO_FOLLOW}"] {
  725. display: none;
  726. }
  727. `);
  728. }
  729. // Hides container and "separator line"
  730. static hideTopicsToFollow() {
  731. if (!I18N.TOPICS_TO_FOLLOW) { // Unsupported lang, no TOPICS_TO_FOLLOW constant
  732. return;
  733. }
  734. const elem = xpath(`.//section[@role="region" and child::div[@aria-label="${I18N.TOPICS_TO_FOLLOW}"]]/../..`);
  735. if (!elem) {
  736. return;
  737. }
  738. elem.classList.add("ujs-hidden");
  739.  
  740. elem.previousSibling.classList.add("ujs-hidden"); // a "separator line" (empty element of "TRENDS", for example)
  741. // in fact it's a hack // todo rework // may hide "You might like" section [bug]
  742. }
  743.  
  744. // todo split to two methods
  745. // todo fix it, currently it works questionably
  746. // not tested with non eng langs
  747. static footerHandled = false;
  748. static hideAndMoveFooter() { // "Terms of Service Privacy Policy Cookie Policy"
  749. let footer = document.querySelector(`main[role=main] nav[aria-label=${I18N.FOOTER}][role=navigation]`);
  750. const nav = document.querySelector("nav[aria-label=Primary][role=navigation]"); // I18N."Primary" [?]
  751.  
  752. if (footer) {
  753. footer = footer.parentNode;
  754. const separatorLine = footer.previousSibling;
  755.  
  756. if (Features.footerHandled) {
  757. footer.remove();
  758. separatorLine.remove();
  759. return;
  760. }
  761.  
  762. nav.append(separatorLine);
  763. nav.append(footer);
  764. footer.classList.add("ujs-show-on-hover");
  765. separatorLine.classList.add("ujs-show-on-hover");
  766.  
  767. Features.footerHandled = true;
  768. }
  769. }
  770. static hideLoginPopup() { // When you are not logged in
  771. const targetNode = document.querySelector("html");
  772. const observerOptions = {
  773. attributes: true,
  774. };
  775. const observer = new MutationObserver(callback);
  776. observer.observe(targetNode, observerOptions);
  777. function callback(mutationList, observer) {
  778. const html = document.querySelector("html");
  779. console.log(mutationList);
  780. // overflow-y: scroll; overscroll-behavior-y: none; font-size: 15px; // default
  781. // overflow: hidden; overscroll-behavior-y: none; font-size: 15px; margin-right: 15px; // popup
  782. if (html.style["overflow"] === "hidden") {
  783. html.style["overflow"] = "";
  784. html.style["overflow-y"] = "scroll";
  785. html.style["margin-right"] = "";
  786. }
  787. const popup = document.querySelector(`#layers div[data-testid="sheetDialog"]`);
  788. if (popup) {
  789. popup.closest(`div[role="dialog"]`).remove();
  790. verbose && (document.title = "⚒" + document.title);
  791. // observer.disconnect();
  792. }
  793. }
  794. }
  795. }
  796. return Features;
  797. }
  798.  
  799. // --- Twitter.RequiredCSS --- //
  800. function getUserScriptCSS() {
  801. const labelText = I18N.IMAGE || "Image";
  802. // By default the scroll is showed all time, since <html style="overflow-y: scroll;>,
  803. // so it works — no need to use `getScrollbarWidth` function from SO (13382516).
  804. const scrollbarWidth = window.innerWidth - document.body.offsetWidth;
  805. const css = `
  806. .ujs-hidden {
  807. display: none;
  808. }
  809. .ujs-no-scroll {
  810. overflow-y: hidden;
  811. }
  812. .ujs-scroll-initial {
  813. overflow-y: initial!important;
  814. }
  815. .ujs-scrollbar-width-margin-right {
  816. margin-right: ${scrollbarWidth}px;
  817. }
  818. .ujs-show-on-hover:hover {
  819. opacity: 1;
  820. transition: opacity 1s ease-out 0.1s;
  821. }
  822. .ujs-show-on-hover {
  823. opacity: 0;
  824. transition: opacity 0.5s ease-out;
  825. }
  826. .ujs-btn-download {
  827. cursor: pointer;
  828. top: 0.5em;
  829. left: 0.5em;
  830. width: 33px;
  831. height: 33px;
  832. background: #e0245e; /*red*/
  833. opacity: 0;
  834. position: absolute;
  835. border-radius: 0.3em;
  836. background-image: linear-gradient(to top, rgba(0,0,0,0.15), rgba(0,0,0,0.05));
  837. ${settings.addBorder ? "border: 2px solid white;" : ""}
  838. }
  839. article[role=article]:hover .ujs-btn-download {
  840. opacity: 1;
  841. }
  842. div[aria-label="${labelText}"]:hover .ujs-btn-download {
  843. opacity: 1;
  844. }
  845. .ujs-btn-download.ujs-downloaded {
  846. background: #4caf50; /*green*/
  847. background-image: linear-gradient(to top, rgba(0,0,0,0.15), rgba(0,0,0,0.05));
  848. opacity: 1;
  849. }
  850. .ujs-btn-download.ujs-video {
  851. left: calc(0.5em + 33px + 3px);
  852. }
  853. article[role=article]:hover .ujs-already-downloaded:not(.ujs-downloaded) {
  854. background: #1da1f2; /*blue*/
  855. background-image: linear-gradient(to top, rgba(0,0,0,0.15), rgba(0,0,0,0.05));
  856. }
  857. div[aria-label="${labelText}"]:hover .ujs-already-downloaded:not(.ujs-downloaded) {
  858. background: #1da1f2; /*blue*/
  859. background-image: linear-gradient(to top, rgba(0,0,0,0.15), rgba(0,0,0,0.05));
  860. }
  861. /* -------------------------------------------------------- */
  862. /* Shadow the button on hover, active and while downloading */
  863. .ujs-btn-download:hover {
  864. background-image: linear-gradient(to top, rgba(0,0,0,0.25), rgba(0,0,0,0.05));
  865. }
  866. .ujs-btn-download:active {
  867. background-image: linear-gradient(to top, rgba(0,0,0,0.55), rgba(0,0,0,0.25));
  868. }
  869. .ujs-btn-download.ujs-downloading {
  870. background-image: linear-gradient(to top, rgba(0,0,0,0.45), rgba(0,0,0,0.15));
  871. }
  872. article[role=article]:hover .ujs-already-downloaded:not(.ujs-downloaded):hover {
  873. background-image: linear-gradient(to top, rgba(0,0,0,0.25), rgba(0,0,0,0.05));
  874. }
  875. article[role=article]:hover .ujs-already-downloaded:not(.ujs-downloaded):active {
  876. background-image: linear-gradient(to top, rgba(0,0,0,0.55), rgba(0,0,0,0.25));
  877. }
  878. article[role=article]:hover .ujs-already-downloaded:not(.ujs-downloaded).ujs-downloading {
  879. background-image: linear-gradient(to top, rgba(0,0,0,0.45), rgba(0,0,0,0.15));
  880. }
  881. div[aria-label="${labelText}"]:hover .ujs-already-downloaded:not(.ujs-downloaded):hover {
  882. background-image: linear-gradient(to top, rgba(0,0,0,0.25), rgba(0,0,0,0.05));
  883. }
  884. div[aria-label="${labelText}"]:hover .ujs-already-downloaded:not(.ujs-downloaded):active {
  885. background-image: linear-gradient(to top, rgba(0,0,0,0.55), rgba(0,0,0,0.25));
  886. }
  887. div[aria-label="${labelText}"]:hover .ujs-already-downloaded:not(.ujs-downloaded).ujs-downloading {
  888. background-image: linear-gradient(to top, rgba(0,0,0,0.45), rgba(0,0,0,0.15));
  889. }
  890. /* -------------------------------------------------------- */
  891. `;
  892. return css.replaceAll(" ".repeat(8), "");
  893. }
  894.  
  895. /*
  896. Features depend on:
  897.  
  898. addRequiredCSS: IMAGE
  899.  
  900. expandSpoilers: YES_VIEW_PROFILE, SHOW_NUDITY, VIEW
  901. handleTitle: QUOTES, ON_TWITTER, TWITTER
  902. hideSignUpSection: SIGNUP
  903. hideTrends: TRENDS
  904. hideTopicsToFollowInstantly: TOPICS_TO_FOLLOW,
  905. hideTopicsToFollow: TOPICS_TO_FOLLOW,
  906.  
  907. [unused]
  908. hideAndMoveFooter: FOOTER
  909. */
  910.  
  911. // --- Twitter.LangConstants --- //
  912. function getLanguageConstants() { //todo: "de", "fr"
  913. const defaultQuotes = [`"`, `"`];
  914.  
  915. const SUPPORTED_LANGUAGES = ["en", "ru", "es", "zh", "ja", ];
  916. // texts
  917. const VIEW = ["View", "Посмотреть", "Ver", "查看", "表示", ];
  918. const YES_VIEW_PROFILE = ["Yes, view profile", "Да, посмотреть профиль", "Sí, ver perfil", "是,查看个人资料", "プロフィールを表示する", ];
  919. const SHOW_NUDITY = ["Show", "Показать", "Mostrar", "显示", "表示", ];
  920. // aria-label texts
  921. const IMAGE = ["Image", "Изображение", "Imagen", "图像", "画像", ];
  922. const SIGNUP = ["Sign up", "Зарегистрироваться", "Regístrate", "注册", "アカウント作成", ];
  923. const TRENDS = ["Timeline: Trending now", "Лента: Актуальные темы", "Cronología: Tendencias del momento", "时间线:当前趋势", "タイムライン: トレンド", ];
  924. const TOPICS_TO_FOLLOW = ["Timeline: ", "Лента: ", "Cronología: ", "时间线:", /*[1]*/ "タイムライン: ", /*[1]*/ ];
  925. const WHO_TO_FOLLOW = ["Who to follow", "Кого читать", "A quién seguir", "推荐关注", "おすすめユーザー" ];
  926. const FOOTER = ["Footer", "Нижний колонтитул", "Pie de página", "页脚", "フッター", ];
  927. // *1 — it's a suggestion, need to recheck. But I can't find a page where I can check it. Was it deleted?
  928. // document.title "{AUTHOR}{ON_TWITTER} {QUOTES[0]}{TEXT}{QUOTES[1]} / {TWITTER}"
  929. const QUOTES = [defaultQuotes, [`«`, `»`], defaultQuotes, defaultQuotes, [`「`, `」`], ];
  930. const ON_TWITTER = [" on Twitter:", " в Твиттере:", " en Twitter:", " 在 Twitter:", "さんはTwitterを使っています", ];
  931. const TWITTER = ["Twitter", "Твиттер", "Twitter", "Twitter", "Twitter", ];
  932. const lang = document.querySelector("html").getAttribute("lang");
  933. const langIndex = SUPPORTED_LANGUAGES.indexOf(lang);
  934.  
  935. return {
  936. SUPPORTED_LANGUAGES,
  937. VIEW: VIEW[langIndex],
  938. YES_VIEW_PROFILE: YES_VIEW_PROFILE[langIndex],
  939. SIGNUP: SIGNUP[langIndex],
  940. TRENDS: TRENDS[langIndex],
  941. TOPICS_TO_FOLLOW: TOPICS_TO_FOLLOW[langIndex],
  942. WHO_TO_FOLLOW: WHO_TO_FOLLOW[langIndex],
  943. FOOTER: FOOTER[langIndex],
  944. QUOTES: QUOTES[langIndex],
  945. ON_TWITTER: ON_TWITTER[langIndex],
  946. TWITTER: TWITTER[langIndex],
  947. IMAGE: IMAGE[langIndex],
  948. SHOW_NUDITY: SHOW_NUDITY[langIndex],
  949. }
  950. }
  951.  
  952. // --- Twitter.Tweet --- //
  953. function hoistTweet() {
  954. class Tweet {
  955. constructor({elem, url}) {
  956. if (url) {
  957. this.elem = null;
  958. this.url = url;
  959. } else {
  960. this.elem = elem;
  961. this.url = Tweet.getUrl(elem);
  962. }
  963. }
  964. static of(innerElem) {
  965. // Workaround for media from a quoted tweet
  966. const url = innerElem.closest(`a[href^="/"]`)?.href;
  967. if (url && url.includes("/status/")) {
  968. return new Tweet({url});
  969. }
  970. const elem = innerElem.closest(`[data-testid="tweet"]`);
  971. if (!elem) { // opened image
  972. verbose && console.log("no-tweet elem");
  973. }
  974. return new Tweet({elem});
  975. }
  976. static getUrl(elem) {
  977. if (!elem) { // if opened image
  978. return location.href;
  979. }
  980.  
  981. const tweetAnchor = [...elem.querySelectorAll("a")].find(el => {
  982. return el.childNodes[0]?.nodeName === "TIME";
  983. });
  984.  
  985. if (tweetAnchor) {
  986. return tweetAnchor.href;
  987. }
  988. // else if selected tweet
  989. return location.href;
  990. }
  991.  
  992. get author() {
  993. return this.url.match(/(?<=twitter\.com\/).+?(?=\/)/)?.[0];
  994. }
  995. get id() {
  996. return this.url.match(/(?<=\/status\/)\d+/)?.[0];
  997. }
  998. }
  999. return Tweet;
  1000. }
  1001.  
  1002. // --- Twitter.API --- //
  1003. function hoistAPI() {
  1004. class API {
  1005. static guestToken = getCookie("gt");
  1006. static csrfToken = getCookie("ct0"); // todo: lazy — not available at the first run
  1007. // Guest/Suspended account Bearer token
  1008. static guestAuthorization = "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA";
  1009.  
  1010. // Seems to be outdated at 2022.05
  1011. static async _requestBearerToken() {
  1012. const scriptSrc = [...document.querySelectorAll("script")]
  1013. .find(el => el.src.match(/https:\/\/abs\.twimg\.com\/responsive-web\/client-web\/main[\w\d\.]*\.js/)).src;
  1014.  
  1015. let text;
  1016. try {
  1017. text = await (await fetch(scriptSrc)).text();
  1018. } catch (e) {
  1019. console.error(e, scriptSrc);
  1020. throw e;
  1021. }
  1022.  
  1023. const authorizationKey = text.match(/(?<=")AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D.+?(?=")/)[0];
  1024. const authorization = `Bearer ${authorizationKey}`;
  1025.  
  1026. return authorization;
  1027. }
  1028.  
  1029. static async getAuthorization() {
  1030. if (!API.authorization) {
  1031. API.authorization = await API._requestBearerToken();
  1032. }
  1033. return API.authorization;
  1034. }
  1035.  
  1036. static async apiRequest(url) {
  1037. const _url = url.toString();
  1038. verbose && console.log("[ujs][apiRequest]", _url);
  1039. // Hm... it always is the same. Even for a logged user.
  1040. // const authorization = API.guestToken ? API.guestAuthorization : await API.getAuthorization();
  1041. const authorization = API.guestAuthorization;
  1042.  
  1043. // for debug
  1044. verbose && sessionStorage.setItem("guestAuthorization", API.guestAuthorization);
  1045. verbose && sessionStorage.setItem("authorization", API.authorization);
  1046. verbose && sessionStorage.setItem("x-csrf-token", API.csrfToken);
  1047. verbose && sessionStorage.setItem("x-guest-token", API.guestToken);
  1048.  
  1049. const headers = new Headers({
  1050. authorization,
  1051. "x-csrf-token": API.csrfToken,
  1052. });
  1053. if (API.guestToken) {
  1054. headers.append("x-guest-token", API.guestToken);
  1055. } else { // may be skipped
  1056. headers.append("x-twitter-active-user", "yes");
  1057. headers.append("x-twitter-auth-type", "OAuth2Session");
  1058. }
  1059.  
  1060. let json;
  1061. try {
  1062. const response = await fetch(_url, {headers});
  1063. json = await response.json();
  1064. } catch (e) {
  1065. console.error(e, _url);
  1066. throw e;
  1067. }
  1068.  
  1069. verbose && console.log("[ujs][apiRequest]", JSON.stringify(json, null, " "));
  1070. // 429 - [{code: 88, message: "Rate limit exceeded"}] — for suspended accounts
  1071. return json;
  1072. }
  1073.  
  1074. // @return {bitrate, content_type, url}
  1075. static async getVideoInfo(tweetId) {
  1076. // const url = new URL(`https://api.twitter.com/2/timeline/conversation/${tweetId}.json`); // only for suspended/anon
  1077. const url = new URL(`https://twitter.com/i/api/2/timeline/conversation/${tweetId}.json`);
  1078. url.searchParams.set("tweet_mode", "extended");
  1079.  
  1080. const json = await API.apiRequest(url);
  1081. const tweetData = json.globalObjects.tweets[tweetId];
  1082. const videoVariants = tweetData.extended_entities.media[0].video_info.variants;
  1083. verbose && console.log(videoVariants);
  1084.  
  1085.  
  1086. const video = videoVariants
  1087. .filter(el => el.bitrate !== undefined) // if content_type: "application/x-mpegURL" // .m3u8
  1088. .reduce((acc, cur) => cur.bitrate > acc.bitrate ? cur : acc);
  1089. return video;
  1090. }
  1091.  
  1092. static async getUserInfo(username) {
  1093. const qHash = "Bhlf1dYJ3bYCKmLfeEQ31A"; // todo: change
  1094. const variables = JSON.stringify({"screen_name": username, "withSafetyModeUserFields": true, "withSuperFollowsUserFields": true});
  1095. const url = `https://twitter.com/i/api/graphql/${qHash}/UserByScreenName?variables=${encodeURIComponent(variables)}`;
  1096. const json = await API.apiRequest(url);
  1097. verbose && console.log("[getUserInfo]", json);
  1098. return json.data.user.result.legacy.entities.url?.urls[0].expanded_url;
  1099. }
  1100. }
  1101. return API;
  1102. }
  1103.  
  1104. // ---------------------------------------------------------------------------------------------------------------------
  1105. // ---------------------------------------------------------------------------------------------------------------------
  1106. // --- Common Utils --- //
  1107.  
  1108. // --- LocalStorage util class --- //
  1109. function hoistLS(settings = {}) {
  1110. const {
  1111. verbose, // debug "messages" in the document.title
  1112. } = settings;
  1113. class LS {
  1114. constructor(name) {
  1115. this.name = name;
  1116. }
  1117. getItem(defaultValue) {
  1118. return LS.getItem(this.name, defaultValue);
  1119. }
  1120. setItem(value) {
  1121. LS.setItem(this.name, value);
  1122. }
  1123. removeItem() {
  1124. LS.removeItem(this.name);
  1125. }
  1126. async pushItem(value) { // array method
  1127. await LS.pushItem(this.name, value);
  1128. }
  1129. async popItem(value) { // array method
  1130. await LS.popItem(this.name, value);
  1131. }
  1132. hasItem(value) { // array method
  1133. return LS.hasItem(this.name, value);
  1134. }
  1135.  
  1136. static getItem(name, defaultValue) {
  1137. const value = localStorage.getItem(name);
  1138. if (value === undefined) {
  1139. return undefined;
  1140. }
  1141. if (value === null) { // when there is no such item
  1142. LS.setItem(name, defaultValue);
  1143. return defaultValue;
  1144. }
  1145. return JSON.parse(value);
  1146. }
  1147. static setItem(name, value) {
  1148. localStorage.setItem(name, JSON.stringify(value));
  1149. }
  1150. static removeItem(name) {
  1151. localStorage.removeItem(name);
  1152. }
  1153. static async pushItem(name, value) {
  1154. const array = LS.getItem(name, []);
  1155. array.push(value);
  1156. LS.setItem(name, array);
  1157.  
  1158. //sanity check
  1159. await sleep(50);
  1160. if (!LS.hasItem(name, value)) {
  1161. if (verbose) {
  1162. document.title = "🟥" + document.title;
  1163. }
  1164. await LS.pushItem(name, value);
  1165. }
  1166. }
  1167. static async popItem(name, value) { // remove from an array
  1168. const array = LS.getItem(name, []);
  1169. if (array.indexOf(value) !== -1) {
  1170. array.splice(array.indexOf(value), 1);
  1171. LS.setItem(name, array);
  1172.  
  1173. //sanity check
  1174. await sleep(50);
  1175. if (LS.hasItem(name, value)) {
  1176. if (verbose) {
  1177. document.title = "🟨" + document.title;
  1178. }
  1179. await LS.popItem(name, value);
  1180. }
  1181. }
  1182. }
  1183. static hasItem(name, value) { // has in array
  1184. const array = LS.getItem(name, []);
  1185. return array.indexOf(value) !== -1;
  1186. }
  1187. }
  1188. return LS;
  1189. }
  1190.  
  1191. // --- Just groups them in a function for the convenient code looking --- //
  1192. function getUtils({verbose}) {
  1193. function sleep(time) {
  1194. return new Promise(resolve => setTimeout(resolve, time));
  1195. }
  1196.  
  1197. async function fetchResource(url) {
  1198. try {
  1199. const response = await fetch(url, {
  1200. // cache: "force-cache",
  1201. });
  1202. const lastModifiedDateSeconds = response.headers.get("last-modified");
  1203. const contentType = response.headers.get("content-type");
  1204.  
  1205. const lastModifiedDate = dateToDayDateString(lastModifiedDateSeconds);
  1206. const extension = contentType ? extensionFromMime(contentType) : null;
  1207. const blob = await response.blob();
  1208.  
  1209. // https://pbs.twimg.com/media/AbcdEFgijKL01_9?format=jpg&name=orig -> AbcdEFgijKL01_9
  1210. // https://pbs.twimg.com/ext_tw_video_thumb/1234567890123456789/pu/img/Ab1cd2345EFgijKL.jpg?name=orig -> Ab1cd2345EFgijKL.jpg
  1211. // https://video.twimg.com/ext_tw_video/1234567890123456789/pu/vid/946x720/Ab1cd2345EFgijKL.mp4?tag=10 -> Ab1cd2345EFgijKL.mp4
  1212. const _url = new URL(url);
  1213. const {filename} = (_url.origin + _url.pathname).match(/(?<filename>[^\/]+$)/).groups;
  1214.  
  1215. const {name} = filename.match(/(?<name>^[^\.]+)/).groups;
  1216. return {blob, lastModifiedDate, contentType, extension, name};
  1217. } catch (error) {
  1218. verbose && console.error("[fetchResource]", url, error);
  1219. throw error;
  1220. }
  1221. }
  1222.  
  1223. function extensionFromMime(mimeType) {
  1224. let extension = mimeType.match(/(?<=\/).+/)[0];
  1225. extension = extension === "jpeg" ? "jpg" : extension;
  1226. return extension;
  1227. }
  1228.  
  1229. // the original download url will be posted as hash of the blob url, so you can check it in the download manager's history
  1230. function download(blob, name, url) {
  1231. const anchor = document.createElement("a");
  1232. anchor.setAttribute("download", name || "");
  1233. const blobUrl = URL.createObjectURL(blob);
  1234. anchor.href = blobUrl + (url ? ("#" + url) : "");
  1235. anchor.click();
  1236. setTimeout(() => URL.revokeObjectURL(blobUrl), 30000);
  1237. }
  1238.  
  1239. // "Sun, 10 Jan 2021 22:22:22 GMT" -> "2021.01.10"
  1240. function dateToDayDateString(dateValue, utc = true) {
  1241. const _date = new Date(dateValue);
  1242. function pad(str) {
  1243. return str.toString().padStart(2, "0");
  1244. }
  1245. const _utc = utc ? "UTC" : "";
  1246. const year = _date[`get${_utc}FullYear`]();
  1247. const month = _date[`get${_utc}Month`]() + 1;
  1248. const date = _date[`get${_utc}Date`]();
  1249.  
  1250. return year + "." + pad(month) + "." + pad(date);
  1251. }
  1252.  
  1253.  
  1254. function addCSS(css) {
  1255. const styleElem = document.createElement("style");
  1256. styleElem.textContent = css;
  1257. document.body.append(styleElem);
  1258. return styleElem;
  1259. }
  1260.  
  1261.  
  1262. function getCookie(name) {
  1263. verbose && console.log(document.cookie);
  1264. const regExp = new RegExp(`(?<=${name}=)[^;]+`);
  1265. return document.cookie.match(regExp)?.[0];
  1266. }
  1267.  
  1268. function throttle(runnable, time = 50) {
  1269. let waiting = false;
  1270. let queued = false;
  1271. let context;
  1272. let args;
  1273.  
  1274. return function() {
  1275. if (!waiting) {
  1276. waiting = true;
  1277. setTimeout(function() {
  1278. if (queued) {
  1279. runnable.apply(context, args);
  1280. context = args = undefined;
  1281. }
  1282. waiting = queued = false;
  1283. }, time);
  1284. return runnable.apply(this, arguments);
  1285. } else {
  1286. queued = true;
  1287. context = this;
  1288. args = arguments;
  1289. }
  1290. }
  1291. }
  1292. function throttleWithResult(func, time = 50) {
  1293. let waiting = false;
  1294. let args;
  1295. let context;
  1296. let timeout;
  1297. let promise;
  1298.  
  1299. return async function() {
  1300. if (!waiting) {
  1301. waiting = true;
  1302. timeout = new Promise(async resolve => {
  1303. await sleep(time);
  1304. waiting = false;
  1305. resolve();
  1306. });
  1307. return func.apply(this, arguments);
  1308. } else {
  1309. args = arguments;
  1310. context = this;
  1311. }
  1312.  
  1313. if (!promise) {
  1314. promise = new Promise(async resolve => {
  1315. await timeout;
  1316. const result = func.apply(context, args);
  1317. args = context = promise = undefined;
  1318. resolve(result);
  1319. });
  1320. }
  1321. return promise;
  1322. }
  1323. }
  1324.  
  1325.  
  1326. function xpath(path, node = document) {
  1327. let xPathResult = document.evaluate(path, node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  1328. return xPathResult.singleNodeValue;
  1329. }
  1330. function xpathAll(path, node = document) {
  1331. let xPathResult = document.evaluate(path, node, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  1332. const nodes = [];
  1333. try {
  1334. let node = xPathResult.iterateNext();
  1335.  
  1336. while (node) {
  1337. nodes.push(node);
  1338. node = xPathResult.iterateNext();
  1339. }
  1340. return nodes;
  1341. }
  1342. catch (e) {
  1343. // todo need investigate it
  1344. console.error(e); // "The document has mutated since the result was returned."
  1345. return [];
  1346. }
  1347. }
  1348.  
  1349. return {
  1350. sleep, fetchResource, extensionFromMime, download, dateToDayDateString,
  1351. addCSS,
  1352. getCookie,
  1353. throttle, throttleWithResult,
  1354. xpath, xpathAll,
  1355. }
  1356. }
  1357.  
  1358.  
  1359. // ---------------------------------------------------------------------------------------------------------------------
  1360. // ---------------------------------------------------------------------------------------------------------------------