Upload image to Mobilism Images

Add button to direct image pages to upload Mobilism, resizing if the image is too large.

  1. // ==UserScript==
  2. // @name Upload image to Mobilism Images
  3. // @namespace https://github.com/AbdurazaaqMohammed
  4. // @version 1.2
  5. // @description Add button to direct image pages to upload Mobilism, resizing if the image is too large.
  6. // @author Abdurazaaq Mohammed
  7. // @match https://images.mobilism.org/
  8. // @match https://images.mobilism.org/thumbnail.php
  9. // @match https://*/*.png
  10. // @match https://*/*.jpg
  11. // @match https://*/*.jpeg
  12. // @match https://*/*.gif
  13. // @match https://*/*.bmp
  14. // @match https://*/*.psd
  15. // @match http://*/*.png
  16. // @match http://*/*.jpg
  17. // @match http://*/*.jpeg
  18. // @match http://*/*.gif
  19. // @match http://*/*.bmp
  20. // @match http://*/*.psd
  21. // @grant GM_setClipboard
  22. // @grant GM_setValue
  23. // @grant GM_getValue
  24. // @homepage https://github.com/AbdurazaaqMohammed/userscripts
  25. // @supportURL https://github.com/AbdurazaaqMohammed/userscripts/issues
  26. // @license The Unlicense
  27. // ==/UserScript==
  28.  
  29. (function() {
  30. 'use strict';
  31. const url = window.location.href;
  32. const mobilism = 'https://images.mobilism.org/';
  33.  
  34. function addButton() {
  35. var container = document.createElement('div');
  36. container.style.position = 'fixed';
  37. container.style.top = '10px';
  38. container.style.left = '10px';
  39.  
  40. var button = document.createElement('button');
  41. button.innerText = 'Upload Image to Mobilism';
  42. button.addEventListener('click', goToMobilism);
  43.  
  44. container.appendChild(button);
  45. document.body.appendChild(container);
  46. }
  47.  
  48. function goToMobilism() {
  49. const image = document.querySelector('img');
  50. const h = image.naturalHeight;
  51. const w = image.naturalWidth;
  52. checkImageSize();
  53. if (needsResize(h, w)) {
  54. setNewRes(h, w, h/w)
  55. window.location.href = 'https://ezgif.com/resize?url=' + url;
  56. }
  57. else if (GM_getValue('needsCompress')) {
  58. window.location.href = 'https://ezgif.com/optimize?url=' + url;
  59. }
  60. else {
  61. GM_setValue('u', url);
  62. window.location.href = mobilism;
  63. }
  64. }
  65.  
  66. function uploadImg() {
  67. document.getElementById('imgUrl').value = GM_getValue('u');
  68. document.querySelector('#uploadbutton').click();
  69. }
  70.  
  71. function copyImageLink() {
  72. const link = document.querySelector('#codelbb')
  73. GM_setClipboard(link.value);
  74. link.click();
  75. link.click();
  76. }
  77.  
  78. function checkImageSize() {
  79. fetch(url)
  80. .then(response => {
  81. if (response.ok) {
  82. return response;
  83. }
  84. throw new Error('Network response was not ok.');
  85. })
  86. .then(response => {
  87. // Access the Content-Length header to get the file size
  88. const contentLength = response.headers.get('Content-Length');
  89. if (contentLength > 4000000) GM_setValue('needsCompress', true);
  90. })
  91. .catch(error => console.error('Error fetching image:', error));
  92. }
  93.  
  94. function needsResize(h, w) {
  95. return h>2499 || w>2499;
  96. }
  97.  
  98. function setNewRes(height, width, aspectRatio) {
  99. if (height > width) {
  100. height = 2499;
  101. width = Math.round(height / aspectRatio);
  102. } else {
  103. width = 2499;
  104. height = Math.round(width * aspectRatio);
  105. }
  106. GM_setValue('newHeight', height);
  107. GM_setValue('newWidth', width);
  108. }
  109.  
  110. if (url.endsWith(mobilism)) window.addEventListener('load', uploadImg);
  111. else if (url.endsWith('.php')) window.addEventListener('load', copyImageLink);
  112. else if (url.startsWith('https://ezgif.com/resize')) {
  113. document.querySelector(".new-height.number.text").value = GM_getValue('newHeight');
  114. document.querySelector(".new-width.number.text").value = GM_getValue('newWidth');
  115. document.getElementById('ar').selectedIndex = 2;
  116. }
  117. else if (url.includes('ezgif.com/tmp/') || url.startsWith('https://f-droid.org/')) goToMobilism();
  118. else addButton();
  119. })();