def get_rel_url(js): js = js[31:-9] # js=js.replace('location','\"http://bbs.21ic.com/icview-2545904-1-1.html\"') js = 'location= function(){return "http://bbs.21ic.com/icview-2545904-1-1.html\";};' + js js = 'window = function(){return "bbs.21ic.com";};' + js for st in [ 'window', '\'location\'', '\'assign\'', '\'href\'', '\'replace\'' ]: equal = re.findall('[_A-Za-z0-9= ]+{};'.format(st), js) # 找到变量赋值等式 if equal == []: continue else: var = equal[0].split('=') left = var[0].strip() ri = var[1].strip() js = js.replace(left + ';', ri) # js = js.replace(equal, st) # print(js) js = js.replace("['{}']".format(st).strip("'"), '.{}'.format(st).strip("'")) if re.findall('window.href=\"http://www.php1.cn/\">', js): js = js.replace('window.href=\"http://www.php1.cn/\">', js) js = js.replace('location.href=\"http://www.php1.cn/\">', js) ctxt = JSContext() ctxt.__enter__() x = (ctxt.eval(js)) vars = ctxt.locals url = 'http://bbs.21ic.com' + str(vars.location['href']) return url
class CommonJS(): _js_path = [ os.path.dirname(__file__), os.path.join(os.path.dirname(__file__), 'core') ] _js_logger = logger().instance() def __init__(self): self._js_threadlock = False self._js_ctx = JSContext(self) self._js_modules = {} self._loaded_modules = {} for jsroot in CommonJS._js_path: for (root, dirs, files) in os.walk(jsroot): #print files for _file in files: m = re.compile('(.*)\.js$').match(_file) relpath = os.path.relpath(root, jsroot) namespace = re.sub(r'^\.', '', relpath) namespace = re.sub(r'^\\', '/', namespace) if (namespace): namespace = namespace + '/' if (m): self._js_modules.update({ namespace + m.group(1): os.path.join(root, _file) }) self.execute("var exports;") @staticmethod def append(path): if (path not in CommonJS._js_path): CommonJS._js_path.append(path) def require(self, module): if (not self._js_modules.has_key(module)): raise Exception, "unknown module `" + module + "`" path = self._js_modules[module] if (not self._loaded_modules.has_key(path)): self._js_logger.info("loading module <%s>...", module) code = file(path).read() try: code = code.decode('utf-8') if (platform.system() == 'Windows'): code = code.encode('utf-8') self._js_ctx.eval(code) except JSError, ex: self._js_logger.error(ex) self._js_logger.debug(ex.stackTrace) raise Exception, ex self._loaded_modules[path] = self._js_ctx.locals.exports return self._loaded_modules[path] else:
class CommonJS: _js_path = [os.path.dirname(__file__), os.path.join(os.path.dirname(__file__), "core")] _js_logger = logger().instance() def __init__(self): self._js_threadlock = False self._js_ctx = JSContext(self) self._js_modules = {} self._loaded_modules = {} for jsroot in CommonJS._js_path: for (root, dirs, files) in os.walk(jsroot): # print files for _file in files: m = re.compile("(.*)\.js$").match(_file) relpath = os.path.relpath(root, jsroot) namespace = re.sub(r"^\.", "", relpath) namespace = re.sub(r"^\\", "/", namespace) if namespace: namespace = namespace + "/" if m: self._js_modules.update({namespace + m.group(1): os.path.join(root, _file)}) self.execute("var exports;") @staticmethod def append(path): if path not in CommonJS._js_path: CommonJS._js_path.append(path) def require(self, module): if not self._js_modules.has_key(module): raise Exception, "unknown module `" + module + "`" path = self._js_modules[module] if not self._loaded_modules.has_key(path): self._js_logger.info("loading module <%s>...", module) code = file(path).read() try: code = code.decode("utf-8") if platform.system() == "Windows": code = code.encode("utf-8") self._js_ctx.eval(code) except JSError, ex: self._js_logger.error(ex) self._js_logger.debug(ex.stackTrace) raise Exception, ex self._loaded_modules[path] = self._js_ctx.locals.exports return self._loaded_modules[path] else:
def evalExpr(self, expr): with JSContext(self.g) as c: try: ret = c.eval(expr) except Exception, e: raise ExprEvalError(e, []) # TODO: this causes variabels typeof == 'undefined' to become null instead. for key in c.locals.keys(): setattr(self.g, key, c.locals[key]) return ret
def __init__(self): self._js_threadlock = False self._js_ctx = JSContext(self) self._js_modules = {} self._loaded_modules = {} for jsroot in CommonJS._js_path: for (root, dirs, files) in os.walk(jsroot): #print files for _file in files: m = re.compile('(.*)\.js$').match(_file) relpath = os.path.relpath(root, jsroot) namespace = re.sub(r'^\.', '', relpath) namespace = re.sub(r'^\\', '/', namespace) if (namespace): namespace = namespace + '/' if (m): self._js_modules.update({ namespace + m.group(1): os.path.join(root, _file) }) self.execute("var exports;")
def __init__(self): self._js_threadlock = False self._js_ctx = JSContext(self) self._js_modules = {} self._loaded_modules = {} for jsroot in CommonJS._js_path: for (root, dirs, files) in os.walk(jsroot): # print files for _file in files: m = re.compile("(.*)\.js$").match(_file) relpath = os.path.relpath(root, jsroot) namespace = re.sub(r"^\.", "", relpath) namespace = re.sub(r"^\\", "/", namespace) if namespace: namespace = namespace + "/" if m: self._js_modules.update({namespace + m.group(1): os.path.join(root, _file)}) self.execute("var exports;")
def __init__(self, template): super(BemhtmlRenderer, self).__init__() with JSLocker(): self.ctx = JSContext() with self.ctx as ctx: ctx.eval(template)
output = self.evalExpr("(%s)" % contentNode.get("expr")) elif len(contentNode) == 0: output = contentNode.xpath("normalize-space(./text())") elif len(contentNode) > 0: output = contentNode.xpath("./*") else: self.logger.error("Line %s: error when parsing content node." % contentNode.sourceline) return return output def evalExpr(self, expr): return self[expr] def execExpr(self, expr): # TODO: should fail on exmode=strict self.logger.warn("The script element is ignored by the xpath datamodel.") # raise DataModelError("multiline expressions can't be executed on the xpath datamodel.") if __name__ == "__main__": # import PyV8 #@UnresolvedImport d = ECMAScriptDataModel() d.evalExpr("var a = [1,2,3]") c = JSContext(d.g) c.enter() print d["a"] # d = DataModel()
elif node_name == "script": try: src = node.text or self.script_src.get(node) or "" self.execExpr(src) except ExprEvalError, e: raise ExecutableError(e, node) elif node_name == "if": self.parseIf(node) elif node_name == "foreach": startIndex = 0 if self.datamodel != "xpath" else 1 try: array = self.getExprValue(node.get("array")) if self.datamodel == "ecmascript": from PyV8 import JSContext c = JSContext(self.dm.g) c.enter() # if self.datamodel == "xpath": # assert all(map(lambda x: x, array)) except ExprEvalError, e: raise AttributeEvalError(e, node, "array") except TypeError, e: err = DataModelError(e) raise AttributeEvalError(err, node, "array") for index, item in enumerate(array, startIndex): try: # if it's not a correct QName: crash. etree.QName(node.get("item")) if self.datamodel != "xpath": self.dm[node.get("item")] = item
elif node_name == "script": try: src = node.text or self.script_src.get(node) or "" self.execExpr(src) except ExprEvalError, e: raise ExecutableError(e, node) elif node_name == "if": self.parseIf(node) elif node_name == "foreach": startIndex = 0 if self.datamodel != "xpath" else 1 try: array = self.getExprValue(node.get("array")) if self.datamodel == "ecmascript": from PyV8 import JSContext c = JSContext(self.dm.g) c.enter() itr = enumerate(array, startIndex) # if self.datamodel == "xpath": # assert all(map(lambda x: x, array)) except ExprEvalError, e: raise AttributeEvalError(e, node, "array") except TypeError, e: err = DataModelError(e) raise AttributeEvalError(err, node, "array") for index, item in itr: try: if self.datamodel != "xpath": self.dm[node.get("item")] = item