Greasyfork in your language

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

当前为 2017-05-30 提交的版本,查看 最新版本

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