From 62d7491936c97a27876eafc05fbc5c73c39168ee Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Tue, 26 Apr 2022 15:18:29 -0600 Subject: [PATCH] Only create ip card if main result div is found The ip address card that is created for searches like "my ip" only needs to be created/inserted if a main result div id is found. Fixes #735 --- app/utils/results.py | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/app/utils/results.py b/app/utils/results.py index 70a3696..2341c36 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -227,26 +227,28 @@ def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: BeautifulSoup """ - # HTML IP card tag - ip_tag = html_soup.new_tag('div') - ip_tag['class'] = 'ZINbbc xpd O9g5cc uUPGi' - - # For IP Address html tag - ip_address = html_soup.new_tag('div') - ip_address['class'] = 'kCrYT ip-address-div' - ip_address.string = ip - - # Text below the IP address - ip_text = html_soup.new_tag('div') - ip_text.string = 'Your public IP address' - ip_text['class'] = 'kCrYT ip-text-div' - - # Adding all the above html tags to the IP card - ip_tag.append(ip_address) - ip_tag.append(ip_text) - - # Insert the element at the top of the result list - html_soup.select_one('#main').insert_before(ip_tag) + main_div = html_soup.select_one('#main') + if main_div: + # HTML IP card tag + ip_tag = html_soup.new_tag('div') + ip_tag['class'] = 'ZINbbc xpd O9g5cc uUPGi' + + # For IP Address html tag + ip_address = html_soup.new_tag('div') + ip_address['class'] = 'kCrYT ip-address-div' + ip_address.string = ip + + # Text below the IP address + ip_text = html_soup.new_tag('div') + ip_text.string = 'Your public IP address' + ip_text['class'] = 'kCrYT ip-text-div' + + # Adding all the above html tags to the IP card + ip_tag.append(ip_address) + ip_tag.append(ip_text) + + # Insert the element at the top of the result list + main_div.insert_before(ip_tag) return html_soup