Medium Freedium Button

Adds a "Freedium" button to Medium posts

  1. // ==UserScript==
  2. // @name Medium Freedium Button
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Adds a "Freedium" button to Medium posts
  6. // @author lmdw
  7. // @match *://medium.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. let found = false; // Biến để theo dõi xem đã tìm thấy bài đăng hay chưa
  15. let url = window.location.href; // Lưu URL ban đầu
  16. let checkInterval; // Biến để lưu interval ID
  17. let timeout; // Biến để lưu timeout ID
  18. let metaTagRef = null; // Lưu trữ tham chiếu đến tag meta (tối ưu hóa)
  19. let freediumButton = null; // Biến để lưu tham chiếu đến nút Freedium
  20.  
  21. // Function to create and append the Freedium button
  22. function createFreediumButton() {
  23. // Create the button element
  24. freediumButton = document.createElement('button');
  25. freediumButton.textContent = 'Freedium';
  26.  
  27. // Style the button to match Medium's design (example styles - adjust as needed)
  28. freediumButton.style.position = 'fixed';
  29. freediumButton.style.top = '50%'; // Position vertically centered
  30. freediumButton.style.right = '10px'; // Position near the scrollbar
  31. freediumButton.style.transform = 'translateY(-50%)'; // Correct vertical centering
  32. freediumButton.style.backgroundColor = '#292929';
  33. freediumButton.style.color = '#fff';
  34. freediumButton.style.border = 'none';
  35. freediumButton.style.padding = '10px 15px';
  36. freediumButton.style.borderRadius = '4px';
  37. freediumButton.style.cursor = 'pointer';
  38. freediumButton.style.zIndex = '9999'; // Ensure it's on top of other elements
  39. freediumButton.style.fontFamily = 'sohne, "Helvetica Neue", Helvetica, Arial, sans-serif'; // Medium's font
  40. freediumButton.style.fontSize = '14px';
  41.  
  42. // Add opacity and hover effect
  43. freediumButton.style.opacity = '0.4'; // Initial opacity (more transparent)
  44. freediumButton.style.transition = 'opacity 0.2s ease-in-out'; // Smooth transition
  45.  
  46. freediumButton.addEventListener('mouseover', function() {
  47. freediumButton.style.opacity = '1'; // Fully opaque on hover
  48. freediumButton.style.backgroundColor = '#3e3e3e';
  49. });
  50. freediumButton.addEventListener('mouseout', function() {
  51. freediumButton.style.opacity = '0.6'; // Return to original opacity
  52. freediumButton.style.backgroundColor = '#292929';
  53. });
  54.  
  55.  
  56. // Add the click event listener to redirect to Freedium
  57. freediumButton.addEventListener('click', function() {
  58. window.location.href = 'https://freedium.cfd/' + window.location.href;
  59. });
  60.  
  61. // Append the button to the body
  62. document.body.appendChild(freediumButton);
  63. }
  64.  
  65. function checkAndPrint() {
  66. if (!metaTagRef) {
  67. metaTagRef = document.querySelector('meta[data-rh="true"][property="og:type"][content="article"]');
  68. }
  69.  
  70. if (metaTagRef) {
  71. // In thông báo nổi bật bằng CSS
  72. console.log('%cFound Medium post', 'color: white; background-color: green; font-size: 16px; padding: 5px;');
  73. createFreediumButton();
  74. found = true;
  75. clearInterval(checkInterval);
  76. clearTimeout(timeout);
  77.  
  78. }
  79. }
  80.  
  81. function notFound() {
  82. if (!found) {
  83. console.log('%cNot found Medium post', 'color: white; background-color: red; font-size: 16px; padding: 5px;');
  84. clearInterval(checkInterval);
  85. found = true;
  86. }
  87. }
  88.  
  89. function startChecking() {
  90. // Xóa nút Freedium nếu nó tồn tại
  91. if (freediumButton) {
  92. freediumButton.remove();
  93. freediumButton = null;
  94. }
  95.  
  96. found = false;
  97. metaTagRef = null; // Reset tham chiếu tag meta
  98. clearInterval(checkInterval);
  99. clearTimeout(timeout);
  100.  
  101. checkInterval = setInterval(checkAndPrint, 1000);
  102.  
  103. timeout = setTimeout(notFound, 5000);
  104. }
  105.  
  106. // Kiểm tra khi trang được tải xong hoàn toàn (load event)
  107. window.addEventListener('load', function() {
  108. startChecking();
  109. });
  110.  
  111. // Theo dõi thay đổi URL (giảm tần suất kiểm tra)
  112. setInterval(function() {
  113. if (window.location.href !== url) {
  114. url = window.location.href;
  115. startChecking();
  116. }
  117. }, 1000);
  118.  
  119. window.addEventListener('beforeunload', function (e) {
  120. clearInterval(checkInterval);
  121. clearTimeout(timeout);
  122. });
  123.  
  124. })();