Persistent Invidious Settings

Makes Invidious settings persist across instances and in private browsing.

当前为 2023-04-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Persistent Invidious Settings
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.20
  5. // @description Makes Invidious settings persist across instances and in private browsing.
  6. // @author Veeno
  7. // @license GPLv3
  8. // @connect api.invidious.io
  9. // @match https://*/*
  10. // @icon https://invidious.io/invidious-colored-vector.svg
  11. // @run-at document-start
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_xmlhttpRequest
  15. // ==/UserScript==
  16.  
  17. /* jshint esversion: 11 */
  18.  
  19. (() => {
  20. 'use strict';
  21.  
  22. if(window.self !== window.top) return;
  23.  
  24. function execute(domainUpToDate){
  25. const domain = location.hostname;
  26.  
  27. if(!Object.hasOwn(domainUpToDate, domain)) return;
  28.  
  29. function validateCurrentDomain(){
  30. domainUpToDate[domain] = true;
  31. GM_setValue("Invidious_DomainUpToDate", domainUpToDate);
  32. }
  33.  
  34. function invalidateOtherDomains(){
  35. Object.keys(domainUpToDate).forEach(key => { domainUpToDate[key] = false; });
  36. validateCurrentDomain();
  37. }
  38.  
  39. const storedSettings = GM_getValue(
  40. "Invidious_Settings",
  41. encodeURIComponent(JSON.stringify({
  42. annotations: false,
  43. annotations_subscribed: false,
  44. autoplay: true,
  45. automatic_instance_redirect: false,
  46. captions: ["", "", ""],
  47. comments: ["youtube", ""],
  48. continue: false,
  49. continue_autoplay: true,
  50. dark_mode: "",
  51. latest_only: false,
  52. listen: false,
  53. local: false,
  54. watch_history: false,
  55. vr_mode: true,
  56. show_nick: false,
  57. locale: "en-US",
  58. region: "US",
  59. max_results: 40,
  60. notifications_only: false,
  61. player_style: "invidious",
  62. quality: "hd720",
  63. quality_dash: "auto",
  64. default_home: "Popular",
  65. feed_menu: ["Popular", "Trending"],
  66. related_videos: true,
  67. sort: "published",
  68. speed: 1,
  69. thin_mode: false,
  70. unseen_only: false,
  71. video_loop: false,
  72. extend_desc: false,
  73. volume: 100,
  74. save_player_pos: false
  75. }))
  76. );
  77.  
  78. const cookieSettings = document.cookie
  79. .split("; ")
  80. .find(entry => entry.startsWith("PREFS="))
  81. ?.slice(6);
  82.  
  83. if(cookieSettings && domainUpToDate[domain]){
  84. if(cookieSettings !== storedSettings){
  85. GM_setValue("Invidious_Settings", cookieSettings);
  86. invalidateOtherDomains();
  87. }
  88. } else{
  89. const date = new Date();
  90. date.setFullYear(date.getFullYear() + 2);
  91. document.cookie = "PREFS=" + storedSettings + "; domain=" + domain + "; path=/; expires=" + date.toGMTString() + "; Secure; SameSite=Lax";
  92. validateCurrentDomain();
  93. location.reload();
  94. }
  95. }
  96.  
  97. const now = (new Date()).getTime();
  98.  
  99. if(now - GM_getValue("Invidious_InstancesUpdatedAt", 0) > 172800000){
  100. GM_xmlhttpRequest({
  101. method: "GET",
  102. url: "https://api.invidious.io/instances.json",
  103. responseType: "json",
  104. onload: function(response){
  105. execute((() => {
  106. const storedDomainUpToDate = GM_getValue("Invidious_DomainUpToDate", {});
  107. try{
  108. const domainUpToDate = Object.fromEntries(
  109. response.response
  110. .filter(instance => instance[1].type === "https")
  111. .map(instance => [instance[0], storedDomainUpToDate[instance[0]] || false])
  112. );
  113. GM_setValue("Invidious_DomainUpToDate", domainUpToDate);
  114. GM_setValue("Invidious_InstancesUpdatedAt", now);
  115. return domainUpToDate;
  116. } catch(e){
  117. console.error("Error parsing Invidious instances", e, response);
  118. return storedDomainUpToDate;
  119. }
  120. })());
  121. },
  122. onerror: function(response){
  123. console.error("Error loading Invidious instances", response);
  124. }
  125. });
  126. } else{
  127. execute(GM_getValue("Invidious_DomainUpToDate", {}));
  128. }
  129. })();