forked from kakaroto/Beyond20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.pyj
199 lines (185 loc) · 9 KB
/
popup.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from elementmaker import E
from settings import options_list, character_settings, createHTMLOption, createHTMLOptionEx, \
initializeSettings, saveSettings, setCurrentTab, loadSettings
from constants import ROLL20_URL, DNDBEYOND_CHARACTER_URL, DNDBEYOND_MONSTER_URL, \
DNDBEYOND_ENCOUNTER_URL, DNDBEYOND_ENCOUNTERS_URL, DNDBEYOND_COMBAT_URL, DNDBEYOND_VEHICLE_URL
from utils import isFVTT, urlMatches
def sendMessageToTab(tab_id, message, callback):
if chrome.tabs:
chrome.tabs.sendMessage(tab_id, message, callback)
else:
chrome.runtime.sendMessage({"action": "forward", "tab": tab_id, "message": message}, callback)
character = None
settings = None
def gotSettings(stored_settings):
nonlocal settings
settings = stored_settings
$("ul").removeClass("disabled")
def createOptionList():
options = []
for option in options_list:
child = createHTMLOption(option, True)
if child:
options.append(child)
$("main").prepend(E.ul(class_="list-group beyond20-options", *options))
$(".beyond20-options").append(E.li(class_="list-group-item beyond20-option",
E.a(id="openOptions", class_="list-content", href='#',
E.h4("More Options")
)
)
)
img = $("#donate").find("img")
img.attr({
"src": img.attr("src").replace("donate.png", "donate32.png"),
"width": 32,
"height": 32
})
$("#openOptions").on('click', def (ev):
chrome.runtime.openOptionsPage()
)
def canAlertify(tab_id):
$("#openOptions").off('click').on('click', def (ev):
sendMessageToTab(tab_id, {"action": "open-options"})
window.close()
)
def save_settings():
nonlocal character
saveSettings(def(settings):
chrome.runtime.sendMessage({"action": "settings", "type": "general", "settings": settings})
)
if character is not None:
saveSettings(def(settings):
chrome.runtime.sendMessage({"action": "settings", "type": "character", "id": character.id, "settings": settings})
, "character-" + character.id, character_settings)
def setupHTML():
createOptionList()
$('.beyond20-option-input').change(save_settings)
$(".beyond20-options").on("markaChanged", save_settings)
$(document).on('click', 'a', def(ev):
href = this.getAttribute('href')
if len(href) > 0 and href != "#":
window.open(this.href)
return False
)
$("ul").addClass("disabled")
def populateCharacter(response):
nonlocal character
character = response
if response?:
console.log("Received character: ", response)
options = $(".beyond20-options")
options.append(E.li(class_="list-group-item beyond20-option", id="character-option", style="text-align: center; padding: 10px 15px;",
E.h4(" == Character Specific Options ==", style="margin: 0px;"),
E.p(response.name, style="margin: 0px;"))
)
e = createHTMLOption("versatile-choice", False, character_settings)
options.append(e)
e = createHTMLOption("custom-roll-dice", False, character_settings)
options.append(e)
e = createHTMLOption("custom-damage-dice", False, character_settings)
options.append(e)
if "Rogue" in response.classes:
e = createHTMLOption("rogue-sneak-attack", False, character_settings)
options.append(e)
if "Disciple of Life" in response["class-features"]:
e = createHTMLOption("cleric-disciple-life", False, character_settings)
options.append(e)
if "Bard" in response.classes:
e = createHTMLOption("bard-joat", False, character_settings)
options.append(e)
if "Sharpshooter" in response["feats"]:
e = createHTMLOption("sharpshooter", False, character_settings)
options.append(e)
if "Great Weapon Master" in response["feats"]:
e = createHTMLOption("great-weapon-master", False, character_settings)
options.append(e)
if "Brutal Critical" in response["class-features"] or \
"Savage Attacks" in response["racial-traits"]:
e = createHTMLOption("brutal-critical", False, character_settings)
options.append(e)
if "Rage" in response["class-features"]:
e = createHTMLOption("barbarian-rage", False, character_settings)
options.append(e)
if "Crimson Rite" in response["class-features"]:
e = createHTMLOption("bloodhunter-crimson-rite", False, character_settings)
options.append(e)
if "Dread Ambusher" in response["class-features"]:
e = createHTMLOption("ranger-dread-ambusher", False, character_settings)
options.append(e)
if "Channel Divinity: Legendary Strike" in response["actions"]:
e = createHTMLOption("paladin-legendary-strike", False, character_settings)
options.append(e)
if "Improved Divine Smite" in response["class-features"]:
e = createHTMLOption("paladin-improved-divine-smite", False, character_settings)
options.append(e)
if "Hexblade’s Curse" in response["class-features"]:
e = createHTMLOption("warlock-hexblade-curse", False, character_settings)
options.append(e)
if "Assassinate" in response["class-features"]:
e = createHTMLOption("rogue-assassinate", False, character_settings)
options.append(e)
if "Giant Might" in response["class-features"]:
e = createHTMLOption("fighter-giant-might", False, character_settings)
options.append(e)
if "Arcane Firearm" in response["class-features"]:
e = createHTMLOption("artificer-arcane-firearm", False, character_settings)
options.append(e)
if "Divine Strike" in response["class-features"]:
e = createHTMLOption("cleric-divine-strike", False, character_settings)
options.append(e)
if "Psychic Blades" in response["class-features"]:
e = createHTMLOption("bard-psychic-blades", False, character_settings)
options.append(e)
loadSettings(response.settings, character_settings)
$('.beyond20-option-input').off('change', save_settings)
$('.beyond20-option-input').change(save_settings)
initializeSettings(gotSettings)
def addMonsterOptions():
option = options_list["whisper-type-monsters"]
option["short"] = "Whisper monster rolls"
e = createHTMLOptionEx("whisper-type-monsters", option, True)
$(e).insertAfter($("#whisper-type").parents("li"))
options = $(".beyond20-options")
options.append(E.li(class_="list-group-item beyond20-option", style="text-align: center; padding: 10px;",
E.h4(" == Stat Block Specific Options =="))
)
e = createHTMLOption("subst-dndbeyond-stat-blocks", False)
options.append(e)
e = createHTMLOption("handle-stat-blocks", False)
options.append(e)
$('.beyond20-option-input').off('change', save_settings)
$('.beyond20-option-input').change(save_settings)
initializeSettings(gotSettings)
def tabMatches(tab, url):
return tab.url.match(url.replace(/\*/g, "[^]*")) != None
def actOnCurrentTab(tab):
setCurrentTab(tab)
if urlMatches(tab.url, ROLL20_URL) or isFVTT(tab.title):
vtt = "Foundry VTT" if isFVTT(tab.title) else "Roll20"
options = $(".beyond20-options")
options.append(E.li(class_="list-group-item beyond20-option", style="text-align: center; margin: 10px;",
E.h4(" == " + vtt + " Tab Specific Options ==")
))
if vtt == "Roll20":
e = createHTMLOption("roll20-template", False)
options.append(e)
e = createHTMLOption("display-conditions", False)
options.append(e)
e = options_list["vtt-tab"].createHTMLElement("vtt-tab", True)
options.append(e)
$('.beyond20-option-input').off('change', save_settings)
$('.beyond20-option-input').change(save_settings)
initializeSettings(gotSettings)
elif urlMatches(tab.url, DNDBEYOND_CHARACTER_URL):
sendMessageToTab(tab.id, {"action": "get-character"}, populateCharacter)
elif urlMatches(tab.url, DNDBEYOND_MONSTER_URL) or urlMatches(tab.url, DNDBEYOND_VEHICLE_URL) or \
urlMatches(tab.url, DNDBEYOND_ENCOUNTERS_URL) or urlMatches(tab.url, DNDBEYOND_ENCOUNTER_URL) or urlMatches(tab.url, DNDBEYOND_COMBAT_URL):
addMonsterOptions()
else:
initializeSettings(gotSettings)
canAlertify(tab.id)
setupHTML()
if chrome.tabs?:
chrome.tabs.query({"active": True, "currentWindow": True}, def(tabs): actOnCurrentTab(tabs[0]);)
else:
chrome.runtime.sendMessage({"action": "get-current-tab"}, actOnCurrentTab)