Skip to content

Commit

Permalink
Merge pull request #60 from suchmememanyskill/dev
Browse files Browse the repository at this point in the history
Release v1.5.0
  • Loading branch information
beebls authored Feb 1, 2023
2 parents 0cc6548 + e88deb0 commit 99b7986
Show file tree
Hide file tree
Showing 29 changed files with 1,098 additions and 429 deletions.
35 changes: 18 additions & 17 deletions css_inject.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from typing import List
from css_utils import Result, Log
from css_tab_mapping import get_multiple_tab_mappings, inject_css, remove_css
from css_tab_mapping import Tab, get_tabs

class Inject:
def __init__(self, cssPath : str, tabs : List[str], theme):
self.css = None
self.cssPath = cssPath
self.tabs = get_multiple_tab_mappings(tabs)
self.tabs = get_tabs(tabs)
self.uuids = {}
self.theme = theme
self.enabled = False
for x in self.tabs:
self.uuids[x] = []
self.uuids[x.id] = []

async def load(self) -> Result:
try:
Expand All @@ -25,7 +25,7 @@ async def load(self) -> Result:
except Exception as e:
return Result(False, str(e))

async def inject(self, tab : str = None) -> Result:
async def inject(self, tab : Tab = None) -> Result:
if (tab is None):
for x in self.tabs:
await self._inject_internal(x)
Expand All @@ -34,11 +34,11 @@ async def inject(self, tab : str = None) -> Result:
else:
return await self._inject_internal(tab)

async def _inject_internal(self, tab : str) -> Result:
async def _inject_internal(self, tab : Tab) -> Result:
if (tab not in self.tabs):
return Result(True) # this is kind of cheating but

if (len(self.uuids[tab]) > 0):
if (len(self.uuids[tab.id]) > 0):
await self.remove(tab)
self.enabled = True # In case the below code fails, it will never be re-injected unless it's still enabled

Expand All @@ -48,19 +48,19 @@ async def _inject_internal(self, tab : str) -> Result:
return result

try:
res = await inject_css(tab, self.css)
if not res["success"]:
return Result(False, str(res["result"]))
res = await tab.inject_css(self.css)
if not res.success:
return res

Log(f"+{str(res['result'])} @ {tab}")
self.uuids[tab].append(str(res["result"]))
Log(f"+{str(res.message)} @ {tab.get_name()}")
self.uuids[tab.id].append(str(res.message))
except Exception as e:
return Result(False, str(e))

self.enabled = True
return Result(True)

async def remove(self, tab : str = None) -> Result:
async def remove(self, tab : Tab = None) -> Result:
if (tab is None):
for x in self.tabs:
await self.remove(x)
Expand All @@ -70,19 +70,20 @@ async def remove(self, tab : str = None) -> Result:
if (tab not in self.tabs):
return Result(True) # this is kind of cheating but

if (len(self.uuids[tab]) <= 0):
if (len(self.uuids[tab.id]) <= 0):
return Result(True) # this is kind of cheating but

try:
for x in self.uuids[tab]:
Log(f"-{x} @ {tab}")
res = await remove_css(tab, x)
for x in self.uuids[tab.id]:
Log(f"-{x} @ {tab.get_name()}")
res = await tab.remove_css(x)
#if not res["success"]:
# return Result(False, res["result"])
# Silently ignore error. If any page gets reloaded, and there was css loaded. this will fail as it will fail to remove the css

self.uuids[tab] = []
self.uuids[tab.id] = []
except Exception as e:
# TODO: Investigate failure here
return Result(False, str(e))

self.enabled = False
Expand Down
Loading

0 comments on commit 99b7986

Please sign in to comment.