web.telegram下载被禁止下载的图片

点开图片向左上角添加一个下载按钮. GPT NB!

目前为 2023-04-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name web.telegram下载被禁止下载的图片
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 点开图片向左上角添加一个下载按钮. GPT NB!
  6. // @author ohy
  7. // @match https://web.telegram.org/k/
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=telegram.org
  9. // @grant GM_registerMenuCommand
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13.  
  14. (function() {
  15.  
  16. const downloadBtn = document.createElement('button');
  17. downloadBtn.className = 'btn-icon tgico-download';
  18.  
  19. // 给下载按钮添加一个点击事件监听器
  20. downloadBtn.addEventListener('click', () => {
  21. const xhr = new XMLHttpRequest();
  22. xhr.open("GET", window.globalVar, true);
  23. xhr.responseType = "blob";
  24. xhr.onload = function(e) {
  25. if (this.status === 200) {
  26. const blob = this.response;
  27. const img = document.createElement("img");
  28. img.src = URL.createObjectURL(blob);
  29. const a = document.createElement('a');
  30. const randomValue = Math.random();
  31. const fileName = `${randomValue}.jpg`;
  32. a.href = img.src;
  33. a.download = fileName;
  34. a.style.display = 'none';
  35. document.body.appendChild(a);
  36. a.click();
  37. URL.revokeObjectURL(a.href);
  38. document.body.removeChild(a);
  39. console.log('图片已下载到本地');
  40. }
  41. timerId = setTimeout(checkElement, 10); // 间隔1秒再次检查元素
  42. };
  43. xhr.send();
  44. });
  45.  
  46. // 找到用于定位下载按钮位置的元素,并将下载按钮添加到其后面
  47. window.globalVar = "";
  48. let timerId = null; // 定义一个变量,用于保存定时器ID
  49.  
  50. function checkElement() {
  51. const element = document.querySelector("#page-chats > div.media-viewer-whole.no-forwards.active > div.media-viewer-movers > div.media-viewer-mover.center.no-transition.active > div > img");
  52. if (element) {
  53. const jss = document.querySelector("#page-chats > div.media-viewer-whole.no-forwards.active > div.media-viewer-movers > div.media-viewer-mover.center.no-transition.active > div > img").src;
  54. const url = jss;
  55. if (url !== window.globalVar) {
  56. window.globalVar = url; // 更新全局变量的值
  57. const zoomInBtn = document.querySelector('.btn-icon.tgico-zoom.zoom-in');
  58. zoomInBtn.insertAdjacentElement('afterend', downloadBtn);
  59. }
  60. timerId = setTimeout(checkElement, 100); // 间隔100毫秒再次检查元素
  61. } else {
  62. timerId = setTimeout(checkElement, 100); // 间隔100毫秒再次检查元素
  63. }
  64. }
  65.  
  66. // 开始定时器
  67. timerId = setTimeout(checkElement, 100);
  68.  
  69. })();

QingJ © 2025

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