コード例 #1
0
ファイル: commonjs.py プロジェクト: cash2one/weizoophoto
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:
コード例 #2
0
ファイル: commonjs.py プロジェクト: rhiokim/sublime-packages
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:
コード例 #3
0
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