diff --git a/app/routes.py b/app/routes.py index 259e53a..d26dbca 100644 --- a/app/routes.py +++ b/app/routes.py @@ -355,8 +355,8 @@ def search(): html_soup = bsoup(str(response), 'html.parser') if search_util.widget == 'ip': response = add_ip_card(html_soup, get_client_ip(request)) - elif search_util.widget == 'calculator' and not 'nojs' in request.args: - response = add_calculator_card(html_soup) + elif not 'nojs' in request.args: + response = add_widget(html_soup, search_util.widget) # Update tabs content tabs = get_tabs_content(app.config['HEADER_TABS'], diff --git a/app/static/widgets/color_picker.html b/app/static/widgets/color_picker.html new file mode 100644 index 0000000..cb8abba --- /dev/null +++ b/app/static/widgets/color_picker.html @@ -0,0 +1,575 @@ + + + +
+ +

+ + + +


+

x

+
+
+
+
+ + HEX + +
+ + RGB + +
+ + CMYK + +
+ + HSL + +
+ + HSV + +
+ + diff --git a/app/utils/search.py b/app/utils/search.py index f643cbe..5a36cf2 100644 --- a/app/utils/search.py +++ b/app/utils/search.py @@ -110,6 +110,7 @@ class Search: "($|( *[^a-z0-9] *(((addres|address|adres|" + "adress)|a)? *$)))", self.query.lower()) else self.widget self.widget = 'calculator' if re.search("calculator|calc|calclator|math", self.query.lower()) else self.widget + self.widget = 'color_picker' if re.search("color|colour", self.query.lower()) else self.widget return self.query def generate_response(self) -> str: diff --git a/app/utils/widgets.py b/app/utils/widgets.py index eef06fc..f58393b 100644 --- a/app/utils/widgets.py +++ b/app/utils/widgets.py @@ -1,5 +1,47 @@ from bs4 import BeautifulSoup +widgets = { + 'calculator': { + 'widget_file': 'app/static/widgets/calculator.html', + 'wrapper_id': 'calculator-wrapper', + 'display_name': 'Calculator', + }, + 'color_picker': { + 'widget_file': 'app/static/widgets/color_picker.html', + 'wrapper_id': 'color-picker-wrapper', + 'display_name': 'Color Picker' + } +} + +def add_widget(html_soup: BeautifulSoup, name: str) -> BeautifulSoup: + """Adds the a widget to the search results + Args: + html_soup: The parsed search result containing the keywords + name: The name of the widget to be added + Returns: + BeautifulSoup + """ + if not name in widgets: + # if widget doesn't exist, silently fail + return + main_div = html_soup.select_one('#main') + if main_div: + widget_file = open(widgets[name]['widget_file']) + widget_tag = html_soup.new_tag('div') + widget_tag['class'] = 'ZINbbc xpd O9g5cc uUPGi' + widget_tag['id'] = widgets[name]['wrapper_id'] + widget_text = html_soup.new_tag('div') + widget_text['class'] = 'kCrYT ip-address-div' + widget_text.string = widgets[name]['display_name'] + widget = html_soup.new_tag('div') + widget.append(BeautifulSoup(widget_file, 'html.parser')); + widget['class'] = 'kCrYT ip-text-div' + widget_tag.append(widget_text) + widget_tag.append(widget) + main_div.insert_before(widget_tag) + widget_file.close() + return html_soup + def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: """Adds the client's IP address to the search results if query contains keywords @@ -35,31 +77,3 @@ def add_ip_card(html_soup: BeautifulSoup, ip: str) -> BeautifulSoup: # Insert the element at the top of the result list main_div.insert_before(ip_tag) return html_soup - -def add_calculator_card(html_soup: BeautifulSoup) -> BeautifulSoup: - """Adds the a calculator widget to the search results - if query contains keywords - - Args: - html_soup: The parsed search result containing the keywords - - Returns: - BeautifulSoup - """ - main_div = html_soup.select_one('#main') - if main_div: - widget_file = open('app/static/widgets/calculator.html') - widget_tag = html_soup.new_tag('div') - widget_tag['class'] = 'ZINbbc xpd O9g5cc uUPGi' - widget_tag['id'] = 'calculator-wrapper' - calculator_text = html_soup.new_tag('div') - calculator_text['class'] = 'kCrYT ip-address-div' - calculator_text.string = 'Calculator' - calculator_widget = html_soup.new_tag('div') - calculator_widget.append(BeautifulSoup(widget_file, 'html.parser')); - calculator_widget['class'] = 'kCrYT ip-text-div' - widget_tag.append(calculator_text) - widget_tag.append(calculator_widget) - main_div.insert_before(widget_tag) - widget_file.close() - return html_soup