GIPHY legacy gifmaker export to GIF

Dump canvas to GIF

  1. // ==UserScript==
  2. // @name GIPHY legacy gifmaker export to GIF
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.1
  5. // @description Dump canvas to GIF
  6. // @author CODEX & RZR1911
  7. // @match https://giphy.com/create/gifmaker*
  8. // @grant none
  9. // @require https://cdn.jsdelivr.net/npm/gif.js@0.2.0/dist/gif.min.js
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function GetGiphyLegacyCanvas() {
  17. const ReactCanvas = document.getElementsByClassName('LseTRCTVuhsl6arwQUvpF')[0];
  18. if (!ReactCanvas) {
  19. console.error(`Failed to find React canvas for legacy gif maker.`);
  20. return null;
  21. }
  22.  
  23. const FiberKey = Object.keys(ReactCanvas).find(key => key.startsWith("__reactFiber$"));
  24. if (!FiberKey) {
  25. console.error(`Failed to find react fiber key for ${ReactCanvas}`);
  26. return null;
  27. }
  28.  
  29. let FiberNode = ReactCanvas[FiberKey];
  30. while (FiberNode) {
  31. if (FiberNode.stateNode && typeof FiberNode.stateNode.getWrappedComponent === "function") {
  32. return FiberNode.stateNode.getWrappedComponent();
  33. }
  34. FiberNode = FiberNode.return;
  35. }
  36.  
  37. console.error(`Failed to find canvas from react fibernode ${ReactCanvas[FiberKey]}`);
  38. return null;
  39. }
  40.  
  41. function createGIF(framesArray) {
  42. const firstFrame = framesArray[0];
  43. const gif = new GIF({
  44. workers: 4,
  45. quality: 8,
  46. width: firstFrame.data.width,
  47. height: firstFrame.data.height,
  48. workerScript: '/static/public/workers/gif.worker.js',
  49. });
  50.  
  51. framesArray.forEach(frame => {
  52. console.log(frame);
  53. gif.addFrame(frame.data, {
  54. delay: frame.delay,
  55. copy: true
  56. });
  57. });
  58.  
  59. gif.on('finished', function(blob) {
  60. console.log('finished');
  61. const url = URL.createObjectURL(blob);
  62. const link = document.createElement('a');
  63. link.href = url;
  64. link.download = 'giphy_export.gif';
  65. link.click();
  66. URL.revokeObjectURL(url);
  67. });
  68.  
  69. gif.render();
  70. console.log('rendering');
  71. }
  72.  
  73. function ExportGif() {
  74. const canvasComponent = GetGiphyLegacyCanvas();
  75. if (!canvasComponent) {
  76. console.error('Could not find Giphy canvas component');
  77. return;
  78. }
  79.  
  80. const frames = canvasComponent.getFrames();
  81. if (!frames || !Array.isArray(frames)) {
  82. console.error('Failed to get frames Array from canvas');
  83. return;
  84. }
  85.  
  86. createGIF(frames);
  87. }
  88.  
  89. function observeButton() {
  90. const observer = new MutationObserver((mutations, obs) => {
  91. const buttons = document.querySelectorAll('._6WZyciPmD3H_QZtPwGaAq._1Fba10Vcpc4_UBtLy_oMYy');
  92. if (buttons.length > 1 && !buttons[1].textContent.includes('Export GIF')) {
  93. const button = buttons[1];
  94. console.log('Second button found', button);
  95.  
  96. const newButton = button.cloneNode(true);
  97. newButton.classList.remove("_3HPxzg225YhJydf2_YUlBl");
  98. newButton.textContent = 'Export GIF :D';
  99. newButton.onclick = ExportGif;
  100.  
  101. button.parentNode.replaceChild(newButton, button);
  102. }
  103. });
  104.  
  105. observer.observe(document.body, { childList: true, subtree: true });
  106. }
  107.  
  108. observeButton();
  109. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址