Weblate.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ==UserScript==
  2. // @name New script
  3. // @namespace Violentmonkey Scripts
  4. // @match https://hosted.weblate.org/projects/ffupdater/*
  5. // @grant GM_xmlhttpRequest
  6. // @version 1.0
  7. // @author -
  8. // @description 02/02/2023, 10:54:50
  9. // ==/UserScript==
  10. function getDateOfLastFFUpdaterRelease() {
  11. return new Promise(resolve => {
  12. let control = GM_xmlhttpRequest({
  13. url: 'https://api.github.com/repos/Tobi823/ffupdater/releases/latest',
  14. method: 'GET',
  15. responseType: 'json',
  16. onload: function (rq) {
  17. console.log(`Request result: ${rq}`);
  18. let latestRelease = JSON.parse(rq.responseText);
  19. console.log(`JSON: ${latestRelease}`);
  20. let date = latestRelease['created_at'].split('T')[0];
  21. console.log(`Latest FFUpdater release date: ${latestRelease}`);
  22. resolve(date);
  23. },
  24. onerror: function (e) {
  25. console.error(e);
  26. }
  27. })
  28. });
  29. }
  30. function getContributorStats(startDate) {
  31. return new Promise(resolve => {
  32. let formData = new FormData();
  33. let csrf = document.getElementsByName('csrfmiddlewaretoken')[0].value;
  34. console.log(`CSRF: ${csrf}`);
  35. formData.append('csrfmiddlewaretoken', csrf);
  36. formData.append('style', 'json');
  37. formData.append('period', '');
  38. formData.append('start_date', startDate);
  39. formData.append('end_date', new Date().toISOString().split('T')[0]);
  40. let control = GM_xmlhttpRequest({
  41. url: 'https://hosted.weblate.org/counts/ffupdater/',
  42. method: 'POST',
  43. headers: {
  44. 'Referer': 'https://hosted.weblate.org/'
  45. },
  46. data: formData,
  47. responseType: 'json',
  48. onload: function (rq) {
  49. console.log(`Request result: ${rq}`);
  50. let users = JSON.parse(rq.responseText);
  51. console.log(`JSON: ${users}`);
  52. let userNames = users.sort((a, b) => b['t_words'] - a['t_words'])
  53. .filter(user => user['name'] !== 'Tobias Hellmann')
  54. .map(user => user['name']);
  55. console.log(`Users: ${userNames}`);
  56. let lastUserName = userNames.pop();
  57. let userNamesAsOneString = `${userNames.join(', ')} and ${lastUserName}`;
  58. let output = `Thanks for the Weblate contributions from: ${userNamesAsOneString}`;
  59. console.log(output);
  60. resolve(output);
  61. },
  62. onerror: function (e) {
  63. console.error(e);
  64. }
  65. });
  66. });
  67. }
  68. const button = document.createElement("button");
  69. button.innerText = "Generate text for changelog";
  70. button.addEventListener("click", async () => {
  71. let startDate = await getDateOfLastFFUpdaterRelease();
  72. let contributors = await getContributorStats(startDate);
  73. const span = document.createElement("span");
  74. span.innerText = contributors;
  75. document.getElementById("reports").append(span);
  76. });
  77. document.getElementById("reports").append(button)