UserAPI Link Redirector

Redirect UserAPI links to a formatted version (single redirect)

目前为 2024-01-04 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name UserAPI Link Redirector
  3. // @version 0.6
  4. // @description Redirect UserAPI links to a formatted version (single redirect)
  5. // @match https://*.userapi.com/*
  6. // @namespace https://greasyfork.org/users/789838
  7. // ==/UserScript==
  8.  
  9. (function() {
  10. 'use strict';
  11.  
  12. // Функция для форматирования и переадресации ссылки
  13. function redirectUserAPILink(link) {
  14. // Регулярное выражение для извлечения нужных параметров из ссылки
  15. var regex = /https:\/\/.*?\/impg\/(.*?)(?:\/(.*?))?\?.*$/;
  16. var matches = link.match(regex);
  17.  
  18. if (matches && matches.length >= 2) {
  19. // Формирование новой ссылки
  20. var newLink = 'https://pp.userapi.com/' + matches[1];
  21. if (matches[2]) {
  22. newLink += '/' + matches[2];
  23. }
  24. console.log('Redirecting to:', newLink);
  25.  
  26. // Проверяем, была ли уже выполнена переадресация
  27. if (!window.location.redirected) {
  28. window.location.replace(newLink);
  29. // Устанавливаем флаг, чтобы избежать повторной переадресации
  30. window.location.redirected = true;
  31. }
  32. } else {
  33. console.log('Unable to format link:', link);
  34. }
  35. }
  36.  
  37. // Переадресуем текущую страницу, если она соответствует фильтру
  38. redirectUserAPILink(window.location.href);
  39. })();