import os
register = {}
Add object instances in a dictionary, it can be used to point to references we don,t want to be garbage collected, for usage later
class Instance:
@staticmethod
def get_instances() -> dict:
return register
@staticmethod
def record(name: str, _type: object) -> None:
register[name] = _type
print(f"Registering ['{name}'] Instance")
@staticmethod
def retrieve(name: str) -> object or str:
try:
return register[name]
except KeyError:
print(f"Instance: ['{name}'] Not Present, to add it use -> Instance.record(['{name}', object])")
return ""
@staticmethod
def auto(name: str, _type: object) -> object:
try:
return register[name]
except KeyError:
register[name] = _type
finally:
print(f"Registering and Retrieving ['{name}'] Instance")
return register[name]
class JavaScript:
@staticmethod
def log(webview, message: str) -> None:
JavaScript.send(webview, f"console.log('JAK log:{message}');")
@staticmethod
def css(webview, styles: str) -> None:
javascript = f"""
var style = document.createElement('style');
style.type = 'text/css';
style.classList.add('jak-custom-style');
style.innerHTML = "{JavaScript.detect_type(styles)}";
document.getElementsByTagName('head')[0].appendChild(style);
"""
JavaScript.send(webview, javascript)
JavaScript.log(webview, f"JAK Custom Styles Applied:[{styles}]")
@staticmethod
def alert(webview, message: str) -> None:
JavaScript.send(webview, f"alert('{message}');")
JavaScript.log(webview, f"JAK Alert:[{message}]")
def send(self, javascript: str) -> None:
self.page().runJavaScript(f"{JavaScript.detect_type(javascript)}")
@staticmethod
def detect_type(inbound) -> str:
if os.path.exists(inbound) and os.path.isfile(inbound):
try:
with open(inbound, "r") as file:
string = file.read()
return string
except IOError:
return False
return True
elif isinstance(inbound, str):
return inbound
else:
print("JavaScript.send() error, file path or string.")