def __init__(self, width, height, onclose): self._w = window.open("", "") self._stage = JSConstructor(GFX.Container)() self.width = width if width != 0 else int(window.innerWidth * 0.9) self.height = height if height != 0 else int(window.innerHeight * 0.9) self._renderer = GFX.autoDetectRenderer(self.width, self.height, {'transparent': True}) self._w.document.body.appendChild(self._renderer.view) consoleArea = self._w.document.createElement('DIV') self.consoleText = self._w.document.createElement('TEXTAREA') self.consoleText.id = "textarea" consoleArea.id = "consoleArea" self.consoleText.readonly = True consoleArea.appendChild(self.consoleText) style = self._w.document.createElement('STYLE') css = '#consoleArea {width: 70px; height: 100%; background-color: black; position: fixed; right: 0; top: 0;} #textarea {color: white; opacity: 0; background-color: rgba(0,0,0,0); width: 260px; height: 810px; position: absolute; top: 50px; margin: 0px; left: 20px; resize: none; border: none; outline: none;} .switch {position: relative; display: inline-block; width: 30px; height: 17px; top: 15px; margin-left: 20px;} .switch input {display:none;} .slider {position: absolute;cursor: pointer;top: 0;left: 0;right: 0;bottom: 0;background-color: #ccc;-webkit-transition: .4s;transition: .4s;} .slider:before {position: absolute;content: "";height: 13px;width: 13px;left: 2px;bottom: 2px;background-color: white;-webkit-transition: .4s;transition: .4s;} input:checked + .slider {background-color: #2196F3;} input:focus + .slider {box-shadow: 0 0 1px #2196F3;} input:checked + .slider:before {-webkit-transform: translateX(13px);-ms-transform: translateX(13px);transform: translateX(13px);} .slider.round {border-radius: 17px;} .slider.round:before {border-radius: 50%;} body{-webkit-touch-callout: none;-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}' style.type = 'text/css' style.appendChild(document.createTextNode(css)) self._w.document.head.appendChild(style) script = self._w.document.createElement('SCRIPT') js = 'function animateIt(dir, max, min) {var elem = document.getElementById("consoleArea"); var elem2 = document.getElementById("textarea"); var pos = max;var target = min;var id = setInterval(frame, 3);function frame() {var opa = Math.round((Math.abs(pos - target) / Math.abs(max - min)) * 100)/100;if (dir > 0) {opa = 1 - opa;} if (pos == target) {clearInterval(id);} else {pos = pos + dir; elem.style.width = pos + "px"; elem2.style.opacity = opa; }}}sessionStorage.switch = sessionStorage.switch ? sessionStorage.switch : "off";function switched() {if (sessionStorage.switch == "on") {sessionStorage.switch = "off";animateIt(-5, 300, 70);} else {sessionStorage.switch = "on";animateIt(5, 70, 300);}}' script.type = 'text/javascript' script.appendChild(document.createTextNode(js)) self._w.document.body.appendChild(script) self._w.onunload = onclose #if didLoadPyinput: # winput_init() consoleArea.insertAdjacentHTML( 'afterbegin', '<label class="switch"><input type="checkbox" onclick = "switched();"><div class="slider round"></div></label>' ) #self._w.document.body.innerHTML += '<div id = "inputScreen" style = "display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.2); z-index: 50;"><div style = "position: fixed; left: calc(50% - 150px); top: calc(50% - 75px); background-color: white;"><p id = "toPrompt"></p><input type = "text" id = "toInput"><input type = "submit" id = "toSubmit"></div></div>' self._w.document.body.appendChild(consoleArea)
def create_opsys_details(oshash, name, details={}): ajax_request = ajax.ajax() ajax_request.bind('complete', opsys_details_loaded) ajax_request.open('PUT', '/opsys/' + oshash, True) os_root = document.createElement('operating_system') os_root.setAttribute('hash', oshash) os_root.appendChild(document.createTextNode(name)) for key, value in details.items(): os_entry = os_root.appendChild(document.createElement(key)) os_entry.appendChild(document.createTextNode(value)) ajax_request.send(remove_xmlns(os_root.outerHTML))
def register_new_os(osname): def os_hash_generated(response): if response.status != 200: console.log("Request failed: " + str(response.status)) return result_hash = None parser = window.DOMParser.new() tree = parser.parseFromString(response.text, 'application/xml') opsys_root = tree.getElementsByTagName('operating_systems')[0] for os in opsys_root.getElementsByTagName('operating_system'): if str(os.textContent).strip() == osname: result_hash = os.getAttribute('hash') break if result_hash: opsys_list_loaded(response, put_details=True) ajax_request = ajax.ajax() ajax_request.bind('complete', os_hash_generated) ajax_request.open('POST', '/opsys/', True) ajax_request.set_header('Content-Type', 'text/xml;charset=utf-8') os_root = document.createElement('operating_systems') os_entry = os_root.appendChild(document.createElement('operating_system')) os_entry.appendChild(document.createTextNode(osname)) ajax_request.send(remove_xmlns(os_root.outerHTML))
def makeList(self, element, items): for i in items: opt = document.createElement("option") opt["value"] = i.key opt.obj = i opt.appendChild(document.createTextNode(str(i))) element.appendChild(opt)
def makeList(self, element, items, select=None): for i in items: opt = document.createElement("option") opt["value"] = i.key opt.obj = i if select and select is i: opt["selected"] = True opt.appendChild(document.createTextNode(str(i))) element.appendChild(opt)
def insert_spaces(nb): # insert four non-breaking spaces for the tab key sel = document.getSelection() _range = sel.getRangeAt(0) tabNode = document.createTextNode(" " * nb) _range.insertNode(tabNode) _range.setStartAfter(tabNode) _range.setEndAfter(tabNode) sel.removeAllRanges() sel.addRange(_range)
def insertTextAtCursor(self, text): try: sel = window.getSelection() _range = sel.getRangeAt(0) _range.deleteContents() _range.insertNode(doc.createTextNode(text)) except: pass try: doc.selection.createRange().text = text except: pass self.cursorToEnd()
def patch_opsys(name, value): oshash = name.split('_')[-1] field_name = name[:-len(oshash) - 1] ajax_request = ajax.ajax() ajax_request.bind('complete', lambda response: opsys_details_loaded(response, True)) ajax_request.open('PATCH', '/opsys/' + oshash, True) ajax_request.set_header('Content-Type', 'text/xml;charset=utf-8') device = document.createElement('operating_system') device.setAttribute('hash', oshash) field = device.appendChild(document.createElement(field_name)) field.appendChild(document.createTextNode(value)) ajax_request.send(remove_xmlns(device.outerHTML))
def opsys_details_loaded(response, invalidate=False): if response.status != 200: console.log("Request failed: " + str(response.status)) return parser = window.DOMParser.new() tree = parser.parseFromString(response.text, 'application/xml') opsys_root = tree.getElementsByTagName('operating_system')[0] os_name_parts = [] for child in opsys_root.childNodes: if child.nodeValue: os_name_parts.append(child.nodeValue) os_name = ''.join(os_name_parts).strip() del os_name_parts for row in document['operatingsystems'].getElementsByTagName('tr'): for cell in row.getElementsByTagName('td'): if cell.getAttribute('class') == 'os_name' and node_text( cell).strip() == os_name: break else: continue for child in opsys_root.children: for cell in row.getElementsByTagName('td'): if cell.getAttribute('class') == child.tagName: input_field = None try: input_field = cell.getElementsByTagName('input')[0] except IndexError: pass if input_field: val = child.textContent input_field.setAttribute('value', val) options_list = input_field.getAttribute('list') if options_list: extend_options_list(options_list, val) else: cell.appendChild( document.createTextNode(child.textContent)) break if invalidate: invalidate_devices_list()
def patch_device(name, value): mac = name.split('_')[-1] field_name = name[:-len(mac) - 1] s_mac = ':'.join(mac[2 * _i:2 * _i + 2] for _i in range(6)) ajax_request = ajax.ajax() ajax_request.bind( 'complete', lambda response: device_details_loaded(response, s_mac, True)) ajax_request.open('PATCH', '/device/' + mac, True) ajax_request.set_header('Content-Type', 'text/xml;charset=utf-8') device = document.createElement('device') device.setAttribute('mac', s_mac) field = device.appendChild(document.createElement(field_name)) field.appendChild(document.createTextNode(value)) ajax_request.send(remove_xmlns(device.outerHTML))
def __init__(self, width, height, onclose): self._w = window.open("", "") self._stage = JSConstructor(GFX.Container)() self.width = width if width != 0 else int(window.innerWidth * 0.9) self.height = height if height != 0 else int(window.innerHeight * 0.9) self._renderer = GFX.autoDetectRenderer(self.width, self.height, {'transparent':True}) self._w.document.body.appendChild(self._renderer.view) consoleArea = self._w.document.createElement('DIV') self.consoleText = self._w.document.createElement('TEXTAREA') self.consoleText.id = "textarea" self.consoleText.readonly = True consoleArea.appendChild(self.consoleText) style = self._w.document.createElement('STYLE') css = 'div {width: 300px; height: 100%; background-color: black; position: fixed; right: 0; top: 0;} #textarea {color: white; background-color: rgba(0,0,0,0); width: 260px; height: 810px; position: fixed; top: 20px; right: 20px; margin: 0px; resize: none; border: none; outline: none;}' style.type = 'text/css' style.appendChild(document.createTextNode(css)) self._w.document.head.appendChild(style) self._w.document.body.appendChild(consoleArea) self._w.onunload = onclose #if didLoadPyinput: # winput_init() self._w.document.body.insertAdjacentHTML('beforeend', '<div id = "inputScreen" style = "display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0,0,0,0.2); z-index: 50;"><div style = "position: fixed; left: calc(50% - 150px); top: calc(50% - 75px); background-color: white;"><p id = "toPrompt"></p><input type = "text" id = "toInput"><input type = "submit" id = "toSubmit"></div></div>')
def on_output(self, command, params): """Show output being sent back from the executor web worker.""" if command == 'output': node = document.createTextNode(params) self.output <= node self.output.class_name = 'output' elif command == 'err': err = html.SPAN(params) err.class_name = 'err' self.output <= err elif command == 'result': pass # discard for now elif command == 'ready': self.outbox.class_name = 'outbox' elif command == 'ex_result': print(params) test_id, err = params item = self.exercise_items[test_id] img = item.select('img')[0] if not err: img.attrs['src'] = 'static/svg/passed.svg' else: img.attrs['src'] = 'static/svg/failed.svg'
def process_devices(i=0): console.log("process_devices " + str(i) + "/" + str(len(devices) // rows_at_time)) if i == 0: while document['progress'].firstChild: document['progress'].removeChild( document['progress'].firstChild) for n in range(len(devices) // rows_at_time): prog_span = document.createElement('span') prog_span.setAttribute('class', 'prog_block_disabled') prog_span.appendChild(document.createTextNode("-")) document['progress'].appendChild(prog_span) document['overlay'].style.display = 'block' if i < len(devices) // rows_at_time: document['progress'].children[i].setAttribute( 'class', 'prog_block_enabled') for n in range(rows_at_time * i, rows_at_time * (i + 1)): if n >= len(devices): document['devices'].appendChild(fragment) document['overlay'].style.display = 'none' invalidate_devices_list() console.log("process_devices finished") break dev = devices[n] tr = fragment.appendChild(document.createElement('tr')) tr.setAttribute('class', 'devices_first') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'mac_address') td.setAttribute('rowspan', '2') mac_address = dev.getAttribute('mac') td.appendChild(document.createTextNode(mac_address)) mac_list.append(mac_address) device_rows[mac_address] = [tr, None] td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'mac_vendor') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'dhcp_fingerprint') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'fingerbank_guess') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'hostname') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'deduced_os') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'operating_system') td.setAttribute('rowspan', '2') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('list', 'opsys_name') s_mac = mac_address.replace(':', '').lower() input_field.setAttribute('name', 'operating_system_' + s_mac) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'devweight') td.setAttribute('rowspan', '2') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'number') input_field.setAttribute('step', '0.01') input_field.setAttribute('name', 'devweight_' + s_mac) bind_input_field(input_field) tr = fragment.appendChild(document.createElement('tr')) tr.setAttribute('class', 'devices_second') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'mac_address') td.setAttribute('rowspan', '2') mac_address = dev.getAttribute('mac') td.appendChild(document.createTextNode(mac_address)) mac_list.append(mac_address) device_rows[mac_address][1] = tr td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'bonjour_name') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'bonjour_model') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'bonjour_services') td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'bonjour_txt') td.setAttribute('colspan', '2') else: set_timeout(lambda: process_devices(i + 1), 0)
def opsys_list_loaded(response, get_details=False, put_details=False): if response.status != 200: console.log("Request failed: " + str(response.status)) return opsys_hashes = {} parser = window.DOMParser.new() tree = parser.parseFromString(response.text, 'application/xml') opsys_root = tree.getElementsByTagName('operating_systems')[0] for os in opsys_root.getElementsByTagName('operating_system'): tr = document['operatingsystems'].appendChild( document.createElement('tr')) os_hash = os.getAttribute('hash') os_name = str(os.textContent).strip() opsys_hashes[os_hash] = os_name td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'os_name') td.appendChild(document.createTextNode(os_name)) delb = td.appendChild(document.createElement('button')) delb.setAttribute('name', 'os_delete_' + os_hash) delb.setAttribute('class', 'os_delete') delb.appendChild(document.createTextNode("x")) bind_os_delete_button(delb) #option = document['opsys_name'].appendChild(document.createElement('option')) #option.setAttribute('value', os_name) extend_options_list('opsys_name', os_name) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'kernel_family') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('list', 'opsys_kernel') input_field.setAttribute('name', 'kernel_family_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'platform') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('list', 'opsys_platform') input_field.setAttribute('name', 'platform_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'flavor') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('list', 'opsys_flavor') input_field.setAttribute('name', 'flavor_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'version_number') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('name', 'version_number_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'code_name') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'text') input_field.setAttribute('name', 'code_name_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'release_date') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'date') input_field.setAttribute('name', 'release_date_' + os_hash) bind_input_field(input_field) td = tr.appendChild(document.createElement('td')) td.setAttribute('class', 'osweight') input_field = td.appendChild(document.createElement('input')) input_field.setAttribute('type', 'number') input_field.setAttribute('step', '0.01') input_field.setAttribute('name', 'osweight_' + os_hash) bind_input_field(input_field) if get_details: for os_hash in opsys_hashes.keys(): load_opsys_details(os_hash) elif put_details: for os_hash, os_name in opsys_hashes.items(): create_opsys_details(os_hash, os_name)
def device_details_loaded(response, mac, invalidate=False): if not len(mac) == 2 * 6 + 5: raise ValueError("mac should have length of " + str(2 * 6 + 5) + " characters") if not all(_ch in '0123456789abcdef:' for _ch in mac): raise ValueError( "the only characters allowed in mac: [0123456789abcdef]") if not mac.count(':') == 5: raise ValueError("mac should have 5 ':' characters") if response.status not in (200, 404): console.log("Request failed: " + str(response.status)) return try: remove_device = (response.status == 404) if remove_device: try: loaded_devices.remove(mac) except KeyError: pass mac_list = [] if not remove_device: parser = window.DOMParser.new() tree = parser.parseFromString(response.text, 'application/xml') device_root = tree.getElementsByTagName('device')[0] #for row in document['devices'].getElementsByTagName('tr'): # for cell in row.getElementsByTagName('td'): # if cell.getAttribute('class') == 'mac_address' and str(cell.textContent).strip() == mac: # break # else: # continue try: row1, row2 = device_rows[mac] except KeyError: console.log("No corresponding entry for " + mac) return if remove_device: document['devices'].removeChild(row1) document['devices'].removeChild(row2) loaded_devices.remove(mac) return loaded_devices.add(mac) for cell in [_cell for _cell in row1.getElementsByTagName('td') ] + [_cell for _cell in row2.getElementsByTagName('td')]: if cell.getAttribute('class') != 'mac_address': if len(cell.getElementsByTagName('input')): input_field = cell.getElementsByTagName('input')[0] input_field.value = '' else: input_field = None while cell.firstChild: cell.removeChild(cell.firstChild) for child in device_root.children: #console.log("adding child: ", child.tagName, cell.getAttribute('class'), (child.tagName == cell.getAttribute('class'))) if cell.getAttribute('class') == child.tagName: if input_field is not None: input_field.value = child.textContent elif child.tagName == 'fingerbank_guess': for textbit in child.textContent.split('/'): cell.appendChild(document.createTextNode(textbit)) cell.appendChild(document.createElement('br')) else: cell.appendChild( document.createTextNode(child.textContent)) if cell.getAttribute('class') == 'bonjour_txt': cell.appendChild(document.createElement('br')) finally: if invalidate: invalidate_devices_list({mac})
global weapondict weapondata = JSON.parse(request.responseText) weapondict = {w['id']:{'name':w['name'], 'tmrUnit':w['tmrUnit']} for w in weapondata if 'tmrUnit' in w.keys()} def requComplete(request): global unitdict unitdict = JSON.parse(request.responseText) code = 'en' document['privatebrowsing'].style.display = 'none' toolbar = html.DIV(id='toolbar', Class='container-fluid mx-3 mb-1') document <= toolbar langbar = html.DIV(id="langbar", Class="btn-group btn-group-toggle px-3", data_toggle="buttons", role="group", aria_label="Select language") langlabel = html.BUTTON(Class='btn-secondary rounded-left', disabled=True) langtext = document.createTextNode('Select language: ') langlabel <= langtext langbar <= langlabel toolbar <= langbar filestatus = html.I(Class='icon ion-md-checkmark-circle ml-3') filestatus.style.display = 'none' docbar = html.DIV(id='docbar', Class='btn-group px-3') doclabel = html.BUTTON(Class='btn-secondary rounded-left', disabled=True) doctext = document.createTextNode('Select file: ') doclabel <= doctext docbar <= doclabel toolbar <= docbar choosefile = html.INPUT(type='file', Class='btn btn-outline-primary') docbar <= choosefile buttonrun = html.BUTTON(id='buttonrun', Class='btn btn-primary px-3') buttontext = document.createTextNode('Show moogles!')