Get data url

input resource url and get data url

  1. // ==UserScript==
  2. // @name Get data url
  3. // @namespace https://blog.bgme.me
  4. // @match *://*/*
  5. // @run-at document-idle
  6. // @version 1.0
  7. // @author bgme
  8. // @description input resource url and get data url
  9. // @supportURL https://github.com/yingziwu/Greasemonkey/issues
  10. // @icon -
  11. // @license AGPL-3.0-or-later
  12. // ==/UserScript==
  13.  
  14. "use strict";
  15.  
  16. unsafeWindow.toDataURL = toDataURL;
  17. unsafeWindow.getDataURL = getDataURL;
  18.  
  19. // https://stackoverflow.com/questions/934012/get-image-data-url-in-javascript/42916772#42916772
  20. function toDataURL(url, callback) {
  21. GM.xmlHttpRequest({
  22. method: "GET",
  23. url: url,
  24. responseType: "blob",
  25. onload(response) {
  26. var fr = new FileReader();
  27.  
  28. fr.onload = function () {
  29. callback(this.result);
  30. };
  31.  
  32. fr.readAsDataURL(response.response);
  33. },
  34. });
  35. }
  36.  
  37. function getDataURL(url) {
  38. toDataURL(url, function (dataurl) {
  39. console.log(dataurl);
  40. });
  41. }

QingJ © 2025

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