Twitter Click'n'Save

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

目前为 2024-05-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Twitter Click'n'Save
  3. // @version 1.11.2-2024.05.29
  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. // @match https://x.com/*
  8. // @homepageURL https://github.com/AlttiRi/twitter-click-and-save
  9. // @supportURL https://github.com/AlttiRi/twitter-click-and-save/issues
  10. // @license GPL-3.0
  11. // @grant GM_registerMenuCommand
  12. // ==/UserScript==
  13. // ---------------------------------------------------------------------------------------------------------------------
  14. // ---------------------------------------------------------------------------------------------------------------------
  15.  
  16.  
  17.  
  18. // Please, report bugs and suggestions on GitHub, not Greasyfork.
  19. // --> https://github.com/AlttiRi/twitter-click-and-save/issues <--
  20.  
  21.  
  22.  
  23. // ---------------------------------------------------------------------------------------------------------------------
  24. const sitename = location.hostname.replace(".com", ""); // "twitter" | "x"
  25. // ---------------------------------------------------------------------------------------------------------------------
  26. // --- "Imports" --- //
  27. const {StorageNames, StorageNamesOld} = getStorageNames();
  28.  
  29. const {verbose, debugPopup} = getDebugSettings(); // --- For debug --- //
  30.  
  31.  
  32. const {
  33. sleep, fetchResource, downloadBlob,
  34. addCSS,
  35. getCookie,
  36. throttle,
  37. xpath, xpathAll,
  38. responseProgressProxy,
  39. dateToDayDateString,
  40. toLineJSON,
  41. isFirefox,
  42. getBrowserName,
  43. removeSearchParams,
  44. } = getUtils({verbose});
  45.  
  46. const LS = hoistLS({verbose});
  47.  
  48. const API = hoistAPI();
  49. const Tweet = hoistTweet();
  50. const Features = hoistFeatures();
  51. const I18N = getLanguageConstants();
  52.  
  53. // ---------------------------------------------------------------------------------------------------------------------
  54.  
  55. function getStorageNames() {
  56. // New LocalStorage key names 2023.07.05
  57. const StorageNames = {
  58. settings: "ujs-twitter-click-n-save-settings",
  59. settingsImageHistoryBy: "ujs-twitter-click-n-save-settings-image-history-by",
  60. downloadedImageNames: "ujs-twitter-click-n-save-downloaded-image-names",
  61. downloadedImageTweetIds: "ujs-twitter-click-n-save-downloaded-image-tweet-ids",
  62. downloadedVideoTweetIds: "ujs-twitter-click-n-save-downloaded-video-tweet-ids",
  63.  
  64. migrated: "ujs-twitter-click-n-save-migrated", // Currently unused
  65. browserName: "ujs-twitter-click-n-save-browser-name", // Hidden settings
  66. verbose: "ujs-twitter-click-n-save-verbose", // Hidden settings for debug
  67. debugPopup: "ujs-twitter-click-n-save-debug-popup", // Hidden settings for debug
  68. };
  69. const StorageNamesOld = {
  70. settings: "ujs-click-n-save-settings",
  71. settingsImageHistoryBy: "ujs-images-history-by",
  72. downloadedImageNames: "ujs-twitter-downloaded-images-names",
  73. downloadedImageTweetIds: "ujs-twitter-downloaded-image-tweet-ids",
  74. downloadedVideoTweetIds: "ujs-twitter-downloaded-video-tweet-ids",
  75. };
  76. return {StorageNames, StorageNamesOld};
  77. }
  78.  
  79. function getDebugSettings() {
  80. let verbose = false;
  81. let debugPopup = false;
  82. try {
  83. verbose = Boolean(JSON.parse(localStorage.getItem(StorageNames.verbose)));
  84. } catch (err) {}
  85. try {
  86. debugPopup = Boolean(JSON.parse(localStorage.getItem(StorageNames.debugPopup)));
  87. } catch (err) {}
  88.  
  89. return {verbose, debugPopup};
  90. }
  91.  
  92. const historyHelper = getHistoryHelper();
  93. historyHelper.migrateLocalStore();
  94.  
  95. // ---------------------------------------------------------------------------------------------------------------------
  96.  
  97.  
  98. // ---------------------------------------------------------------------------------------------------------------------
  99.  
  100. if (globalThis.GM_registerMenuCommand /* undefined in Firefox with VM */ || typeof GM_registerMenuCommand === "function") {
  101. GM_registerMenuCommand("Show settings", showSettings);
  102. }
  103.  
  104. const settings = loadSettings();
  105.  
  106. if (verbose) {
  107. console.log("[ujs][settings]", settings);
  108. }
  109. if (debugPopup) {
  110. showSettings();
  111. }
  112.  
  113. // ---------------------------------------------------------------------------------------------------------------------
  114.  
  115. const fetch = ujs_getGlobalFetch({verbose, strictTrackingProtectionFix: settings.strictTrackingProtectionFix});
  116.  
  117. function ujs_getGlobalFetch({verbose, strictTrackingProtectionFix} = {}) {
  118. const useFirefoxStrictTrackingProtectionFix = strictTrackingProtectionFix === undefined ? true : strictTrackingProtectionFix; // Let's use by default
  119. const useFirefoxFix = useFirefoxStrictTrackingProtectionFix && typeof wrappedJSObject === "object" && typeof wrappedJSObject.fetch === "function";
  120. // --- [VM/GM + Firefox ~90+ + Enabled "Strict Tracking Protection"] fix --- //
  121. function fixedFirefoxFetch(resource, init = {}) {
  122. verbose && console.log("[ujs][wrappedJSObject.fetch]", resource, init);
  123. if (init.headers instanceof Headers) {
  124. // Since `Headers` are not allowed for structured cloning.
  125. init.headers = Object.fromEntries(init.headers.entries());
  126. }
  127. return wrappedJSObject.fetch(cloneInto(resource, document), cloneInto(init, document));
  128. }
  129. return useFirefoxFix ? fixedFirefoxFetch : globalThis.fetch;
  130. }
  131.  
  132. // ---------------------------------------------------------------------------------------------------------------------
  133. // --- Features to execute --- //
  134.  
  135. const doNotPlayVideosAutomatically = false; // Hidden settings
  136.  
  137. function execFeaturesOnce() {
  138. settings.goFromMobileToMainSite && Features.goFromMobileToMainSite();
  139. settings.addRequiredCSS && Features.addRequiredCSS();
  140. settings.hideSignUpBottomBarAndMessages && Features.hideSignUpBottomBarAndMessages(doNotPlayVideosAutomatically);
  141. settings.hideTrends && Features.hideTrends();
  142. settings.highlightVisitedLinks && Features.highlightVisitedLinks();
  143. settings.hideLoginPopup && Features.hideLoginPopup();
  144. }
  145. function execFeaturesImmediately() {
  146. settings.expandSpoilers && Features.expandSpoilers();
  147. }
  148. function execFeatures() {
  149. settings.imagesHandler && Features.imagesHandler();
  150. settings.videoHandler && Features.videoHandler();
  151. settings.expandSpoilers && Features.expandSpoilers();
  152. settings.hideSignUpSection && Features.hideSignUpSection();
  153. settings.directLinks && Features.directLinks();
  154. settings.handleTitle && Features.handleTitle();
  155. }
  156.  
  157. // ---------------------------------------------------------------------------------------------------------------------
  158.  
  159. // ---------------------------------------------------------------------------------------------------------------------
  160. // --- Script runner --- //
  161.  
  162. (function starter(feats) {
  163. const {once, onChangeImmediate, onChange} = feats;
  164.  
  165. once();
  166. onChangeImmediate();
  167. const onChangeThrottled = throttle(onChange, 250);
  168. onChangeThrottled();
  169.  
  170. const targetNode = document.querySelector("body");
  171. const observerOptions = {
  172. subtree: true,
  173. childList: true,
  174. };
  175. const observer = new MutationObserver(callback);
  176. observer.observe(targetNode, observerOptions);
  177.  
  178. function callback(mutationList, _observer) {
  179. verbose && console.log("[ujs][mutationList]", mutationList);
  180. onChangeImmediate();
  181. onChangeThrottled();
  182. }
  183. })({
  184. once: execFeaturesOnce,
  185. onChangeImmediate: execFeaturesImmediately,
  186. onChange: execFeatures
  187. });
  188.  
  189. // ---------------------------------------------------------------------------------------------------------------------
  190. // ---------------------------------------------------------------------------------------------------------------------
  191.  
  192. function loadSettings() {
  193. const defaultSettings = {
  194. hideTrends: true,
  195. hideSignUpSection: false,
  196. hideSignUpBottomBarAndMessages: false,
  197. doNotPlayVideosAutomatically: false,
  198. goFromMobileToMainSite: false,
  199.  
  200. highlightVisitedLinks: true,
  201. highlightOnlySpecialVisitedLinks: true,
  202. expandSpoilers: true,
  203.  
  204. directLinks: true,
  205. handleTitle: true,
  206.  
  207. imagesHandler: true,
  208. videoHandler: true,
  209. addRequiredCSS: true,
  210.  
  211. hideLoginPopup: false,
  212. addBorder: false,
  213.  
  214. downloadProgress: true,
  215. strictTrackingProtectionFix: false,
  216. };
  217.  
  218. let savedSettings;
  219. try {
  220. savedSettings = JSON.parse(localStorage.getItem(StorageNames.settings)) || {};
  221. } catch (err) {
  222. console.error("[ujs][parse-settings]", err);
  223. localStorage.removeItem(StorageNames.settings);
  224. savedSettings = {};
  225. }
  226. savedSettings = Object.assign(defaultSettings, savedSettings);
  227. return savedSettings;
  228. }
  229. function showSettings() {
  230. closeSetting();
  231. if (window.scrollY > 0) {
  232. document.querySelector("html").classList.add("ujs-scroll-initial");
  233. document.body.classList.add("ujs-scrollbar-width-margin-right");
  234. }
  235. document.body.classList.add("ujs-no-scroll");
  236.  
  237. const modalWrapperStyle = `
  238. color-scheme: light;
  239. width: 100%;
  240. height: 100%;
  241. position: fixed;
  242. display: flex;
  243. justify-content: center;
  244. align-items: center;
  245. z-index: 99999;
  246. backdrop-filter: blur(4px);
  247. background-color: rgba(255, 255, 255, 0.5);
  248. `;
  249. const modalSettingsStyle = `
  250. background-color: white;
  251. min-width: 320px;
  252. min-height: 320px;
  253. border: 1px solid darkgray;
  254. padding: 8px;
  255. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  256. `;
  257. const s = settings;
  258. const downloadProgressFFTitle = `Disable the download progress if you use Firefox with "Enhanced Tracking Protection" set to "Strict" and ViolentMonkey, or GreaseMonkey extension`;
  259. const strictTrackingProtectionFixFFTitle = `Choose this if you use ViolentMonkey, or GreaseMonkey in Firefox with "Enhanced Tracking Protection" set to "Strict". It is not required in case you use TamperMonkey.`;
  260. document.body.insertAdjacentHTML("afterbegin", `
  261. <div class="ujs-modal-wrapper" style="${modalWrapperStyle}">
  262. <div class="ujs-modal-settings" style="${modalSettingsStyle}">
  263. <fieldset>
  264. <legend>Optional</legend>
  265. <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>
  266. <label title="WARNING: It may broke the login page, but it works fine if you logged in and want to hide 'Messages'"><input type="checkbox" ${s.hideSignUpBottomBarAndMessages ? "checked" : ""} name="hideSignUpBottomBarAndMessages">Hide <strike><b>Sign Up Bar</b> and</strike> <b>Messages</b> (in the bottom). <span title="WARNING: It may broke the login page!">(beta)</span><br/></label>
  267. <label><input type="checkbox" ${s.hideTrends ? "checked" : ""} name="hideTrends">Hide <b>Trends</b> (in the right column)*<br/></label>
  268. <label hidden><input type="checkbox" ${s.doNotPlayVideosAutomatically ? "checked" : ""} name="doNotPlayVideosAutomatically">Do <i>Not</i> Play Videos Automatically</b><br/></label>
  269. <label hidden><input type="checkbox" ${s.goFromMobileToMainSite ? "checked" : ""} name="goFromMobileToMainSite">Redirect from Mobile version (beta)<br/></label>
  270. </fieldset>
  271. <fieldset>
  272. <legend>Recommended</legend>
  273. <label><input type="checkbox" ${s.highlightVisitedLinks ? "checked" : ""} name="highlightVisitedLinks">Highlight Visited Links<br/></label>
  274. <label title="In most cases absolute links are 3rd-party links"><input type="checkbox" ${s.highlightOnlySpecialVisitedLinks ? "checked" : ""} name="highlightOnlySpecialVisitedLinks">Highlight Only Absolute Visited Links<br/></label>
  275.  
  276. <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>
  277. </fieldset>
  278. <fieldset>
  279. <legend>Highly Recommended</legend>
  280. <label><input type="checkbox" ${s.directLinks ? "checked" : ""} name="directLinks">Direct Links</label><br/>
  281. <label><input type="checkbox" ${s.handleTitle ? "checked" : ""} name="handleTitle">Enchance Title*<br/></label>
  282. </fieldset>
  283. <fieldset ${isFirefox ? '': 'style="display: none"'}>
  284. <legend>Firefox only</legend>
  285. <label title='${downloadProgressFFTitle}'><input type="radio" ${s.downloadProgress ? "checked" : ""} name="firefoxDownloadProgress" value="downloadProgress">Download Progress<br/></label>
  286. <label title='${strictTrackingProtectionFixFFTitle}'><input type="radio" ${s.strictTrackingProtectionFix ? "checked" : ""} name="firefoxDownloadProgress" value="strictTrackingProtectionFix">Strict Tracking Protection Fix<br/></label>
  287. </fieldset>
  288. <fieldset>
  289. <legend>Main</legend>
  290. <label><input type="checkbox" ${s.imagesHandler ? "checked" : ""} name="imagesHandler">Image Download Button<br/></label>
  291. <label><input type="checkbox" ${s.videoHandler ? "checked" : ""} name="videoHandler">Video Download Button<br/></label>
  292. <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 -->
  293. </fieldset>
  294. <fieldset>
  295. <legend title="Outdated due to Twitter's updates, or impossible to reimplement">Outdated</legend>
  296. <strike>
  297.  
  298. <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>
  299. <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.\nIt's recommended to use only if you do not have an account to hide the annoiyng login popup."><input type="checkbox" ${s.hideLoginPopup ? "checked" : ""} name="hideLoginPopup">Hide <strike>Login</strike> Popups. (beta)<br/></label>
  300.  
  301. </strike>
  302. </fieldset>
  303. <hr>
  304. <div style="display: flex; justify-content: space-around;">
  305. <div>
  306. History:
  307. <button class="ujs-reload-export-button" style="padding: 5px" >Export</button>
  308. <button class="ujs-reload-import-button" style="padding: 5px" >Import</button>
  309. <button class="ujs-reload-merge-button" style="padding: 5px" >Merge</button>
  310. </div>
  311. <div>
  312. <button class="ujs-reload-setting-button" style="padding: 5px" title="Reload the web page to apply changes">Reload page</button>
  313. <button class="ujs-close-setting-button" style="padding: 5px" title="Just close this popup.\nNote: You need to reload the web page to apply changes.">Close popup</button>
  314. </div>
  315. </div>
  316. <hr>
  317. <h4 style="margin: 0; padding-left: 8px; color: #444;">Notes:</h4>
  318. <ul style="margin: 2px; padding-left: 16px; color: #444;">
  319. <li><b>Reload the page</b> to apply changes.</li>
  320. <li><b>*</b>-marked settings are language dependent. Currently, the follow languages are supported:<br/> "en", "ru", "es", "zh", "ja".</li>
  321. <li hidden>The extension downloads only from twitter.com, not from <b>mobile</b>.twitter.com</li>
  322. </ul>
  323. </div>
  324. </div>`);
  325.  
  326. async function onDone(button) {
  327. button.classList.remove("ujs-btn-error");
  328. button.classList.add("ujs-btn-done");
  329. await sleep(900);
  330. button.classList.remove("ujs-btn-done");
  331. }
  332. async function onError(button, err) {
  333. button.classList.remove("ujs-btn-done");
  334. button.classList.add("ujs-btn-error");
  335. button.title = err.message;
  336. await sleep(1800);
  337. button.classList.remove("ujs-btn-error");
  338. }
  339.  
  340. const exportButton = document.querySelector("body > .ujs-modal-wrapper .ujs-reload-export-button");
  341. const importButton = document.querySelector("body > .ujs-modal-wrapper .ujs-reload-import-button");
  342. const mergeButton = document.querySelector("body > .ujs-modal-wrapper .ujs-reload-merge-button");
  343.  
  344. exportButton.addEventListener("click", (event) => {
  345. const button = event.currentTarget;
  346. historyHelper.exportHistory(() => onDone(button));
  347. });
  348. sleep(50).then(() => {
  349. const infoObj = getStoreInfo();
  350. exportButton.title = Object.entries(infoObj).reduce((acc, [key, value]) => {
  351. acc += `${key}: ${value}\n`;
  352. return acc;
  353. }, "");
  354. });
  355.  
  356. importButton.addEventListener("click", (event) => {
  357. const button = event.currentTarget;
  358. historyHelper.importHistory(
  359. () => onDone(button),
  360. (err) => onError(button, err)
  361. );
  362. });
  363. mergeButton.addEventListener("click", (event) => {
  364. const button = event.currentTarget;
  365. historyHelper.mergeHistory(
  366. () => onDone(button),
  367. (err) => onError(button, err)
  368. );
  369. });
  370.  
  371. document.querySelector("body > .ujs-modal-wrapper .ujs-reload-setting-button").addEventListener("click", () => {
  372. location.reload();
  373. });
  374.  
  375. const checkboxList = document.querySelectorAll("body > .ujs-modal-wrapper input[type=checkbox], body > .ujs-modal-wrapper input[type=radio]");
  376. checkboxList.forEach(checkbox => {
  377. checkbox.addEventListener("change", saveSetting);
  378. });
  379.  
  380. document.querySelector("body > .ujs-modal-wrapper .ujs-close-setting-button").addEventListener("click", closeSetting);
  381.  
  382. function saveSetting() {
  383. const entries = [...document.querySelectorAll("body > .ujs-modal-wrapper input[type=checkbox]")]
  384. .map(checkbox => [checkbox.name, checkbox.checked]);
  385. const radioEntries = [...document.querySelectorAll("body > .ujs-modal-wrapper input[type=radio]")]
  386. .map(checkbox => [checkbox.value, checkbox.checked])
  387. const settings = Object.fromEntries([entries, radioEntries].flat());
  388. // verbose && console.log("[ujs][save-settings]", settings);
  389. localStorage.setItem(StorageNames.settings, JSON.stringify(settings));
  390. }
  391.  
  392. function closeSetting() {
  393. document.body.classList.remove("ujs-no-scroll");
  394. document.body.classList.remove("ujs-scrollbar-width-margin-right");
  395. document.querySelector("html").classList.remove("ujs-scroll-initial");
  396. document.querySelector("body > .ujs-modal-wrapper")?.remove();
  397. }
  398.  
  399.  
  400. }
  401.  
  402. // ---------------------------------------------------------------------------------------------------------------------
  403. // ---------------------------------------------------------------------------------------------------------------------
  404. // --- Twitter Specific code --- //
  405.  
  406. const downloadedImages = new LS(StorageNames.downloadedImageNames);
  407. const downloadedImageTweetIds = new LS(StorageNames.downloadedImageTweetIds);
  408. const downloadedVideoTweetIds = new LS(StorageNames.downloadedVideoTweetIds);
  409.  
  410. // --- That to use for the image history --- //
  411. /** @type {"TWEET_ID" | "IMAGE_NAME"} */
  412. const imagesHistoryBy = LS.getItem(StorageNames.settingsImageHistoryBy, "IMAGE_NAME"); // Hidden settings
  413. // With "TWEET_ID" downloading of 1 image of 4 will mark all 4 images as "already downloaded"
  414. // on the next time when the tweet will appear.
  415. // "IMAGE_NAME" will count each image of a tweet, but it will take more data to store.
  416.  
  417.  
  418. // ---------------------------------------------------------------------------------------------------------------------
  419. // --- Twitter.Features --- //
  420. function hoistFeatures() {
  421. class Features {
  422. static createButton({url, downloaded, isVideo, isThumb, isMultiMedia}) {
  423. const btn = document.createElement("div");
  424. btn.innerHTML = `
  425. <div class="ujs-btn-common ujs-btn-background"></div>
  426. <div class="ujs-btn-common ujs-hover"></div>
  427. <div class="ujs-btn-common ujs-shadow"></div>
  428. <div class="ujs-btn-common ujs-progress" style="--progress: 0%"></div>
  429. <div class="ujs-btn-common ujs-btn-error-text"></div>`.slice(1);
  430. btn.classList.add("ujs-btn-download");
  431. if (!downloaded) {
  432. btn.classList.add("ujs-not-downloaded");
  433. } else {
  434. btn.classList.add("ujs-already-downloaded");
  435. }
  436. if (isVideo) {
  437. btn.classList.add("ujs-video");
  438. }
  439. if (url) {
  440. btn.dataset.url = url;
  441. }
  442. if (isThumb) {
  443. btn.dataset.thumb = "true";
  444. }
  445. if (isMultiMedia) {
  446. btn.dataset.isMultiMedia = "true";
  447. }
  448. return btn;
  449. }
  450.  
  451. static _markButtonAsDownloaded(btn) {
  452. btn.classList.remove("ujs-downloading");
  453. btn.classList.remove("ujs-recently-downloaded");
  454. btn.classList.add("ujs-downloaded");
  455. btn.addEventListener("pointerenter", e => {
  456. btn.classList.add("ujs-recently-downloaded");
  457. }, {once: true});
  458. }
  459.  
  460. // Banner/Background
  461. static async _downloadBanner(url, btn) {
  462. const username = location.pathname.slice(1).split("/")[0];
  463.  
  464. btn.classList.add("ujs-downloading");
  465.  
  466. // https://pbs.twimg.com/profile_banners/34743251/1596331248/1500x500
  467. const {
  468. id, seconds, res
  469. } = url.match(/(?<=\/profile_banners\/)(?<id>\d+)\/(?<seconds>\d+)\/(?<res>\d+x\d+)/)?.groups || {};
  470.  
  471. const {blob, lastModifiedDate, extension, name} = await fetchResource(url);
  472.  
  473. Features.verifyBlob(blob, url, btn);
  474.  
  475. const filename = `[twitter][bg] ${username}—${lastModifiedDate}—${id}—${seconds}.${extension}`;
  476. downloadBlob(blob, filename, url);
  477.  
  478. Features._markButtonAsDownloaded(btn);
  479. }
  480.  
  481. static _ImageHistory = class {
  482. static getImageNameFromUrl(url) {
  483. const _url = new URL(url);
  484. const {filename} = (_url.origin + _url.pathname).match(/(?<filename>[^\/]+$)/).groups;
  485. return filename.match(/^[^.]+/)[0]; // remove extension
  486. }
  487. static isDownloaded({id, url}) {
  488. if (imagesHistoryBy === "TWEET_ID") {
  489. return downloadedImageTweetIds.hasItem(id);
  490. } else if (imagesHistoryBy === "IMAGE_NAME") {
  491. const name = Features._ImageHistory.getImageNameFromUrl(url);
  492. return downloadedImages.hasItem(name);
  493. }
  494. }
  495. static async markDownloaded({id, url}) {
  496. if (imagesHistoryBy === "TWEET_ID") {
  497. await downloadedImageTweetIds.pushItem(id);
  498. } else if (imagesHistoryBy === "IMAGE_NAME") {
  499. const name = Features._ImageHistory.getImageNameFromUrl(url);
  500. await downloadedImages.pushItem(name);
  501. }
  502. }
  503. }
  504. static async imagesHandler() {
  505. verbose && console.log("[ujs][imagesHandler]");
  506. const images = document.querySelectorAll(`img:not([data-handled]):not([src$=".svg"])`);
  507. for (const img of images) {
  508. if (img.dataset.handled) {
  509. continue;
  510. }
  511. img.dataset.handled = "true";
  512. if (img.width === 0) {
  513. const imgOnload = new Promise(async (resolve) => {
  514. img.onload = resolve;
  515. });
  516. await Promise.any([imgOnload, sleep(500)]);
  517. await sleep(10); // to get updated img.width
  518. }
  519. if (img.width < 140) {
  520. continue;
  521. }
  522. verbose && console.log("[ujs][imagesHandler]", {img, img_width: img.width});
  523.  
  524. let anchor = img.closest("a");
  525. // if expanded_url (an image is _opened_ "https://twitter.com/UserName/status/1234567890123456789/photo/1" [fake-url])
  526. if (!anchor) {
  527. anchor = img.parentNode;
  528. }
  529.  
  530. const listitemEl = img.closest(`li[role="listitem"]`);
  531. const isThumb = Boolean(listitemEl); // isMediaThumbnail
  532.  
  533. if (isThumb && anchor.querySelector("svg")) {
  534. await Features.multiMediaThumbHandler(img);
  535. continue;
  536. }
  537.  
  538. const isMobileVideo = img.src.includes("ext_tw_video_thumb") || img.closest(`a[aria-label="Embedded video"]`) || img.alt === "Animated Text GIF" || img.alt === "Embedded video"
  539. || img.src.includes("tweet_video_thumb") /* GIF thumb */;
  540. if (isMobileVideo) {
  541. await Features.mobileVideoHandler(img, isThumb);
  542. continue;
  543. }
  544.  
  545. const btn = Features.createButton({url: img.src, isThumb});
  546. btn.addEventListener("click", Features._imageClickHandler);
  547. anchor.append(btn);
  548.  
  549. const downloaded = Features._ImageHistory.isDownloaded({
  550. id: Tweet.of(btn).id,
  551. url: btn.dataset.url
  552. });
  553. if (downloaded) {
  554. btn.classList.add("ujs-already-downloaded");
  555. }
  556. }
  557. }
  558. static async _imageClickHandler(event) {
  559. event.preventDefault();
  560. event.stopImmediatePropagation();
  561.  
  562. const btn = event.currentTarget;
  563. let url = btn.dataset.url;
  564.  
  565. const isBanner = url.includes("/profile_banners/");
  566. if (isBanner) {
  567. return Features._downloadBanner(url, btn);
  568. }
  569.  
  570. const {id, author} = Tweet.of(btn);
  571. verbose && console.log("[ujs][_imageClickHandler]", {id, author});
  572.  
  573. await Features._downloadPhotoMediaEntry(id, author, url, btn);
  574. Features._markButtonAsDownloaded(btn);
  575. }
  576. static async _downloadPhotoMediaEntry(id, author, url, btn) {
  577. const btnErrorTextElem = btn.querySelector(".ujs-btn-error-text");
  578. const btnProgress = btn.querySelector(".ujs-progress");
  579. if (btn.textContent !== "") {
  580. btnErrorTextElem.textContent = "";
  581. }
  582. btn.classList.remove("ujs-error");
  583. btn.classList.add("ujs-downloading");
  584.  
  585. let onProgress = null;
  586. if (settings.downloadProgress) {
  587. onProgress = ({loaded, total}) => btnProgress.style.cssText = "--progress: " + loaded / total * 90 + "%";
  588. }
  589.  
  590. const originals = ["orig", "4096x4096"];
  591. const samples = ["large", "medium", "900x900", "small", "360x360", /*"240x240", "120x120", "tiny"*/];
  592. let isSample = false;
  593. const previewSize = new URL(url).searchParams.get("name");
  594. if (!samples.includes(previewSize)) {
  595. samples.push(previewSize);
  596. }
  597.  
  598. function handleImgUrl(url) {
  599. const urlObj = new URL(url);
  600. if (originals.length) {
  601. urlObj.searchParams.set("name", originals.shift());
  602. } else if (samples.length) {
  603. isSample = true;
  604. urlObj.searchParams.set("name", samples.shift());
  605. } else {
  606. throw new Error("All fallback URLs are failed to download.");
  607. }
  608. if (urlObj.searchParams.get("format") === "webp") {
  609. urlObj.searchParams.set("format", "jpg");
  610. }
  611. url = urlObj.toString();
  612. verbose && console.log("[ujs][handleImgUrl][url]", url);
  613. return url;
  614. }
  615.  
  616. async function safeFetchResource(url) {
  617. while (true) {
  618. url = handleImgUrl(url);
  619. try {
  620. const result = await fetchResource(url, onProgress);
  621. if (result.status === 404) {
  622. const urlObj = new URL(url);
  623. const params = urlObj.searchParams;
  624. if (params.get("name") === "orig" && params.get("format") === "jpg") {
  625. params.set("format", "png");
  626. url = urlObj.toString();
  627. return await fetchResource(url, onProgress);
  628. }
  629. }
  630. return result;
  631. } catch (err) {
  632. if (!originals.length) {
  633. btn.classList.add("ujs-error");
  634. btnErrorTextElem.textContent = "";
  635. // Add ⚠
  636. btnErrorTextElem.style = `background-image: url("https://abs-0.twimg.com/emoji/v2/svg/26a0.svg"); background-size: 1.5em; background-position: center; background-repeat: no-repeat;`;
  637. btn.title = "[warning] Original images are not available.";
  638. }
  639.  
  640. const ffAutoAllocateChunkSizeBug = err.message.includes("autoAllocateChunkSize"); // https://bugzilla.mozilla.org/show_bug.cgi?id=1757836
  641. if (!samples.length || ffAutoAllocateChunkSizeBug) {
  642. btn.classList.add("ujs-error");
  643. btnErrorTextElem.textContent = "";
  644. // Add ❌
  645. btnErrorTextElem.style = `background-image: url("https://abs-0.twimg.com/emoji/v2/svg/274c.svg"); background-size: 1.5em; background-position: center; background-repeat: no-repeat;`;
  646.  
  647. const ffHint = isFirefox && !settings.strictTrackingProtectionFix && ffAutoAllocateChunkSizeBug ? "\nTry to enable 'Strict Tracking Protection Fix' in the userscript settings." : "";
  648. btn.title = "Failed to download the image." + ffHint;
  649. throw new Error("[error] Fallback URLs are failed.");
  650. }
  651. }
  652. }
  653. }
  654.  
  655. const {blob, lastModifiedDate, extension, name} = await safeFetchResource(url);
  656.  
  657. Features.verifyBlob(blob, url, btn);
  658.  
  659. btnProgress.style.cssText = "--progress: 100%";
  660.  
  661. const sampleText = !isSample ? "" : "[sample]";
  662. const filename = `[twitter]${sampleText} ${author}—${lastModifiedDate}—${id}—${name}.${extension}`;
  663. downloadBlob(blob, filename, url);
  664.  
  665. const downloaded = btn.classList.contains("ujs-already-downloaded") || btn.classList.contains("ujs-downloaded");
  666. if (!downloaded && !isSample) {
  667. await Features._ImageHistory.markDownloaded({id, url});
  668. }
  669.  
  670. if (btn.dataset.isMultiMedia && !isSample) { // dirty fix
  671. const isDownloaded = Features._ImageHistory.isDownloaded({id, url});
  672. if (!isDownloaded) {
  673. await Features._ImageHistory.markDownloaded({id, url});
  674. }
  675. }
  676.  
  677. await sleep(40);
  678. btnProgress.style.cssText = "--progress: 0%";
  679. }
  680.  
  681.  
  682. // Quick Dirty Fix // todo refactor
  683. static async mobileVideoHandler(imgElem, isThumb) { // + thumbVideoHandler
  684. verbose && console.log("[ujs][mobileVideoHandler][vid]", imgElem);
  685.  
  686. const btn = Features.createButton({isVideo: true, url: imgElem.src, isThumb});
  687. btn.addEventListener("click", Features._videoClickHandler);
  688.  
  689. let anchor = imgElem.closest("a");
  690. if (!anchor) {
  691. anchor = imgElem.parentNode;
  692. }
  693. anchor.append(btn);
  694.  
  695. const tweet = Tweet.of(btn);
  696. const id = tweet.id;
  697. const tweetElem = tweet.elem || btn.closest(`[data-testid="tweet"]`);
  698. let vidNumber = 0;
  699.  
  700. if (tweetElem) {
  701. const map = Features.tweetVidWeakMapMobile;
  702. if (map.has(tweetElem)) {
  703. vidNumber = map.get(tweetElem) + 1;
  704. map.set(tweetElem, vidNumber);
  705. } else {
  706. map.set(tweetElem, vidNumber); // can throw an error for null
  707. }
  708. } // else thumbnail
  709.  
  710. const historyId = vidNumber ? id + "-" + vidNumber : id;
  711.  
  712. const downloaded = downloadedVideoTweetIds.hasItem(historyId);
  713. if (downloaded) {
  714. btn.classList.add("ujs-already-downloaded");
  715. }
  716. }
  717.  
  718.  
  719. static async multiMediaThumbHandler(imgElem) {
  720. verbose && console.log("[ujs][multiMediaThumbHandler]", imgElem);
  721. let isVideo = false;
  722. if (imgElem.src.includes("/ext_tw_video_thumb/")) {
  723. isVideo = true;
  724. }
  725.  
  726. const btn = Features.createButton({url: imgElem.src, isVideo, isThumb: true, isMultiMedia: true});
  727. btn.addEventListener("click", Features._multiMediaThumbClickHandler);
  728. let anchor = imgElem.closest("a");
  729. if (!anchor) {
  730. anchor = imgElem.parentNode;
  731. }
  732. anchor.append(btn);
  733.  
  734. let downloaded;
  735. const tweetId = Tweet.of(btn).id;
  736. if (isVideo) {
  737. downloaded = downloadedVideoTweetIds.hasItem(tweetId);
  738. } else {
  739. downloaded = Features._ImageHistory.isDownloaded({
  740. id: tweetId,
  741. url: btn.dataset.url
  742. });
  743. }
  744. if (downloaded) {
  745. btn.classList.add("ujs-already-downloaded");
  746. }
  747. }
  748. static async _multiMediaThumbClickHandler(event) {
  749. event.preventDefault();
  750. event.stopImmediatePropagation();
  751. const btn = event.currentTarget;
  752. const btnErrorTextElem = btn.querySelector(".ujs-btn-error-text");
  753. if (btn.textContent !== "") {
  754. btnErrorTextElem.textContent = "";
  755. }
  756. const {id} = Tweet.of(btn);
  757. /** @type {TweetMediaEntry[]} */
  758. let medias;
  759. try {
  760. medias = await API.getTweetMedias(id);
  761. medias = medias.filter(mediaEntry => mediaEntry.tweet_id === id);
  762. } catch (err) {
  763. console.error(err);
  764. btn.classList.add("ujs-error");
  765. btnErrorTextElem.textContent = "Error";
  766. btn.title = "API.getTweetMedias Error";
  767. throw new Error("API.getTweetMedias Error");
  768. }
  769.  
  770. for (const mediaEntry of medias) {
  771. if (mediaEntry.type === "video") {
  772. await Features._downloadVideoMediaEntry(mediaEntry, btn, id);
  773. } else { // "photo"
  774. const {screen_name: author,download_url: url, tweet_id: id} = mediaEntry;
  775. await Features._downloadPhotoMediaEntry(id, author, url, btn);
  776. }
  777. await sleep(50);
  778. }
  779. Features._markButtonAsDownloaded(btn);
  780. }
  781.  
  782. static tweetVidWeakMapMobile = new WeakMap();
  783. static tweetVidWeakMap = new WeakMap();
  784. static async videoHandler() {
  785. const videos = document.querySelectorAll("video:not([data-handled])");
  786. for (const vid of videos) {
  787. if (vid.dataset.handled) {
  788. continue;
  789. }
  790. vid.dataset.handled = "true";
  791. verbose && console.log("[ujs][videoHandler][vid]", vid);
  792.  
  793. const poster = vid.getAttribute("poster");
  794.  
  795. const btn = Features.createButton({isVideo: true, url: poster});
  796. btn.addEventListener("click", Features._videoClickHandler);
  797.  
  798. let elem = vid.closest(`[data-testid="videoComponent"]`).parentNode;
  799. if (elem) {
  800. elem.append(btn);
  801. } else {
  802. elem = vid.parentNode.parentNode.parentNode;
  803. elem.after(btn);
  804. }
  805.  
  806.  
  807. const tweet = Tweet.of(btn);
  808. const id = tweet.id;
  809. const tweetElem = tweet.elem;
  810. let vidNumber = 0;
  811.  
  812. if (tweetElem) {
  813. const map = Features.tweetVidWeakMap;
  814. if (map.has(tweetElem)) {
  815. vidNumber = map.get(tweetElem) + 1;
  816. map.set(tweetElem, vidNumber);
  817. } else {
  818. map.set(tweetElem, vidNumber); // can throw an error for null
  819. }
  820. } else { // expanded_url
  821. await sleep(10);
  822. const match = location.pathname.match(/(?<=\/video\/)\d/);
  823. if (!match) {
  824. verbose && console.log("[ujs][videoHandler] missed match for match");
  825. }
  826. vidNumber = Number(match[0]) - 1;
  827.  
  828. console.warn("[ujs][videoHandler] vidNumber", vidNumber);
  829. // todo: add support for expanded_url video downloading
  830. }
  831.  
  832. const historyId = vidNumber ? id + "-" + vidNumber : id;
  833.  
  834. const downloaded = downloadedVideoTweetIds.hasItem(historyId);
  835. if (downloaded) {
  836. btn.classList.add("ujs-already-downloaded");
  837. }
  838. }
  839. }
  840. static async _videoClickHandler(event) { // todo: parse the URL from HTML (For "Embedded video" (?))
  841. event.preventDefault();
  842. event.stopImmediatePropagation();
  843.  
  844. const btn = event.currentTarget;
  845. const btnErrorTextElem = btn.querySelector(".ujs-btn-error-text");
  846. const {id} = Tweet.of(btn);
  847.  
  848. if (btn.textContent !== "") {
  849. btnErrorTextElem.textContent = "";
  850. }
  851. btn.classList.remove("ujs-error");
  852. btn.classList.add("ujs-downloading");
  853.  
  854. let mediaEntry;
  855. try {
  856. const medias = await API.getTweetMedias(id);
  857. const posterUrl = btn.dataset.url; // [note] if `posterUrl` has `searchParams`, it will have no extension at the end of `pathname`.
  858. const posterUrlClear = removeSearchParams(posterUrl);
  859. mediaEntry = medias.find(media => media.preview_url.startsWith(posterUrlClear));
  860. verbose && console.log("[ujs][_videoClickHandler] mediaEntry", mediaEntry);
  861. } catch (err) {
  862. console.error(err);
  863. btn.classList.add("ujs-error");
  864. btnErrorTextElem.textContent = "Error";
  865. btn.title = "API.getVideoInfo Error";
  866. throw new Error("API.getVideoInfo Error");
  867. }
  868.  
  869. await Features._downloadVideoMediaEntry(mediaEntry, btn, id);
  870. Features._markButtonAsDownloaded(btn);
  871. }
  872.  
  873. static async _downloadVideoMediaEntry(mediaEntry, btn, id /* of original tweet */) {
  874. const {
  875. screen_name: author,
  876. tweet_id: videoTweetId,
  877. download_url: url,
  878. type_index: vidNumber,
  879. } = mediaEntry;
  880. if (!url) {
  881. throw new Error("No video URL found");
  882. }
  883.  
  884. const btnProgress = btn.querySelector(".ujs-progress");
  885.  
  886. let onProgress = null;
  887. if (settings.downloadProgress) {
  888. onProgress = ({loaded, total}) => btnProgress.style.cssText = "--progress: " + loaded / total * 90 + "%";
  889. }
  890.  
  891. async function safeFetchResource(url, onProgress) {
  892. try {
  893. return await fetchResource(url, onProgress);
  894. } catch (err) {
  895. const btnErrorTextElem = btn.querySelector(".ujs-btn-error-text");
  896. const ffAutoAllocateChunkSizeBug = err.message.includes("autoAllocateChunkSize"); // https://bugzilla.mozilla.org/show_bug.cgi?id=1757836
  897. btn.classList.add("ujs-error");
  898. btnErrorTextElem.textContent = "";
  899. // Add ❌
  900. btnErrorTextElem.style = `background-image: url("https://abs-0.twimg.com/emoji/v2/svg/274c.svg"); background-size: 1.5em; background-position: center; background-repeat: no-repeat;`;
  901.  
  902. const ffHint = isFirefox && !settings.strictTrackingProtectionFix && ffAutoAllocateChunkSizeBug ? "\nTry to enable 'Strict Tracking Protection Fix' in the userscript settings." : "";
  903. btn.title = "Video download failed." + ffHint;
  904. throw new Error("[error] Video download failed.");
  905. }
  906. }
  907.  
  908. const {blob, lastModifiedDate, extension, name} = await safeFetchResource(url, onProgress);
  909.  
  910. btnProgress.style.cssText = "--progress: 100%";
  911.  
  912. Features.verifyBlob(blob, url, btn);
  913.  
  914. const filename = `[twitter] ${author}—${lastModifiedDate}—${videoTweetId}—${name}.${extension}`;
  915. downloadBlob(blob, filename, url);
  916.  
  917. const downloaded = btn.classList.contains("ujs-already-downloaded");
  918. const historyId = vidNumber /* not 0 */ ? videoTweetId + "-" + vidNumber : videoTweetId;
  919. if (!downloaded) {
  920. await downloadedVideoTweetIds.pushItem(historyId);
  921. if (videoTweetId !== id) { // if QRT
  922. const historyId = vidNumber ? id + "-" + vidNumber : id;
  923. await downloadedVideoTweetIds.pushItem(historyId);
  924. }
  925. }
  926.  
  927. if (btn.dataset.isMultiMedia) { // dirty fix
  928. const isDownloaded = downloadedVideoTweetIds.hasItem(historyId);
  929. if (!isDownloaded) {
  930. await downloadedVideoTweetIds.pushItem(historyId);
  931. if (videoTweetId !== id) { // if QRT
  932. const historyId = vidNumber ? id + "-" + vidNumber : id;
  933. await downloadedVideoTweetIds.pushItem(historyId);
  934. }
  935. }
  936. }
  937.  
  938. await sleep(40);
  939. btnProgress.style.cssText = "--progress: 0%";
  940. }
  941.  
  942. static verifyBlob(blob, url, btn) {
  943. if (!blob.size) {
  944. btn.classList.add("ujs-error");
  945. btn.querySelector(".ujs-btn-error-text").textContent = "Error";
  946. btn.title = "Download Error";
  947. throw new Error("Zero size blob: " + url);
  948. }
  949. }
  950.  
  951. static addRequiredCSS() {
  952. const code = getUserScriptCSS();
  953. addCSS(code);
  954. }
  955.  
  956. // it depends on `directLinks()` use only it after `directLinks()`
  957. static handleTitle(title) {
  958.  
  959. if (!I18N.QUOTES) { // Unsupported lang, no QUOTES, ON_TWITTER, TWITTER constants
  960. return;
  961. }
  962.  
  963. // if not an opened tweet
  964. if (!location.href.match(/(twitter|x)\.com\/[^\/]+\/status\/\d+/)) {
  965. return;
  966. }
  967.  
  968. let titleText = title || document.title;
  969. if (titleText === Features.lastHandledTitle) {
  970. return;
  971. }
  972. Features.originalTitle = titleText;
  973.  
  974. const [OPEN_QUOTE, CLOSE_QUOTE] = I18N.QUOTES;
  975. const urlsToReplace = [
  976. ...titleText.matchAll(new RegExp(`https:\\/\\/t\\.co\\/[^ ${CLOSE_QUOTE}]+`, "g"))
  977. ].map(el => el[0]);
  978. // the last one may be the URL to the tweet // or to an embedded shared URL
  979.  
  980. const map = new Map();
  981. const anchors = document.querySelectorAll(`a[data-redirect^="https://t.co/"]`);
  982. for (const anchor of anchors) {
  983. if (urlsToReplace.includes(anchor.dataset.redirect)) {
  984. map.set(anchor.dataset.redirect, anchor.href);
  985. }
  986. }
  987.  
  988. const lastUrl = urlsToReplace.slice(-1)[0];
  989. let lastUrlIsAttachment = false;
  990. let attachmentDescription = "";
  991. if (!map.has(lastUrl)) {
  992. const a = document.querySelector(`a[href="${lastUrl}?amp=1"]`);
  993. if (a) {
  994. lastUrlIsAttachment = true;
  995. attachmentDescription = document.querySelectorAll(`a[href="${lastUrl}?amp=1"]`)[1].innerText;
  996. attachmentDescription = attachmentDescription.replaceAll("\n", " — ");
  997. }
  998. }
  999.  
  1000. for (const [key, value] of map.entries()) {
  1001. titleText = titleText.replaceAll(key, value + ` (${key})`);
  1002. }
  1003.  
  1004. titleText = titleText.replace(new RegExp(`${I18N.ON_TWITTER}(?= ${OPEN_QUOTE})`), ":");
  1005. titleText = titleText.replace(new RegExp(`(?<=${CLOSE_QUOTE}) \\\/ ${I18N.TWITTER}$`), "");
  1006. if (!lastUrlIsAttachment) {
  1007. const regExp = new RegExp(`(?<short> https:\\/\\/t\\.co\\/.{6,14})${CLOSE_QUOTE}$`);
  1008. titleText = titleText.replace(regExp, (match, p1, p2, offset, string) => `${CLOSE_QUOTE} ${p1}`);
  1009. } else {
  1010. titleText = titleText.replace(lastUrl, `${lastUrl} (${attachmentDescription})`);
  1011. }
  1012. document.title = titleText; // Note: some characters will be removed automatically (`\n`, extra spaces)
  1013. Features.lastHandledTitle = document.title;
  1014. }
  1015. static lastHandledTitle = "";
  1016. static originalTitle = "";
  1017.  
  1018. static profileUrlCache = new Map();
  1019. static async directLinks() {
  1020. verbose && console.log("[ujs][directLinks]");
  1021. const hasHttp = url => Boolean(url.match(/^https?:\/\//));
  1022. const anchors = xpathAll(`.//a[@dir="ltr" and child::span and not(@data-handled)]`);
  1023. for (const anchor of anchors) {
  1024. const redirectUrl = new URL(anchor.href);
  1025. const shortUrl = redirectUrl.origin + redirectUrl.pathname; // remove "?amp=1"
  1026.  
  1027. const hrefAttr = anchor.getAttribute("href");
  1028. if (hrefAttr.startsWith("/")) {
  1029. anchor.dataset.handled = "true";
  1030. return;
  1031. }
  1032.  
  1033. verbose && console.log("[ujs][directLinks]", {hrefAttr, redirectUrl_href: redirectUrl.href, shortUrl});
  1034.  
  1035. anchor.dataset.redirect = shortUrl;
  1036. anchor.dataset.handled = "true";
  1037. anchor.rel = "nofollow noopener noreferrer";
  1038.  
  1039. if (Features.profileUrlCache.has(shortUrl)) {
  1040. anchor.href = Features.profileUrlCache.get(shortUrl);
  1041. continue;
  1042. }
  1043.  
  1044. const nodes = xpathAll(`./span[text() != "…"]|./text()`, anchor);
  1045. let url = nodes.map(node => node.textContent).join("");
  1046.  
  1047. const doubleProtocolPrefix = url.match(/(?<dup>^https?:\/\/)(?=https?:)/)?.groups.dup;
  1048. if (doubleProtocolPrefix) {
  1049. url = url.slice(doubleProtocolPrefix.length);
  1050. const span = anchor.querySelector(`[aria-hidden="true"]`);
  1051. if (hasHttp(span.textContent)) { // Fix Twitter's bug related to text copying
  1052. span.style = "display: none;";
  1053. }
  1054. }
  1055.  
  1056. anchor.href = url;
  1057.  
  1058. if (anchor.dataset?.testid === "UserUrl") {
  1059. const href = anchor.getAttribute("href");
  1060. const profileUrl = hasHttp(href) ? href : "https://" + href;
  1061. anchor.href = profileUrl;
  1062. verbose && console.log("[ujs][directLinks][profileUrl]", profileUrl);
  1063.  
  1064. // Restore if URL's text content is too long
  1065. if (anchor.textContent.endsWith("…")) {
  1066. anchor.href = shortUrl;
  1067.  
  1068. try {
  1069. const author = location.pathname.slice(1).match(/[^\/]+/)[0];
  1070. const expanded_url = await API.getUserInfo(author); // todo: make lazy
  1071. anchor.href = expanded_url;
  1072. Features.profileUrlCache.set(shortUrl, expanded_url);
  1073. } catch (err) {
  1074. verbose && console.error("[ujs]", err);
  1075. }
  1076. }
  1077. }
  1078. }
  1079. if (anchors.length) {
  1080. Features.handleTitle(Features.originalTitle);
  1081. }
  1082. }
  1083.  
  1084. // Do NOT throttle it
  1085. static expandSpoilers() {
  1086. const main = document.querySelector("main[role=main]");
  1087. if (!main) {
  1088. return;
  1089. }
  1090.  
  1091. if (!I18N.YES_VIEW_PROFILE) { // Unsupported lang, no YES_VIEW_PROFILE, SHOW_NUDITY, VIEW constants
  1092. return;
  1093. }
  1094.  
  1095. const a = main.querySelectorAll("[data-testid=primaryColumn] [role=button]");
  1096. if (a) {
  1097. const elems = [...a];
  1098. const button = elems.find(el => el.textContent === I18N.YES_VIEW_PROFILE);
  1099. if (button) {
  1100. button.click();
  1101. }
  1102.  
  1103. // "Content warning: Nudity"
  1104. // "The Tweet author flagged this Tweet as showing sensitive content."
  1105. // "Show"
  1106. const buttonShow = elems.find(el => el.textContent === I18N.SHOW_NUDITY);
  1107. if (buttonShow) {
  1108. // const verifying = a.previousSibling.textContent.includes("Nudity"); // todo?
  1109. // if (verifying) {
  1110. buttonShow.click();
  1111. // }
  1112. }
  1113. }
  1114.  
  1115. // todo: expand spoiler commentary in photo view mode (.../photo/1)
  1116. const b = main.querySelectorAll("article [role=presentation] div[role=button]");
  1117. if (b) {
  1118. const elems = [...b];
  1119. const buttons = elems.filter(el => el.textContent === I18N.VIEW);
  1120. if (buttons.length) {
  1121. buttons.forEach(el => el.click());
  1122. }
  1123. }
  1124. }
  1125.  
  1126. static hideSignUpSection() { // "New to Twitter?"
  1127. if (!I18N.SIGNUP) {// Unsupported lang, no SIGNUP constant
  1128. return;
  1129. }
  1130. const elem = document.querySelector(`section[aria-label="${I18N.SIGNUP}"][role=region]`);
  1131. if (elem) {
  1132. elem.parentNode.classList.add("ujs-hidden");
  1133. }
  1134. }
  1135.  
  1136. // Call it once.
  1137. // "Don’t miss what’s happening" if you are not logged in.
  1138. // It looks that `#layers` is used only for this bar.
  1139. static hideSignUpBottomBarAndMessages(doNotPlayVideosAutomatically) {
  1140. if (doNotPlayVideosAutomatically) {
  1141. addCSS(`
  1142. #layers > div:nth-child(1) {
  1143. display: none;
  1144. }
  1145. `);
  1146. } else {
  1147. addCSS(`
  1148. #layers > div:nth-child(1) {
  1149. height: 1px;
  1150. opacity: 0;
  1151. }
  1152. `);
  1153. }
  1154. }
  1155.  
  1156. // "Trends for you"
  1157. static hideTrends() {
  1158. if (!I18N.TRENDS) { // Unsupported lang, no TRENDS constant
  1159. return;
  1160. }
  1161. addCSS(`
  1162. [aria-label="${I18N.TRENDS}"]
  1163. {
  1164. display: none;
  1165. }
  1166. `);
  1167. }
  1168.  
  1169. static highlightVisitedLinks() {
  1170. if (settings.highlightOnlySpecialVisitedLinks) {
  1171. addCSS(`
  1172. a[href^="http"]:visited {
  1173. color: darkorange !important;
  1174. }
  1175. `);
  1176. return;
  1177. }
  1178. addCSS(`
  1179. a:visited {
  1180. color: darkorange !important;
  1181. }
  1182. `);
  1183. }
  1184.  
  1185. // todo split to two methods
  1186. // todo fix it, currently it works questionably
  1187. // not tested with non eng languages
  1188. static footerHandled = false;
  1189. static hideAndMoveFooter() { // "Terms of Service Privacy Policy Cookie Policy"
  1190. let footer = document.querySelector(`main[role=main] nav[aria-label=${I18N.FOOTER}][role=navigation]`);
  1191. const nav = document.querySelector("nav[aria-label=Primary][role=navigation]"); // I18N."Primary" [?]
  1192.  
  1193. if (footer) {
  1194. footer = footer.parentNode;
  1195. const separatorLine = footer.previousSibling;
  1196.  
  1197. if (Features.footerHandled) {
  1198. footer.remove();
  1199. separatorLine.remove();
  1200. return;
  1201. }
  1202.  
  1203. nav.append(separatorLine);
  1204. nav.append(footer);
  1205. footer.classList.add("ujs-show-on-hover");
  1206. separatorLine.classList.add("ujs-show-on-hover");
  1207.  
  1208. Features.footerHandled = true;
  1209. }
  1210. }
  1211.  
  1212. static hideLoginPopup() { // When you are not logged in
  1213. const targetNode = document.querySelector("html");
  1214. const observerOptions = {
  1215. attributes: true,
  1216. };
  1217. const observer = new MutationObserver(callback);
  1218. observer.observe(targetNode, observerOptions);
  1219.  
  1220. function callback(mutationList, _observer) {
  1221. const html = document.querySelector("html");
  1222. verbose && console.log("[ujs][hideLoginPopup][mutationList]", mutationList);
  1223. // overflow-y: scroll; overscroll-behavior-y: none; font-size: 15px; // default
  1224. // overflow: hidden; overscroll-behavior-y: none; font-size: 15px; margin-right: 15px; // popup
  1225. if (html.style["overflow"] === "hidden") {
  1226. html.style["overflow"] = "";
  1227. html.style["overflow-y"] = "scroll";
  1228. html.style["margin-right"] = "";
  1229. }
  1230. const popup = document.querySelector(`#layers div[data-testid="sheetDialog"]`);
  1231. if (popup) {
  1232. popup.closest(`div[role="dialog"]`).remove();
  1233. verbose && (document.title = "⚒" + document.title);
  1234. // observer.disconnect();
  1235. }
  1236. }
  1237. }
  1238.  
  1239. static goFromMobileToMainSite() { // uncompleted
  1240. if (location.href.startsWith("https://mobile.twitter.com/")) {
  1241. location.href = location.href.replace("https://mobile.twitter.com/", "https://twitter.com/");
  1242. }
  1243. // TODO: add #redirected, remove by timer // to prevent a potential infinity loop
  1244. }
  1245. }
  1246.  
  1247. return Features;
  1248. }
  1249.  
  1250. function getStoreInfo() {
  1251. const resultObj = {
  1252. total: 0
  1253. };
  1254. for (const [name, lsKey] of Object.entries(StorageNames)) {
  1255. const valueStr = localStorage.getItem(lsKey);
  1256. if (valueStr) {
  1257. try {
  1258. const value = JSON.parse(valueStr);
  1259. if (Array.isArray(value)) {
  1260. const size = new Set(value).size;
  1261. resultObj[name] = size;
  1262. resultObj.total += size;
  1263. }
  1264. } catch (err) {
  1265. // ...
  1266. }
  1267. }
  1268. }
  1269. return resultObj;
  1270. }
  1271.  
  1272. // --- Twitter.RequiredCSS --- //
  1273. function getUserScriptCSS() {
  1274. const labelText = I18N.IMAGE || "Image";
  1275.  
  1276. // By default, the scroll is showed all time, since <html style="overflow-y: scroll;>,
  1277. // so it works — no need to use `getScrollbarWidth` function from SO (13382516).
  1278. const scrollbarWidth = window.innerWidth - document.body.offsetWidth;
  1279.  
  1280. const css = `
  1281. .ujs-modal-wrapper .ujs-modal-settings {
  1282. color: black;
  1283. }
  1284. .ujs-hidden {
  1285. display: none;
  1286. }
  1287. .ujs-no-scroll {
  1288. overflow-y: hidden;
  1289. }
  1290. .ujs-scroll-initial {
  1291. overflow-y: initial!important;
  1292. }
  1293. .ujs-scrollbar-width-margin-right {
  1294. margin-right: ${scrollbarWidth}px;
  1295. }
  1296.  
  1297. .ujs-show-on-hover:hover {
  1298. opacity: 1;
  1299. transition: opacity 1s ease-out 0.1s;
  1300. }
  1301. .ujs-show-on-hover {
  1302. opacity: 0;
  1303. transition: opacity 0.5s ease-out;
  1304. }
  1305.  
  1306. :root {
  1307. --ujs-shadow-1: linear-gradient(to top, rgba(0,0,0,0.15), rgba(0,0,0,0.05));
  1308. --ujs-shadow-2: linear-gradient(to top, rgba(0,0,0,0.25), rgba(0,0,0,0.05));
  1309. --ujs-shadow-3: linear-gradient(to top, rgba(0,0,0,0.45), rgba(0,0,0,0.15));
  1310. --ujs-shadow-4: linear-gradient(to top, rgba(0,0,0,0.55), rgba(0,0,0,0.25));
  1311. --ujs-red: #e0245e;
  1312. --ujs-blue: #1da1f2;
  1313. --ujs-green: #4caf50;
  1314. --ujs-gray: #c2cbd0;
  1315. --ujs-error: white;
  1316. }
  1317.  
  1318. .ujs-progress {
  1319. background-image: linear-gradient(to right, var(--ujs-green) var(--progress), transparent 0%);
  1320. }
  1321.  
  1322. .ujs-shadow {
  1323. background-image: var(--ujs-shadow-1);
  1324. }
  1325. .ujs-btn-download:hover .ujs-hover {
  1326. background-image: var(--ujs-shadow-2);
  1327. }
  1328. .ujs-btn-download.ujs-downloading .ujs-shadow {
  1329. background-image: var(--ujs-shadow-3);
  1330. }
  1331. .ujs-btn-download:active .ujs-shadow {
  1332. background-image: var(--ujs-shadow-4);
  1333. }
  1334.  
  1335. .ujs-btn-download.ujs-downloaded.ujs-recently-downloaded {
  1336. opacity: 0;
  1337. }
  1338.  
  1339. li[role="listitem"]:hover .ujs-btn-download {
  1340. opacity: 1;
  1341. }
  1342. article[role=article]:hover .ujs-btn-download {
  1343. opacity: 1;
  1344. }
  1345. div[aria-label="${labelText}"]:hover .ujs-btn-download {
  1346. opacity: 1;
  1347. }
  1348. .ujs-btn-download.ujs-downloaded {
  1349. opacity: 1;
  1350. }
  1351. .ujs-btn-download.ujs-downloading {
  1352. opacity: 1;
  1353. }
  1354. [data-testid="videoComponent"]:hover + .ujs-btn-download {
  1355. opacity: 1;
  1356. }
  1357. [data-testid="videoComponent"] + .ujs-btn-download:hover {
  1358. opacity: 1;
  1359. }
  1360.  
  1361. .ujs-btn-download {
  1362. cursor: pointer;
  1363. top: 0.5em;
  1364. left: 0.5em;
  1365. position: absolute;
  1366. opacity: 0;
  1367. }
  1368. .ujs-btn-common {
  1369. width: 33px;
  1370. height: 33px;
  1371. border-radius: 0.3em;
  1372. top: 0;
  1373. position: absolute;
  1374. border: 1px solid transparent;
  1375. border-color: var(--ujs-gray);
  1376. ${settings.addBorder ? "border: 2px solid white;" : "border-color: var(--ujs-gray);"}
  1377. }
  1378. .ujs-not-downloaded .ujs-btn-background {
  1379. background: var(--ujs-red);
  1380. }
  1381.  
  1382. .ujs-already-downloaded .ujs-btn-background {
  1383. background: var(--ujs-blue);
  1384. }
  1385.  
  1386. .ujs-btn-done {
  1387. box-shadow: 0 0 6px var(--ujs-green);
  1388. }
  1389. .ujs-btn-error {
  1390. box-shadow: 0 0 6px var(--ujs-red);
  1391. }
  1392.  
  1393. .ujs-downloaded .ujs-btn-background {
  1394. background: var(--ujs-green);
  1395. }
  1396.  
  1397. .ujs-error .ujs-btn-background {
  1398. background: var(--ujs-error);
  1399. }
  1400.  
  1401. .ujs-btn-error-text {
  1402. display: flex;
  1403. align-items: center;
  1404. justify-content: center;
  1405. color: black;
  1406. font-size: 100%;
  1407. }`;
  1408. return css.slice(1);
  1409. }
  1410.  
  1411. /*
  1412. Features depend on:
  1413.  
  1414. addRequiredCSS: IMAGE
  1415.  
  1416. expandSpoilers: YES_VIEW_PROFILE, SHOW_NUDITY, VIEW
  1417. handleTitle: QUOTES, ON_TWITTER, TWITTER
  1418. hideSignUpSection: SIGNUP
  1419. hideTrends: TRENDS
  1420.  
  1421. [unused]
  1422. hideAndMoveFooter: FOOTER
  1423. */
  1424.  
  1425. // --- Twitter.LangConstants --- //
  1426. function getLanguageConstants() { // todo: "de", "fr"
  1427. const defaultQuotes = [`"`, `"`];
  1428.  
  1429. const SUPPORTED_LANGUAGES = ["en", "ru", "es", "zh", "ja", ];
  1430.  
  1431. // texts
  1432. const VIEW = ["View", "Посмотреть", "Ver", "查看", "表示", ];
  1433. const YES_VIEW_PROFILE = ["Yes, view profile", "Да, посмотреть профиль", "Sí, ver perfil", "是,查看个人资料", "プロフィールを表示する", ];
  1434. const SHOW_NUDITY = ["Show", "Показать", "Mostrar", "显示", "表示", ];
  1435.  
  1436. // aria-label texts
  1437. const IMAGE = ["Image", "Изображение", "Imagen", "图像", "画像", ];
  1438. const SIGNUP = ["Sign up", "Зарегистрироваться", "Regístrate", "注册", "アカウント作成", ];
  1439. const TRENDS = ["Timeline: Trending now", "Лента: Актуальные темы", "Cronología: Tendencias del momento", "时间线:当前趋势", "タイムライン: トレンド", ];
  1440. const FOOTER = ["Footer", "Нижний колонтитул", "Pie de página", "页脚", "フッター", ];
  1441.  
  1442. // document.title "{AUTHOR}{ON_TWITTER} {QUOTES[0]}{TEXT}{QUOTES[1]} / {TWITTER}"
  1443. const QUOTES = [defaultQuotes, [`«`, `»`], defaultQuotes, defaultQuotes, [`「`, `」`], ];
  1444. const ON_TWITTER = [" on X:", " в X:", " en X:", " 在 X:", "さんはXを使っています", ];
  1445. const TWITTER = ["X", "X", "X", "X", "X", ];
  1446.  
  1447. const lang = document.querySelector("html").getAttribute("lang");
  1448. const langIndex = SUPPORTED_LANGUAGES.indexOf(lang);
  1449.  
  1450. return {
  1451. SUPPORTED_LANGUAGES,
  1452. VIEW: VIEW[langIndex],
  1453. YES_VIEW_PROFILE: YES_VIEW_PROFILE[langIndex],
  1454. SHOW_NUDITY: SHOW_NUDITY[langIndex],
  1455. IMAGE: IMAGE[langIndex],
  1456. SIGNUP: SIGNUP[langIndex],
  1457. TRENDS: TRENDS[langIndex],
  1458. FOOTER: FOOTER[langIndex],
  1459. QUOTES: QUOTES[langIndex],
  1460. ON_TWITTER: ON_TWITTER[langIndex],
  1461. TWITTER: TWITTER[langIndex],
  1462. }
  1463. }
  1464.  
  1465. // --- Twitter.Tweet --- //
  1466. function hoistTweet() {
  1467. class Tweet {
  1468. constructor({elem, url}) {
  1469. if (url) {
  1470. this.elem = null;
  1471. this.url = url;
  1472. } else {
  1473. this.elem = elem;
  1474. this.url = Tweet.getUrl(elem);
  1475. }
  1476. }
  1477.  
  1478. static of(innerElem) {
  1479. // Workaround for media from a quoted tweet
  1480. const url = innerElem.closest(`a[href^="/"]`)?.href;
  1481. if (url && url.includes("/status/")) {
  1482. return new Tweet({url});
  1483. }
  1484.  
  1485. const elem = innerElem.closest(`[data-testid="tweet"]`);
  1486. if (!elem) { // opened image
  1487. verbose && console.log("[ujs][Tweet.of]", "No-tweet elem");
  1488. }
  1489. return new Tweet({elem});
  1490. }
  1491.  
  1492. static getUrl(elem) {
  1493. if (!elem) {
  1494. verbose && console.log("[ujs][Tweet.getUrl]", "Opened full screen image");
  1495. return location.href;
  1496. }
  1497. const quotedTweetAnchorEl = [...elem.querySelectorAll("a")].find(el => {
  1498. return el.childNodes[0]?.nodeName === "TIME";
  1499. });
  1500. if (quotedTweetAnchorEl) {
  1501. verbose && console.log("[ujs][Tweet.getUrl]", "Quoted/Re Tweet");
  1502. return quotedTweetAnchorEl.href;
  1503. }
  1504. verbose && console.log("[ujs][Tweet.getUrl]", "Unreachable"); // Is it used?
  1505. return location.href;
  1506. }
  1507.  
  1508. get author() {
  1509. return this.url.match(/(?<=(twitter|x)\.com\/).+?(?=\/)/)?.[0];
  1510. }
  1511.  
  1512. get id() {
  1513. return this.url.match(/(?<=\/status\/)\d+/)?.[0];
  1514. }
  1515. }
  1516.  
  1517. return Tweet;
  1518. }
  1519.  
  1520. // --- Twitter.API --- //
  1521. function hoistAPI() {
  1522. class API {
  1523. static guestToken = getCookie("gt");
  1524. static csrfToken = getCookie("ct0"); // todo: lazy — not available at the first run
  1525. // Guest/Suspended account Bearer token
  1526. static guestAuthorization = "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA";
  1527.  
  1528. // Seems to be outdated at 2022.05
  1529. static async _requestBearerToken() {
  1530. const scriptSrc = [...document.querySelectorAll("script")]
  1531. .find(el => el.src.match(/https:\/\/abs\.twimg\.com\/responsive-web\/client-web\/main[\w.]*\.js/)).src;
  1532.  
  1533. let text;
  1534. try {
  1535. text = await (await fetch(scriptSrc)).text();
  1536. } catch (err) {
  1537. /* verbose && */ console.error("[ujs][_requestBearerToken][scriptSrc]", scriptSrc);
  1538. /* verbose && */ console.error("[ujs][_requestBearerToken]", err);
  1539. throw err;
  1540. }
  1541.  
  1542. const authorizationKey = text.match(/(?<=")AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D.+?(?=")/)[0];
  1543. const authorization = `Bearer ${authorizationKey}`;
  1544.  
  1545. return authorization;
  1546. }
  1547.  
  1548. static async getAuthorization() {
  1549. if (!API.authorization) {
  1550. API.authorization = await API._requestBearerToken();
  1551. }
  1552. return API.authorization;
  1553. }
  1554.  
  1555. static requestCache = new Map();
  1556. static vacuumCache() {
  1557. if (API.requestCache.size > 16) {
  1558. API.requestCache.delete(API.requestCache.keys().next().value);
  1559. }
  1560. }
  1561.  
  1562. static async apiRequest(url) {
  1563. const _url = url.toString();
  1564. verbose && console.log("[ujs][apiRequest]", _url);
  1565.  
  1566. if (API.requestCache.has(_url)) {
  1567. verbose && console.log("[ujs][apiRequest] Use cached API request", _url);
  1568. return API.requestCache.get(_url);
  1569. }
  1570.  
  1571. // Hm... it is always the same. Even for a logged user.
  1572. // const authorization = API.guestToken ? API.guestAuthorization : await API.getAuthorization();
  1573. const authorization = API.guestAuthorization;
  1574.  
  1575. // for debug
  1576. verbose && sessionStorage.setItem("guestAuthorization", API.guestAuthorization);
  1577. verbose && sessionStorage.setItem("authorization", API.authorization);
  1578. verbose && sessionStorage.setItem("x-csrf-token", API.csrfToken);
  1579. verbose && sessionStorage.setItem("x-guest-token", API.guestToken);
  1580.  
  1581. const headers = new Headers({
  1582. authorization,
  1583. "x-csrf-token": API.csrfToken,
  1584. "x-twitter-client-language": "en",
  1585. "x-twitter-active-user": "yes"
  1586. });
  1587. if (API.guestToken) {
  1588. headers.append("x-guest-token", API.guestToken);
  1589. } else { // may be skipped
  1590. headers.append("x-twitter-auth-type", "OAuth2Session");
  1591. }
  1592.  
  1593. let json;
  1594. try {
  1595. const response = await fetch(_url, {headers});
  1596. json = await response.json();
  1597. if (response.ok) {
  1598. verbose && console.log("[ujs][apiRequest]", "Cache API request", _url);
  1599. API.vacuumCache();
  1600. API.requestCache.set(_url, json);
  1601. }
  1602. } catch (err) {
  1603. /* verbose && */ console.error("[ujs][apiRequest]", _url);
  1604. /* verbose && */ console.error("[ujs][apiRequest]", err);
  1605. throw err;
  1606. }
  1607.  
  1608. verbose && console.log("[ujs][apiRequest][json]", JSON.stringify(json, null, " "));
  1609. // 429 - [{code: 88, message: "Rate limit exceeded"}] — for suspended accounts
  1610.  
  1611. return json;
  1612. }
  1613.  
  1614. static async getTweetJson(tweetId) {
  1615. const url = API.createTweetJsonEndpointUrl(tweetId);
  1616. const json = await API.apiRequest(url);
  1617. verbose && console.log("[ujs][getTweetJson]", json, JSON.stringify(json));
  1618. return json;
  1619. }
  1620.  
  1621. /** return {tweetResult, tweetLegacy, tweetUser} */
  1622. static parseTweetJson(json, tweetId) {
  1623. const instruction = json.data.threaded_conversation_with_injections_v2.instructions.find(ins => ins.type === "TimelineAddEntries");
  1624. const tweetEntry = instruction.entries.find(ins => ins.entryId === "tweet-" + tweetId);
  1625. let tweetResult = tweetEntry.content.itemContent.tweet_results.result; // {"__typename": "Tweet"} // or {"__typename": "TweetWithVisibilityResults", tweet: {...}} (1641596499351212033)
  1626. if (tweetResult.tweet) {
  1627. tweetResult = tweetResult.tweet;
  1628. }
  1629. verbose && console.log("[ujs][parseTweetJson] tweetResult", tweetResult, JSON.stringify(tweetResult));
  1630. const tweetUser = tweetResult.core.user_results.result; // {"__typename": "User"}
  1631. const tweetLegacy = tweetResult.legacy;
  1632. verbose && console.log("[ujs][parseTweetJson] tweetLegacy", tweetLegacy, JSON.stringify(tweetLegacy));
  1633. verbose && console.log("[ujs][parseTweetJson] tweetUser", tweetUser, JSON.stringify(tweetUser));
  1634. return {tweetResult, tweetLegacy, tweetUser};
  1635. }
  1636.  
  1637. /**
  1638. * @typedef {Object} TweetMediaEntry
  1639. * @property {string} screen_name - "kreamu"
  1640. * @property {string} tweet_id - "1687962620173733890"
  1641. * @property {string} download_url - "https://pbs.twimg.com/media/FWYvXNMXgAA7se2?format=jpg&name=orig"
  1642. * @property {"photo" | "video"} type - "photo"
  1643. * @property {"photo" | "video" | "animated_gif"} type_original - "photo"
  1644. * @property {number} index - 0
  1645. * @property {number} type_index - 0
  1646. * @property {number} type_index_original - 0
  1647. * @property {string} preview_url - "https://pbs.twimg.com/media/FWYvXNMXgAA7se2.jpg"
  1648. * @property {string} media_id - "1687949851516862464"
  1649. * @property {string} media_key - "7_1687949851516862464"
  1650. * @property {string} expanded_url - "https://twitter.com/kreamu/status/1687962620173733890/video/1"
  1651. * @property {string} short_expanded_url - "pic.twitter.com/KeXR8T910R"
  1652. * @property {string} short_tweet_url - "https://t.co/KeXR8T910R"
  1653. * @property {string} tweet_text - "Tracer providing some In-flight entertainment"
  1654. */
  1655. /** @returns {TweetMediaEntry[]} */
  1656. static parseTweetLegacyMedias(tweetResult, tweetLegacy, tweetUser) {
  1657. if (!tweetLegacy.extended_entities || !tweetLegacy.extended_entities.media) {
  1658. return [];
  1659. }
  1660.  
  1661. const medias = [];
  1662. const typeIndex = {}; // "photo", "video", "animated_gif"
  1663. let index = -1;
  1664.  
  1665. for (const media of tweetLegacy.extended_entities.media) {
  1666. index++;
  1667. let type = media.type;
  1668. const type_original = media.type;
  1669. typeIndex[type] = (typeIndex[type] === undefined ? -1 : typeIndex[type]) + 1;
  1670. if (type === "animated_gif") {
  1671. type = "video";
  1672. typeIndex[type] = (typeIndex[type] === undefined ? -1 : typeIndex[type]) + 1;
  1673. }
  1674.  
  1675. let download_url;
  1676. if (media.video_info) {
  1677. const videoInfo = media.video_info.variants
  1678. .filter(el => el.bitrate !== undefined) // if content_type: "application/x-mpegURL" // .m3u8
  1679. .reduce((acc, cur) => cur.bitrate > acc.bitrate ? cur : acc);
  1680. download_url = videoInfo.url;
  1681. } else {
  1682. if (media.media_url_https.includes("?format=")) {
  1683. download_url = media.media_url_https;
  1684. } else {
  1685. // "https://pbs.twimg.com/media/FWYvXNMXgAA7se2.jpg" -> "https://pbs.twimg.com/media/FWYvXNMXgAA7se2?format=jpg&name=orig"
  1686. const parts = media.media_url_https.split(".");
  1687. const ext = parts[parts.length - 1];
  1688. const urlPart = parts.slice(0, -1).join(".");
  1689. download_url = `${urlPart}?format=${ext}&name=orig`;
  1690. }
  1691. }
  1692.  
  1693. const screen_name = tweetUser.legacy.screen_name; // "kreamu"
  1694. const tweet_id = tweetResult.rest_id || tweetLegacy.id_str; // "1687962620173733890"
  1695.  
  1696. const type_index = typeIndex[type]; // 0
  1697. const type_index_original = typeIndex[type_original]; // 0
  1698.  
  1699. const preview_url = media.media_url_https; // "https://pbs.twimg.com/ext_tw_video_thumb/1687949851516862464/pu/img/mTBjwz--nylYk5Um.jpg"
  1700. const media_id = media.id_str; // "1687949851516862464"
  1701. const media_key = media.media_key; // "7_1687949851516862464"
  1702.  
  1703. const expanded_url = media.expanded_url; // "https://twitter.com/kreamu/status/1687962620173733890/video/1"
  1704. const short_expanded_url = media.display_url; // "pic.twitter.com/KeXR8T910R"
  1705. const short_tweet_url = media.url; // "https://t.co/KeXR8T910R"
  1706. const tweet_text = tweetLegacy.full_text // "Tracer providing some In-flight entertainment https://t.co/KeXR8T910R"
  1707. .replace(` ${media.url}`, "");
  1708.  
  1709. // {screen_name, tweet_id, download_url, preview_url, type_index}
  1710. /** @type {TweetMediaEntry} */
  1711. const mediaEntry = {
  1712. screen_name, tweet_id,
  1713. download_url, type, type_original, index,
  1714. type_index, type_index_original,
  1715. preview_url, media_id, media_key,
  1716. expanded_url, short_expanded_url, short_tweet_url, tweet_text,
  1717. };
  1718. medias.push(mediaEntry);
  1719. }
  1720.  
  1721. verbose && console.log("[ujs][parseTweetLegacyMedias] medias", medias);
  1722. return medias;
  1723. }
  1724.  
  1725. static async getTweetMedias(tweetId) {
  1726. const tweetJson = await API.getTweetJson(tweetId);
  1727. const {tweetResult, tweetLegacy, tweetUser} = API.parseTweetJson(tweetJson, tweetId);
  1728.  
  1729. let result = API.parseTweetLegacyMedias(tweetResult, tweetLegacy, tweetUser);
  1730.  
  1731. if (tweetResult.quoted_status_result && tweetResult.quoted_status_result.result /* check is the qouted tweet not deleted */) {
  1732. const tweetResultQuoted = tweetResult.quoted_status_result.result;
  1733. const tweetLegacyQuoted = tweetResultQuoted.legacy;
  1734. const tweetUserQuoted = tweetResultQuoted.core.user_results.result;
  1735. result = [...result, ...API.parseTweetLegacyMedias(tweetResultQuoted, tweetLegacyQuoted, tweetUserQuoted)];
  1736. }
  1737.  
  1738. return result;
  1739. }
  1740.  
  1741. /* // dev only snippet (to extract params):
  1742. a = new URL(`https://x.com/i/api/graphql/VwKJcAd7zqlBOitPLUrB8A/TweetDetail?...`);
  1743. console.log("variables", JSON.stringify(JSON.parse(Object.fromEntries(a.searchParams).variables), null, " "))
  1744. console.log("features", JSON.stringify(JSON.parse(Object.fromEntries(a.searchParams).features), null, " "))
  1745. console.log("fieldToggles", JSON.stringify(JSON.parse(Object.fromEntries(a.searchParams).fieldToggles), null, " "))
  1746. */
  1747.  
  1748. // todo: keep `queryId` updated
  1749. // https://github.com/fa0311/TwitterInternalAPIDocument/blob/master/docs/json/API.json
  1750. static TweetDetailQueryId = "VwKJcAd7zqlBOitPLUrB8A"; // TweetDetail (for videos)
  1751. static UserByScreenNameQueryId = "qW5u-DAuXpMEG0zA1F7UGQ"; // UserByScreenName (for the direct user profile url)
  1752.  
  1753. static createTweetJsonEndpointUrl(tweetId) {
  1754. const variables = {
  1755. "focalTweetId": tweetId,
  1756. "with_rux_injections": true,
  1757. "includePromotedContent": true,
  1758. "withCommunity": true,
  1759. "withQuickPromoteEligibilityTweetFields": true,
  1760. "withBirdwatchNotes": true,
  1761. "withVoice": true,
  1762. "withV2Timeline": true
  1763. };
  1764. const features = {
  1765. "rweb_tipjar_consumption_enabled": true,
  1766. "responsive_web_graphql_exclude_directive_enabled": true,
  1767. "verified_phone_label_enabled": false,
  1768. "creator_subscriptions_tweet_preview_api_enabled": true,
  1769. "responsive_web_graphql_timeline_navigation_enabled": true,
  1770. "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
  1771. "communities_web_enable_tweet_community_results_fetch": true,
  1772. "c9s_tweet_anatomy_moderator_badge_enabled": true,
  1773. "articles_preview_enabled": true,
  1774. "tweetypie_unmention_optimization_enabled": true,
  1775. "responsive_web_edit_tweet_api_enabled": true,
  1776. "graphql_is_translatable_rweb_tweet_is_translatable_enabled": true,
  1777. "view_counts_everywhere_api_enabled": true,
  1778. "longform_notetweets_consumption_enabled": true,
  1779. "responsive_web_twitter_article_tweet_consumption_enabled": true,
  1780. "tweet_awards_web_tipping_enabled": false,
  1781. "creator_subscriptions_quote_tweet_preview_enabled": false,
  1782. "freedom_of_speech_not_reach_fetch_enabled": true,
  1783. "standardized_nudges_misinfo": true,
  1784. "tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled": true,
  1785. "rweb_video_timestamps_enabled": true,
  1786. "longform_notetweets_rich_text_read_enabled": true,
  1787. "longform_notetweets_inline_media_enabled": true,
  1788. "responsive_web_enhance_cards_enabled": false
  1789. };
  1790. const fieldToggles = {
  1791. "withArticleRichContentState": true,
  1792. "withArticlePlainText": false,
  1793. "withGrokAnalyze": false
  1794. };
  1795.  
  1796. const urlBase = `https://${sitename}.com/i/api/graphql/${API.TweetDetailQueryId}/TweetDetail`;
  1797. const urlObj = new URL(urlBase);
  1798. urlObj.searchParams.set("variables", JSON.stringify(variables));
  1799. urlObj.searchParams.set("features", JSON.stringify(features));
  1800. urlObj.searchParams.set("fieldToggles", JSON.stringify(fieldToggles));
  1801. const url = urlObj.toString();
  1802. return url;
  1803. }
  1804.  
  1805. static async getUserInfo(username) {
  1806. const variables = {
  1807. "screen_name": username,
  1808. "withSafetyModeUserFields": true,
  1809. };
  1810. const features = {
  1811. "creator_subscriptions_tweet_preview_api_enabled": true,
  1812. "hidden_profile_likes_enabled": true,
  1813. "hidden_profile_subscriptions_enabled": true,
  1814. "highlights_tweets_tab_ui_enabled": true,
  1815. "responsive_web_graphql_exclude_directive_enabled": true,
  1816. "responsive_web_graphql_skip_user_profile_image_extensions_enabled": false,
  1817. "responsive_web_graphql_timeline_navigation_enabled": true,
  1818. "responsive_web_twitter_article_notes_tab_enabled": true,
  1819. "rweb_tipjar_consumption_enabled": true,
  1820. "subscriptions_verification_info_is_identity_verified_enabled": true,
  1821. "subscriptions_verification_info_verified_since_enabled": true,
  1822. "verified_phone_label_enabled": false,
  1823. };
  1824. const fieldToggles = {
  1825. "withAuxiliaryUserLabels": false,
  1826. };
  1827.  
  1828. const urlBase = `https://${sitename}.com/i/api/graphql/${API.UserByScreenNameQueryId}/UserByScreenName?`;
  1829. const urlObj = new URL(urlBase);
  1830. urlObj.searchParams.set("variables", JSON.stringify(variables));
  1831. urlObj.searchParams.set("features", JSON.stringify(features));
  1832. urlObj.searchParams.set("fieldToggles", JSON.stringify(fieldToggles));
  1833. const url = urlObj.toString();
  1834.  
  1835. const json = await API.apiRequest(url);
  1836. verbose && console.log("[ujs][getUserInfo][json]", json);
  1837. return json.data.user.result.legacy.entities.url?.urls[0].expanded_url;
  1838. }
  1839. }
  1840.  
  1841. return API;
  1842. }
  1843.  
  1844. function getHistoryHelper() {
  1845. function migrateLocalStore() {
  1846. // 2023.07.05 // todo: uncomment after two+ months
  1847. // Currently I disable it for cases if some browser's tabs uses the old version of the script.
  1848. // const migrated = localStorage.getItem(StorageNames.migrated);
  1849. // if (migrated === "true") {
  1850. // return;
  1851. // }
  1852.  
  1853. const newToOldNameMap = [
  1854. [StorageNames.settings, StorageNamesOld.settings],
  1855. [StorageNames.settingsImageHistoryBy, StorageNamesOld.settingsImageHistoryBy],
  1856. [StorageNames.downloadedImageNames, StorageNamesOld.downloadedImageNames],
  1857. [StorageNames.downloadedImageTweetIds, StorageNamesOld.downloadedImageTweetIds],
  1858. [StorageNames.downloadedVideoTweetIds, StorageNamesOld.downloadedVideoTweetIds],
  1859. ];
  1860.  
  1861. /**
  1862. * @param {string} newName
  1863. * @param {string} oldName
  1864. * @param {string} value
  1865. */
  1866. function setValue(newName, oldName, value) {
  1867. try {
  1868. localStorage.setItem(newName, value);
  1869. } catch (err) {
  1870. localStorage.removeItem(oldName); // if there is no space ("exceeded the quota")
  1871. localStorage.setItem(newName, value);
  1872. }
  1873. localStorage.removeItem(oldName);
  1874. }
  1875.  
  1876. function mergeOldWithNew({newName, oldName}) {
  1877. const oldValueStr = localStorage.getItem(oldName);
  1878. if (oldValueStr === null) {
  1879. return;
  1880. }
  1881. const newValueStr = localStorage.getItem(newName);
  1882. if (newValueStr === null) {
  1883. setValue(newName, oldName, oldValueStr);
  1884. return;
  1885. }
  1886. try {
  1887. const oldValue = JSON.parse(oldValueStr);
  1888. const newValue = JSON.parse(newValueStr);
  1889. if (Array.isArray(oldValue) && Array.isArray(newValue)) {
  1890. const resultArray = [...new Set([...newValue, ...oldValue])];
  1891. const resultArrayStr = JSON.stringify(resultArray);
  1892. setValue(newName, oldName, resultArrayStr);
  1893. }
  1894. } catch (err) {
  1895. // return;
  1896. }
  1897. }
  1898.  
  1899. for (const [newName, oldName] of newToOldNameMap) {
  1900. mergeOldWithNew({newName, oldName});
  1901. }
  1902. // localStorage.setItem(StorageNames.migrated, "true");
  1903. }
  1904.  
  1905. function exportHistory(onDone) {
  1906. const exportObject = [
  1907. StorageNames.settings,
  1908. StorageNames.settingsImageHistoryBy,
  1909. StorageNames.downloadedImageNames, // only if "settingsImageHistoryBy" === "IMAGE_NAME" (by default)
  1910. StorageNames.downloadedImageTweetIds, // only if "settingsImageHistoryBy" === "TWEET_ID" (need to set manually with DevTools)
  1911. StorageNames.downloadedVideoTweetIds,
  1912. ].reduce((acc, name) => {
  1913. const valueStr = localStorage.getItem(name);
  1914. if (valueStr === null) {
  1915. return acc;
  1916. }
  1917. let value = JSON.parse(valueStr);
  1918. if (Array.isArray(value)) {
  1919. value = [...new Set(value)];
  1920. }
  1921. acc[name] = value;
  1922. return acc;
  1923. }, {});
  1924. const browserName = localStorage.getItem(StorageNames.browserName) || getBrowserName();
  1925. const browserLine = browserName ? "-" + browserName : "";
  1926.  
  1927. downloadBlob(new Blob([toLineJSON(exportObject, true)]), `ujs-twitter-click-n-save-export-${dateToDayDateString(new Date())}${browserLine}.json`);
  1928. onDone();
  1929. }
  1930.  
  1931. function verify(jsonObject) {
  1932. if (Array.isArray(jsonObject)) {
  1933. throw new Error("Wrong object! JSON contains an array.");
  1934. }
  1935. if (Object.keys(jsonObject).some(key => !key.startsWith("ujs-twitter-click-n-save"))) {
  1936. throw new Error("Wrong object! The keys should start with 'ujs-twitter-click-n-save'.");
  1937. }
  1938. }
  1939.  
  1940. function importHistory(onDone, onError) {
  1941. const importInput = document.createElement("input");
  1942. importInput.type = "file";
  1943. importInput.accept = "application/json";
  1944. importInput.style.display = "none";
  1945. document.body.prepend(importInput);
  1946. importInput.addEventListener("change", async _event => {
  1947. let json;
  1948. try {
  1949. json = JSON.parse(await importInput.files[0].text());
  1950. verify(json);
  1951.  
  1952. Object.entries(json).forEach(([key, value]) => {
  1953. if (Array.isArray(value)) {
  1954. value = [...new Set(value)];
  1955. }
  1956. localStorage.setItem(key, JSON.stringify(value));
  1957. });
  1958. onDone();
  1959. } catch (err) {
  1960. onError(err);
  1961. } finally {
  1962. await sleep(1000);
  1963. importInput.remove();
  1964. }
  1965. });
  1966. importInput.click();
  1967. }
  1968.  
  1969. function mergeHistory(onDone, onError) { // Only merges arrays
  1970. const mergeInput = document.createElement("input");
  1971. mergeInput.type = "file";
  1972. mergeInput.accept = "application/json";
  1973. mergeInput.style.display = "none";
  1974. document.body.prepend(mergeInput);
  1975. mergeInput.addEventListener("change", async _event => {
  1976. let json;
  1977. try {
  1978. json = JSON.parse(await mergeInput.files[0].text());
  1979. verify(json);
  1980. Object.entries(json).forEach(([key, value]) => {
  1981. if (!Array.isArray(value)) {
  1982. return;
  1983. }
  1984. const existedValue = JSON.parse(localStorage.getItem(key));
  1985. if (Array.isArray(existedValue)) {
  1986. const resultValue = [...new Set([...existedValue, ...value])];
  1987. localStorage.setItem(key, JSON.stringify(resultValue));
  1988. } else {
  1989. localStorage.setItem(key, JSON.stringify(value));
  1990. }
  1991. });
  1992. onDone();
  1993. } catch (err) {
  1994. onError(err);
  1995. } finally {
  1996. await sleep(1000);
  1997. mergeInput.remove();
  1998. }
  1999. });
  2000. mergeInput.click();
  2001. }
  2002.  
  2003. return {exportHistory, importHistory, mergeHistory, migrateLocalStore};
  2004. }
  2005.  
  2006. // ---------------------------------------------------------------------------------------------------------------------
  2007. // ---------------------------------------------------------------------------------------------------------------------
  2008. // --- Common Utils --- //
  2009.  
  2010. // --- LocalStorage util class --- //
  2011. function hoistLS(settings = {}) {
  2012. const {
  2013. verbose, // debug "messages" in the document.title
  2014. } = settings;
  2015.  
  2016. class LS {
  2017. constructor(name) {
  2018. this.name = name;
  2019. }
  2020. getItem(defaultValue) {
  2021. return LS.getItem(this.name, defaultValue);
  2022. }
  2023. setItem(value) {
  2024. LS.setItem(this.name, value);
  2025. }
  2026. removeItem() {
  2027. LS.removeItem(this.name);
  2028. }
  2029. async pushItem(value) { // array method
  2030. await LS.pushItem(this.name, value);
  2031. }
  2032. async popItem(value) { // array method
  2033. await LS.popItem(this.name, value);
  2034. }
  2035. hasItem(value) { // array method
  2036. return LS.hasItem(this.name, value);
  2037. }
  2038.  
  2039. static getItem(name, defaultValue) {
  2040. const value = localStorage.getItem(name);
  2041. if (value === undefined) {
  2042. return undefined;
  2043. }
  2044. if (value === null) { // when there is no such item
  2045. LS.setItem(name, defaultValue);
  2046. return defaultValue;
  2047. }
  2048. return JSON.parse(value);
  2049. }
  2050. static setItem(name, value) {
  2051. localStorage.setItem(name, JSON.stringify(value));
  2052. }
  2053. static removeItem(name) {
  2054. localStorage.removeItem(name);
  2055. }
  2056. static async pushItem(name, value) {
  2057. const array = LS.getItem(name, []);
  2058. array.push(value);
  2059. LS.setItem(name, array);
  2060.  
  2061. //sanity check
  2062. await sleep(50);
  2063. if (!LS.hasItem(name, value)) {
  2064. if (verbose) {
  2065. document.title = "🟥" + document.title;
  2066. }
  2067. await LS.pushItem(name, value);
  2068. }
  2069. }
  2070. static async popItem(name, value) { // remove from an array
  2071. const array = LS.getItem(name, []);
  2072. if (array.indexOf(value) !== -1) {
  2073. array.splice(array.indexOf(value), 1);
  2074. LS.setItem(name, array);
  2075.  
  2076. //sanity check
  2077. await sleep(50);
  2078. if (LS.hasItem(name, value)) {
  2079. if (verbose) {
  2080. document.title = "🟨" + document.title;
  2081. }
  2082. await LS.popItem(name, value);
  2083. }
  2084. }
  2085. }
  2086. static hasItem(name, value) { // has in array
  2087. const array = LS.getItem(name, []);
  2088. return array.indexOf(value) !== -1;
  2089. }
  2090. }
  2091.  
  2092. return LS;
  2093. }
  2094.  
  2095. // --- Just groups them in a function for the convenient code looking --- //
  2096. function getUtils({verbose}) {
  2097. function sleep(time) {
  2098. return new Promise(resolve => setTimeout(resolve, time));
  2099. }
  2100.  
  2101. async function fetchResource(url, onProgress = props => console.log(props)) {
  2102. try {
  2103. /** @type {Response} */
  2104. let response = await fetch(url, {
  2105. // cache: "force-cache",
  2106. });
  2107. const lastModifiedDateSeconds = response.headers.get("last-modified");
  2108. const contentType = response.headers.get("content-type");
  2109.  
  2110. const lastModifiedDate = dateToDayDateString(lastModifiedDateSeconds);
  2111. const extension = contentType ? extensionFromMime(contentType) : null;
  2112.  
  2113. if (onProgress) {
  2114. response = responseProgressProxy(response, onProgress);
  2115. }
  2116.  
  2117. const blob = await response.blob();
  2118.  
  2119. // https://pbs.twimg.com/media/AbcdEFgijKL01_9?format=jpg&name=orig -> AbcdEFgijKL01_9
  2120. // https://pbs.twimg.com/ext_tw_video_thumb/1234567890123456789/pu/img/Ab1cd2345EFgijKL.jpg?name=orig -> Ab1cd2345EFgijKL.jpg
  2121. // https://video.twimg.com/ext_tw_video/1234567890123456789/pu/vid/946x720/Ab1cd2345EFgijKL.mp4?tag=10 -> Ab1cd2345EFgijKL.mp4
  2122. const _url = new URL(url);
  2123. const {filename} = (_url.origin + _url.pathname).match(/(?<filename>[^\/]+$)/).groups;
  2124.  
  2125. const {name} = filename.match(/(?<name>^[^.]+)/).groups;
  2126. return {blob, lastModifiedDate, contentType, extension, name, status: response.status};
  2127. } catch (error) {
  2128. verbose && console.error("[ujs][fetchResource]", url);
  2129. verbose && console.error("[ujs][fetchResource]", error);
  2130. throw error;
  2131. }
  2132. }
  2133.  
  2134. function extensionFromMime(mimeType) {
  2135. let extension = mimeType.match(/(?<=\/).+/)[0];
  2136. extension = extension === "jpeg" ? "jpg" : extension;
  2137. return extension;
  2138. }
  2139.  
  2140. // the original download url will be posted as hash of the blob url, so you can check it in the download manager's history
  2141. function downloadBlob(blob, name, url) {
  2142. const anchor = document.createElement("a");
  2143. anchor.setAttribute("download", name || "");
  2144. const blobUrl = URL.createObjectURL(blob);
  2145. anchor.href = blobUrl + (url ? ("#" + url) : "");
  2146. anchor.click();
  2147. setTimeout(() => URL.revokeObjectURL(blobUrl), 30000);
  2148. }
  2149.  
  2150. // "Sun, 10 Jan 2021 22:22:22 GMT" -> "2021.01.10"
  2151. function dateToDayDateString(dateValue, utc = true) {
  2152. const _date = new Date(dateValue);
  2153. function pad(str) {
  2154. return str.toString().padStart(2, "0");
  2155. }
  2156. const _utc = utc ? "UTC" : "";
  2157. const year = _date[`get${_utc}FullYear`]();
  2158. const month = _date[`get${_utc}Month`]() + 1;
  2159. const date = _date[`get${_utc}Date`]();
  2160.  
  2161. return year + "." + pad(month) + "." + pad(date);
  2162. }
  2163.  
  2164. function addCSS(css) {
  2165. const styleElem = document.createElement("style");
  2166. styleElem.textContent = css;
  2167. document.body.append(styleElem);
  2168. return styleElem;
  2169. }
  2170.  
  2171. function getCookie(name) {
  2172. verbose && console.log("[ujs][getCookie]", document.cookie);
  2173. const regExp = new RegExp(`(?<=${name}=)[^;]+`);
  2174. return document.cookie.match(regExp)?.[0];
  2175. }
  2176.  
  2177. function throttle(runnable, time = 50) {
  2178. let waiting = false;
  2179. let queued = false;
  2180. let context;
  2181. let args;
  2182.  
  2183. return function() {
  2184. if (!waiting) {
  2185. waiting = true;
  2186. setTimeout(function() {
  2187. if (queued) {
  2188. runnable.apply(context, args);
  2189. context = args = undefined;
  2190. }
  2191. waiting = queued = false;
  2192. }, time);
  2193. return runnable.apply(this, arguments);
  2194. } else {
  2195. queued = true;
  2196. context = this;
  2197. args = arguments;
  2198. }
  2199. }
  2200. }
  2201.  
  2202. function throttleWithResult(func, time = 50) {
  2203. let waiting = false;
  2204. let args;
  2205. let context;
  2206. let timeout;
  2207. let promise;
  2208.  
  2209. return async function() {
  2210. if (!waiting) {
  2211. waiting = true;
  2212. timeout = new Promise(async resolve => {
  2213. await sleep(time);
  2214. waiting = false;
  2215. resolve();
  2216. });
  2217. return func.apply(this, arguments);
  2218. } else {
  2219. args = arguments;
  2220. context = this;
  2221. }
  2222.  
  2223. if (!promise) {
  2224. promise = new Promise(async resolve => {
  2225. await timeout;
  2226. const result = func.apply(context, args);
  2227. args = context = promise = undefined;
  2228. resolve(result);
  2229. });
  2230. }
  2231. return promise;
  2232. }
  2233. }
  2234.  
  2235. function xpath(path, node = document) {
  2236. let xPathResult = document.evaluate(path, node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  2237. return xPathResult.singleNodeValue;
  2238. }
  2239. function xpathAll(path, node = document) {
  2240. let xPathResult = document.evaluate(path, node, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
  2241. const nodes = [];
  2242. try {
  2243. let node = xPathResult.iterateNext();
  2244.  
  2245. while (node) {
  2246. nodes.push(node);
  2247. node = xPathResult.iterateNext();
  2248. }
  2249. return nodes;
  2250. } catch (err) {
  2251. // todo need investigate it
  2252. console.error(err); // "The document has mutated since the result was returned."
  2253. return [];
  2254. }
  2255. }
  2256.  
  2257. const identityContentEncodings = new Set([null, "identity", "no encoding"]);
  2258. function getOnProgressProps(response) {
  2259. const {headers, status, statusText, url, redirected, ok} = response;
  2260. const isIdentity = identityContentEncodings.has(headers.get("Content-Encoding"));
  2261. const compressed = !isIdentity;
  2262. const _contentLength = parseInt(headers.get("Content-Length")); // `get()` returns `null` if no header present
  2263. const contentLength = isNaN(_contentLength) ? null : _contentLength;
  2264. const lengthComputable = isIdentity && _contentLength !== null;
  2265.  
  2266. // Original XHR behaviour; in TM it equals to `contentLength`, or `-1` if `contentLength` is `null` (and `0`?).
  2267. const total = lengthComputable ? contentLength : 0;
  2268. const gmTotal = contentLength > 0 ? contentLength : -1; // Like `total` is in TM and GM.
  2269.  
  2270. return {
  2271. gmTotal, total, lengthComputable,
  2272. compressed, contentLength,
  2273. headers, status, statusText, url, redirected, ok
  2274. };
  2275. }
  2276. function responseProgressProxy(response, onProgress) {
  2277. const onProgressProps = getOnProgressProps(response);
  2278. let loaded = 0;
  2279. const reader = response.body.getReader();
  2280. const readableStream = new ReadableStream({
  2281. async start(controller) {
  2282. while (true) {
  2283. const {done, /** @type {Uint8Array} */ value} = await reader.read();
  2284. if (done) {
  2285. break;
  2286. }
  2287. loaded += value.length;
  2288. try {
  2289. onProgress({loaded, ...onProgressProps});
  2290. } catch (err) {
  2291. console.error("[ujs][onProgress]:", err);
  2292. }
  2293. controller.enqueue(value);
  2294. }
  2295. controller.close();
  2296. reader.releaseLock();
  2297. },
  2298. cancel() {
  2299. void reader.cancel();
  2300. }
  2301. });
  2302. return new ResponseEx(readableStream, response);
  2303. }
  2304. class ResponseEx extends Response {
  2305. [Symbol.toStringTag] = "ResponseEx";
  2306.  
  2307. constructor(body, {headers, status, statusText, url, redirected, type, ok}) {
  2308. super(body, {
  2309. status, statusText, headers: {
  2310. ...headers,
  2311. "content-type": headers.get("content-type")?.split("; ")[0] // Fixes Blob type ("text/html; charset=UTF-8") in TM
  2312. }
  2313. });
  2314. this._type = type;
  2315. this._url = url;
  2316. this._redirected = redirected;
  2317. this._ok = ok;
  2318. this._headers = headers; // `HeadersLike` is more user-friendly for debug than the original `Headers` object
  2319. }
  2320. get redirected() { return this._redirected; }
  2321. get url() { return this._url; }
  2322. get type() { return this._type || "basic"; }
  2323. get ok() { return this._ok; }
  2324. /** @returns {Headers} - `Headers`-like object */
  2325. get headers() { return this._headers; }
  2326. }
  2327.  
  2328. function toLineJSON(object, prettyHead = false) {
  2329. let result = "{\n";
  2330. const entries = Object.entries(object);
  2331. const length = entries.length;
  2332. if (prettyHead && length > 0) {
  2333. result += `"${entries[0][0]}":${JSON.stringify(entries[0][1], null, " ")}`;
  2334. if (length > 1) {
  2335. result += `,\n\n`;
  2336. }
  2337. }
  2338. for (let i = 1; i < length - 1; i++) {
  2339. result += `"${entries[i][0]}":${JSON.stringify(entries[i][1])},\n`;
  2340. }
  2341. if (length > 0 && !prettyHead || length > 1) {
  2342. result += `"${entries[length - 1][0]}":${JSON.stringify(entries[length - 1][1])}`;
  2343. }
  2344. result += `\n}`;
  2345. return result;
  2346. }
  2347.  
  2348. const isFirefox = navigator.userAgent.toLowerCase().indexOf("firefox") !== -1;
  2349.  
  2350. function getBrowserName() {
  2351. const userAgent = window.navigator.userAgent.toLowerCase();
  2352. return userAgent.indexOf("edge") > -1 ? "edge-legacy"
  2353. : userAgent.indexOf("edg") > -1 ? "edge"
  2354. : userAgent.indexOf("opr") > -1 && !!window.opr ? "opera"
  2355. : userAgent.indexOf("chrome") > -1 && !!window.chrome ? "chrome"
  2356. : userAgent.indexOf("firefox") > -1 ? "firefox"
  2357. : userAgent.indexOf("safari") > -1 ? "safari"
  2358. : "";
  2359. }
  2360.  
  2361. function removeSearchParams(url) {
  2362. const urlObj = new URL(url);
  2363. const keys = []; // FF + VM fix // Instead of [...urlObj.searchParams.keys()]
  2364. urlObj.searchParams.forEach((v, k) => { keys.push(k); });
  2365. for (const key of keys) {
  2366. urlObj.searchParams.delete(key);
  2367. }
  2368. return urlObj.toString();
  2369. }
  2370.  
  2371. return {
  2372. sleep, fetchResource, extensionFromMime, downloadBlob, dateToDayDateString,
  2373. addCSS,
  2374. getCookie,
  2375. throttle, throttleWithResult,
  2376. xpath, xpathAll,
  2377. responseProgressProxy,
  2378. toLineJSON,
  2379. isFirefox,
  2380. getBrowserName,
  2381. removeSearchParams,
  2382. }
  2383. }
  2384.  
  2385. // ---------------------------------------------------------------------------------------------------------------------
  2386. // ---------------------------------------------------------------------------------------------------------------------