Greasyfork in your language

Whenever a link to localized greasyfork page is clicked, redirect it to the specified language

目前为 2018-06-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Greasyfork in your language
  3. // @description Whenever a link to localized greasyfork page is clicked, redirect it to the specified language
  4. // @namespace wOxxOm.scripts
  5. // @author wOxxOm
  6. // @version 3.0.4
  7. // @match https://greasyfork.org/*
  8. // @exclude https://greasyfork.org/system/*
  9. // @run-at document-start
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // ==/UserScript==
  13. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  14.  
  15. var language = GM_getValue('language', 'en');
  16.  
  17. maybeRedirect(location);
  18.  
  19. window.addEventListener('load', function _() {
  20. window.removeEventListener('load', _);
  21. var _timer, _title;
  22. document.getElementById('language-selector-locale').addEventListener('change', function() {
  23. GM_setValue('language', this.value);
  24. _title = _title || this.title;
  25. this.title = this.value + ' saved in ' + GM_info.script.name;
  26. clearTimeout(_timer);
  27. _timer = setTimeout(function() {
  28. this.title = _title;
  29. _title = null;
  30. }, 5000);
  31. });
  32. });
  33.  
  34. window.addEventListener('mousedown', function(e) {
  35. var a = e.target.closest('a');
  36. if (a &&
  37. a.origin == 'https://greasyfork.org' &&
  38. a.pathname.lastIndexOf('/system/', 0) < 0 &&
  39. !a.pathname.match(/\/code\/.*?\.user\.js/))
  40. maybeRedirect(a);
  41. });
  42.  
  43. function maybeRedirect(url) {
  44. var m = url.pathname.match(/^(?:\/(\w\w(?:-\w\w)?)(?:\/|$))?(.*)$/i);
  45. if (m[1] !== language) {
  46. var path = '/' + language + '/' + m[2].replace(/[?&]locale_override[^&]*/, '').replace(/^\//, '');
  47. url.href = url.origin + path +
  48. (path.indexOf('/forum/') > 0 ? '' : (path.indexOf('?') > 0 ? '&' : '?') + 'locale_override=1');
  49. console.log('Redirected greasyfork url language from %s to %s', m[1], language);
  50. }
  51. }