forked from kakaroto/Beyond20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dndbeyond_encounter.pyj
60 lines (51 loc) · 2.14 KB
/
dndbeyond_encounter.pyj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from settings import getDefaultSettings, getStoredSettings
from dndbeyond import Monster, removeRollButtons
from constants import BUTTON_STYLE_CSS, ROLLTYPE_STYLE_CSS
from utils import isExtensionDisconnected, injectCSS, alertFullSettings
print("Beyond20: D&D Beyond Encounter module loaded.")
settings = getDefaultSettings()
last_monster_name = ""
character = None
def documentModified(mutations, observer):
nonlocal settings
nonlocal last_monster_name
nonlocal character
if isExtensionDisconnected():
console.log("This extension is DOWN!")
observer.disconnect()
return
monster = $(".encounter-details-monster-summary-info-panel,.encounter-details__content-section--monster-stat-block,.combat-tracker-page__content-section--monster-stat-block,.monster-details-modal__body")
monster_name = monster.find(".mon-stat-block__name").text()
console.log("Doc modified, new mon : ", monster_name, " !=?", last_monster_name)
if monster_name == last_monster_name:
return
last_monster_name = monster_name
removeRollButtons()
character = Monster("Monster", global_settings=settings)
character.parseStatBlock(monster)
def updateSettings(new_settings=None):
nonlocal settings
nonlocal character
if new_settings:
settings = new_settings
if character is not None:
character.setGlobalSettings(settings)
else:
getStoredSettings(def(saved_settings):
nonlocal settings
updateSettings(saved_settings)
documentModified()
)
def handleMessage (request, sender, sendResponse):
if request.action == "settings":
if request.type == "general":
updateSettings(request.settings)
elif request.action == "open-options":
alertFullSettings()
updateSettings()
injectCSS(BUTTON_STYLE_CSS)
injectCSS(ROLLTYPE_STYLE_CSS)
chrome.runtime.onMessage.addListener(handleMessage)
observer = new window.MutationObserver(documentModified)
observer.observe(document, {"subtree": True, "childList": True})
chrome.runtime.sendMessage({"action": "activate-icon"})