Fixes for absolute links

This commit is contained in:
Amazed 2018-10-29 21:53:15 +01:00
parent f4fc947c62
commit 35c26d9387
3 changed files with 44 additions and 3 deletions

View File

@ -73,7 +73,7 @@
var iframeWnd = $($("iframe")[0].contentWindow);
// disable a links
$("iframe").contents().find("a").attr("href", "#");
//$("iframe").contents().find("a").attr("href", "#");
iframeWnd.on('click', function(event) {
selectedElement = $(event.target).getPath();

View File

@ -40,6 +40,9 @@
<li class="nav-item">
<a class="nav-link" href="{% url 'feeds' %}">Feeds</a>
</li>
<li class="nav-item">
<a class="nav-link" href="https://hipstercat.fr/gogs/hipstercat/hrss">Source code</a>
</li>
</ul>
</div>
</div>

View File

@ -3,15 +3,53 @@ from django.http import HttpResponse
from .utils import *
from .models import Feed
from django.db.models import ObjectDoesNotExist
from bs4 import BeautifulSoup
# Create your views here.
def iframe(request, url):
content_type = False
try:
html = requests.get(url).text
req = requests.get(url)
html = req.content
bs = False
content_type = req.headers["Content-Type"] if "Content-Type" in req.headers else False
if not content_type or content_type.startswith("text/html"):
bs = BeautifulSoup(html, 'html.parser')
base_scheme = url.split("://")[0]
base_url = url.split("//")[-1].split("/")[0].split('?')[0]
# fixes
# fix click links
all_a = bs.find_all("a")
for a in all_a:
a["href"] = "javascript:void(0)"
# fix absolute CSS
all_links = bs.find_all("link", {"href": True})
for link in all_links:
if link["href"].startswith("/"):
link["href"] = "/iframe/" + base_scheme + "://" + base_url + link["href"]
# fix absolute javascript
all_scripts = bs.find_all("script", {"src": True})
for script in all_scripts:
if script.get("src").startswith("/"):
script["src"] = "/iframe/" + base_scheme + "://" + base_url + script["src"]
# fix absolute images
all_imgs = bs.find_all("img", {"src": True})
for img in all_imgs:
if img.get("src").startswith("/"):
img["src"] = "/iframe/" + base_scheme + "://" + base_url + img["src"]
final_html = str(bs) if bs else html
html = final_html
except Exception as e:
html = str(e)
return HttpResponse(html)
return HttpResponse(html, content_type=content_type)
def dummy(request):
return HttpResponse("toto")