Skip to content

Instantly share code, notes, and snippets.

@vibhuti019
Last active July 23, 2021 22:51
Show Gist options
  • Save vibhuti019/7084d76bddaa8f839be6f7885cac9298 to your computer and use it in GitHub Desktop.
Save vibhuti019/7084d76bddaa8f839be6f7885cac9298 to your computer and use it in GitHub Desktop.
Python File
import requests
import sys
import socket
import os
import re
content = ""
dataTypesToChangeUrl = [".jpg", ".jpeg", ".png", ".gif", ".ico", ".css","css", ".js", ".ttf", ".otf", ".woff", ".woff2",".eot"]
def ChangeUrl(item,url):
global content
global dataTypesToChangeUrl
if any(s in item for s in dataTypesToChangeUrl):
while item.startswith("/"):
item = item[1:]
external = False
prefix = ""
print(item)
if "#" in item:
item = item.split("#")[0]
if item.startswith("https://"):
external = True
prefix = "https://"
item = item.replace("https://", "")
if item.startswith("http://"):
external = True
prefix = "http://"
item = item.replace("http://", "")
if item.startswith("//"):
external = True
prefix = "//"
item = item.replace("//", "")
#Todo " if items are used with ../ "
if not external:
if (content.find(url+"/"+item) == -1):
content = content.replace("/"+item,url+"/"+item)
print(item+" To "+url+"/"+item)
def mainProgram(url):
socket.setdefaulttimeout(15)
global dataTypesToChangeUrl
global content
if "http://" not in url and "https://" not in url:
url = "http://"+url
domain = "//".join(url.split("//")[1:])
with requests.Session() as r:
try:
content = r.get(url).text
except Exception as e:
print("Error: {}".format(e))
resources = re.split("=\"|='", content)
for resource in resources:
resource = re.split("\"|'", resource)[0]
ChangeUrl(resource,url)
hrefs = content.split("href=")
for i in range( len(hrefs) - 1 ):
href = hrefs[i+1]
ChangeUrl(href,url)
print(content)
addScript = """<script>
let oldData = \"\"
document.addEventListener(\"mouseover\", (e)=>{
oldData = e.target.parentElement.style.backgroundColor
e.target.style.backgroundColor = \"red\"
})
document.addEventListener(\"mouseout\", (e)=>{
e.target.style.backgroundColor = oldData
})
document.addEventListener(\"click\", (e)=>{
alert("Do you wish to delete this item?")
e.target.style.display = "none"
})
</script>
</head>"""
content = content.replace("</head>",addScript)
file = open("a.html", "w")
file.write(content)
file.close()
mainProgram("https://www.github.com")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment