Etherscan Contract Downloader

batch download etherscan verified contract

  1. // ==UserScript==
  2. // @name Etherscan Contract Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.9
  5. // @description batch download etherscan verified contract
  6. // @author jason@trillion.fi
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.5.0/jszip.min.js
  8. // @match https://*.etherscan.io/address/*
  9. // @match https://ftmscan.com/address/*
  10. // @match https://bscscan.com/address/*
  11. // @match https://snowtrace.io/address/*
  12. // @match https://polygonscan.com/address/*
  13. // @match https://hecoinfo.com/address/*
  14. // @match https://optimistic.etherscan.io/address/*
  15. // @match https://arbiscan.io/address/*
  16. // @match https://aurorascan.dev/address/*
  17. // @match https://cronoscan.com/address/*
  18. // @icon https://etherscan.io/images/brandassets/etherscan-logo-circle.png
  19. // @grant unsafeWindow
  20. // @license MIT
  21. // ==/UserScript==
  22. (function() {
  23. 'use strict';
  24.  
  25. let editorIds = $("[id^=editor]")
  26. let spans = $('#dividcode span.text-muted:not(:first):not(:last)').toArray()
  27. console.log(`found ${editorIds.length>1?editorIds.length-1:1} editors`);
  28.  
  29.  
  30. function getAddr() {
  31. const regex = /0x[0-9A-Fa-f]{40}/g;
  32. const found = window.location.href.match(regex);
  33. return found[0]
  34. }
  35.  
  36. function getDethUrl() {
  37. let host = window.location.host;
  38. let newHost = host.split(".")[0] + ".deth.net"
  39. let url = window.location.href;
  40. return url.replace(host, newHost);
  41. }
  42.  
  43. function getByteGraphUrl() {
  44. var url = window.location.href;
  45. var modifiedUrl = url.replace(/^https:\/\/etherscan\.io\/address\/(.+?)(?:#.*)?$/, 'https://bytegraph.xyz/address/$1');
  46. return modifiedUrl;
  47. }
  48.  
  49. function downloadAllEditorsAsZip() {
  50. let zip = new JSZip();
  51. let addr = getAddr();
  52. let end = editorIds.length > 1 ? editorIds.length - 1 : 1;
  53. for (let index = 0; index < end; index++) {
  54. let editor = ace.edit(editorIds[index]);
  55. let filename;
  56. try {
  57. filename = `${spans[index].innerText.split(":")[1].trim()}`;
  58. } catch {
  59. filename = `${addr}.sol`;
  60. }
  61. zip.file(filename, editor.getValue());
  62. }
  63.  
  64. // Generate the zip file and create a blob URL for it
  65. zip.generateAsync({type:"blob"}).then(function(content) {
  66. let url = URL.createObjectURL(content);
  67.  
  68. // Create a hidden link and click it to start the download
  69. let link = document.createElement("a");
  70. link.href = url;
  71. link.download = `${addr}.zip`;
  72. link.click();
  73. });
  74. }
  75.  
  76. if (!unsafeWindow.downloadAllEditorsAsZip) {
  77. unsafeWindow.downloadAllEditorsAsZip = downloadAllEditorsAsZip;
  78. }
  79. $("#nav_subtabs").append('<li class="nav-item"><a class="nav-link show" href="#downloadZip" data-toggle="tab" onclick="javascript:downloadAllEditorsAsZip();"><span>Download All as Zip</span></a></li>');
  80. $("#nav_subtabs").append(`<li class="nav-item"><a class="nav-link show" href="#vscode" onclick="window.open('${getDethUrl()}','_blank')" data-toggle="tab""><span>Deth.net</span></a></li>`);
  81. $("#nav_subtabs").append(`<li class="nav-item"><a class="nav-link show" href="#bytegraph" onclick="window.open('${getByteGraphUrl()}','_blank')" data-toggle="tab""><span>Bytegraph.xyz</span></a></li>`);
  82.  
  83. $('.table th:contains("Age")').css("width", "14%");
  84.  
  85. $('.table span:contains(" ago")').each(function() {
  86. var relTime = $(this).text();
  87. var absTime = $(this).attr('title');
  88. if (!absTime) absTime = $(this).attr('data-original-title');
  89. // absTime = moment(absTime, "MMM-DD-YYYY hh:mm:ss A", "en").add(1, 'h').format("YYYY-MM-DD HH:mm:ss");
  90. absTime = moment(absTime).add(8, 'h').format("YYYY-MM-DD HH:mm:ss");
  91. $(this).attr('title', relTime);
  92. $(this).attr('data-original-title', relTime);
  93. $(this).text(absTime);
  94. });
  95.  
  96. //
  97. })();