Skip to content

Instantly share code, notes, and snippets.

@salazarm
Created April 9, 2024 15:53
Show Gist options
  • Select an option

  • Save salazarm/154638c5a4415e01dc16bf7f48465d24 to your computer and use it in GitHub Desktop.

Select an option

Save salazarm/154638c5a4415e01dc16bf7f48465d24 to your computer and use it in GitHub Desktop.
Fetch source maps for a website that exposes them
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
def fetch_html(url):
try:
response = requests.get(url)
response.raise_for_status() # This will raise an exception for HTTP errors
return response.text
except requests.RequestException as e:
print(f"Request failed: {e}")
return None
def fetch_js_files_and_sourcemaps(html_content, base_url):
soup = BeautifulSoup(html_content, 'html.parser')
script_tags = soup.find_all('script', src=True)
for script_tag in script_tags:
js_url = urljoin(base_url, script_tag['src'])
map_url = js_url + '.map'
try:
# Fetch Source Map
map_response = requests.get(map_url)
if map_response.status_code == 200:
map_data = map_response.json()
sources = map_data.get('sources', [])
for source in sources:
source_index = sources.index(source)
original_content = map_data.get('sourcesContent', [])[source_index]
source_filename = source.split('/')[-1]
with open(source_filename, 'w') as source_file:
source_file.write(original_content)
print(f"Re-created original source file: {source_filename}")
else:
print(f"Source map not found or failed to fetch: {map_url}")
except Exception as e:
print(f"Error processing {js_url}: {e}")
def main():
url = 'https://trading.galaxydigital.io'
base_url = url # Assuming the base URL is the same as the fetched URL
html_content = fetch_html(url)
if html_content:
fetch_js_files_and_sourcemaps(html_content, base_url)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment