Пример #1
0
def _init_stylesheet(profile):
    """Initialize custom stylesheets.

    Partially inspired by QupZilla:
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/app/mainapplication.cpp#L1063-L1101
    """
    old_script = profile.scripts().findScript('_qute_stylesheet')
    if not old_script.isNull():
        profile.scripts().remove(old_script)

    css = shared.get_user_stylesheet()
    source = '\n'.join([
        '"use strict";',
        'window._qutebrowser = window._qutebrowser || {};',
        utils.read_file('javascript/stylesheet.js'),
        javascript.assemble('stylesheet', 'set_css', css),
    ])

    script = QWebEngineScript()
    script.setName('_qute_stylesheet')
    script.setInjectionPoint(QWebEngineScript.DocumentCreation)
    script.setWorldId(QWebEngineScript.ApplicationWorld)
    script.setRunsOnSubFrames(True)
    script.setSourceCode(source)
    profile.scripts().insert(script)
Пример #2
0
def inject_userscripts():
    """Register user JavaScript files with the global profiles."""
    # The Greasemonkey metadata block support in QtWebEngine only starts at
    # Qt 5.8. With 5.7.1, we need to inject the scripts ourselves in response
    # to urlChanged.
    if not qtutils.version_check('5.8'):
        return

    # Since we are inserting scripts into profile.scripts they won't
    # just get replaced by new gm scripts like if we were injecting them
    # ourselves so we need to remove all gm scripts, while not removing
    # any other stuff that might have been added. Like the one for
    # stylesheets.
    greasemonkey = objreg.get('greasemonkey')
    for profile in [default_profile, private_profile]:
        scripts = profile.scripts()
        for script in scripts.toList():
            if script.name().startswith("GM-"):
                log.greasemonkey.debug('Removing script: {}'
                                       .format(script.name()))
                removed = scripts.remove(script)
                assert removed, script.name()

        # Then add the new scripts.
        for script in greasemonkey.all_scripts():
            # @run-at (and @include/@exclude/@match) is parsed by
            # QWebEngineScript.
            new_script = QWebEngineScript()
            new_script.setWorldId(QWebEngineScript.MainWorld)
            new_script.setSourceCode(script.code())
            new_script.setName("GM-{}".format(script.name))
            new_script.setRunsOnSubFrames(script.runs_on_sub_frames)
            log.greasemonkey.debug('adding script: {}'
                                   .format(new_script.name()))
            scripts.insert(new_script)
def _init_stylesheet(profile):
    """Initialize custom stylesheets.

    Mostly inspired by QupZilla:
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/app/mainapplication.cpp#L1063-L1101
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/tools/scripts.cpp#L119-L132

    FIXME:qtwebengine Use QWebEngineStyleSheet once that's available
    https://codereview.qt-project.org/#/c/148671/
    """
    old_script = profile.scripts().findScript('_qute_stylesheet')
    if not old_script.isNull():
        profile.scripts().remove(old_script)

    css = shared.get_user_stylesheet()
    source = """
        (function() {{
            var css = document.createElement('style');
            css.setAttribute('type', 'text/css');
            css.appendChild(document.createTextNode('{}'));
            document.getElementsByTagName('head')[0].appendChild(css);
        }})()
    """.format(javascript.string_escape(css))

    script = QWebEngineScript()
    script.setName('_qute_stylesheet')
    script.setInjectionPoint(QWebEngineScript.DocumentReady)
    script.setWorldId(QWebEngineScript.ApplicationWorld)
    script.setRunsOnSubFrames(True)
    script.setSourceCode(source)
    profile.scripts().insert(script)
def _init_stylesheet(profile):
    """Initialize custom stylesheets.

    Partially inspired by QupZilla:
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/app/mainapplication.cpp#L1063-L1101
    """
    old_script = profile.scripts().findScript('_qute_stylesheet')
    if not old_script.isNull():
        profile.scripts().remove(old_script)

    css = shared.get_user_stylesheet()
    source = '\n'.join([
        '"use strict";',
        'window._qutebrowser = window._qutebrowser || {};',
        utils.read_file('javascript/stylesheet.js'),
        javascript.assemble('stylesheet', 'set_css', css),
    ])

    script = QWebEngineScript()
    script.setName('_qute_stylesheet')
    script.setInjectionPoint(QWebEngineScript.DocumentCreation)
    script.setWorldId(QWebEngineScript.ApplicationWorld)
    script.setRunsOnSubFrames(True)
    script.setSourceCode(source)
    profile.scripts().insert(script)
Пример #5
0
def _init_stylesheet(profile):
    """Initialize custom stylesheets.

    Mostly inspired by QupZilla:
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/app/mainapplication.cpp#L1063-L1101
    https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/tools/scripts.cpp#L119-L132
    """
    old_script = profile.scripts().findScript('_qute_stylesheet')
    if not old_script.isNull():
        profile.scripts().remove(old_script)

    css = shared.get_user_stylesheet()
    source = """
        (function() {{
            var css = document.createElement('style');
            css.setAttribute('type', 'text/css');
            css.appendChild(document.createTextNode('{}'));
            document.getElementsByTagName('head')[0].appendChild(css);
        }})()
    """.format(javascript.string_escape(css))

    script = QWebEngineScript()
    script.setName('_qute_stylesheet')
    script.setInjectionPoint(QWebEngineScript.DocumentReady)
    script.setWorldId(QWebEngineScript.ApplicationWorld)
    script.setRunsOnSubFrames(True)
    script.setSourceCode(source)
    profile.scripts().insert(script)
Пример #6
0
    def _initPreviewToTextSync(self):
        """Initialize the system per items 1, 2, and 4 above."""
        # When a web page finishes loading, reinsert our JavaScript.
        page = self._dock._widget.webEngineView.page()

        # Insert our scripts into every loaded page.
        qwebchannel_js = QFile(':/qtwebchannel/qwebchannel.js')
        if not qwebchannel_js.open(QIODevice.ReadOnly):
            raise SystemExit(
                'Failed to load qwebchannel.js with error: %s' %
                qwebchannel_js.errorString())
        qwebchannel_js = bytes(qwebchannel_js.readAll()).decode('utf-8')

        # Set up the QWebChannel. See http://doc.qt.io/qt-5/qtwebchannel-javascript.html.
        # Run the script containing QWebChannel.js first.
        beforeScript = QWebEngineScript()
        beforeScript.setSourceCode(qwebchannel_js + self._jsPreviewSync + self._qtJsInit)
        beforeScript.setName('qwebchannel.js, previewSync')
        # Run this JavaScript separated from any JavaScript present in the loaded web page. This provides better security (rogue pages can't access the QWebChannel) and better isolation (handlers, etc. won't conflict, I hope).
        beforeScript.setWorldId(QWebEngineScript.ApplicationWorld)
        beforeScript.setInjectionPoint(QWebEngineScript.DocumentCreation)
        # Per `setWebChannel <http://doc.qt.io/qt-5/qwebenginepage.html#setWebChannel>`_, only one channel is allowed per page. So, don't run this on sub-frames, since it will attempt the creation of more channels for each subframe.
        beforeScript.setRunsOnSubFrames(False)
        page.scripts().insert(beforeScript)

        # Set up the web channel. See https://riverbankcomputing.com/pipermail/pyqt/2015-August/036346.html
        # and http://stackoverflow.com/questions/28565254/how-to-use-qt-webengine-and-qwebchannel.
        # For debug, ``set QTWEBENGINE_REMOTE_DEBUGGING=port`` then browse to
        # http://127.0.0.1:port, where port=60000 works for me. See https://riverbankcomputing.com/pipermail/pyqt/2015-August/036346.html.
        self.channel = QWebChannel(page)
        self.channel.registerObject("previewSync", self)
        # Expose the ``qt.webChannelTransport`` object in the world where these scripts live.
        page.setWebChannel(self.channel, QWebEngineScript.ApplicationWorld)
Пример #7
0
def client_script():
    """ Reads qtwebchannel.js from disk and creates QWebEngineScript to inject to QT window.
        This allows for JavaScript code to call python methods marked by pyqtSlot in registered objects.

    Args:
        None.

    Returns:
        None.

    Raises:
    """
    qwebchannel_js = QFile(':/qtwebchannel/qwebchannel.js')
    if not qwebchannel_js.open(QIODevice.ReadOnly):
        raise SystemExit(
            'Failed to load qwebchannel.js with error: %s' %
            qwebchannel_js.errorString())
    qwebchannel_js = bytes(qwebchannel_js.readAll()).decode('utf-8')
    script = QWebEngineScript()
    script.setSourceCode(qwebchannel_js)
    script.setName('qWebChannelJS')
    script.setWorldId(QWebEngineScript.MainWorld)
    script.setInjectionPoint(QWebEngineScript.DocumentReady)
    script.setRunsOnSubFrames(True)
    return script
Пример #8
0
    def _inject_early_js(self,
                         name,
                         js_code,
                         *,
                         world=QWebEngineScript.ApplicationWorld,
                         subframes=False):
        """Inject the given script to run early on a page load.

        This runs the script both on DocumentCreation and DocumentReady as on
        some internal pages, DocumentCreation will not work.

        That is a WORKAROUND for https://bugreports.qt.io/browse/QTBUG-66011
        """
        scripts = self._widget.page().scripts()
        for injection in ['creation', 'ready']:
            injection_points = {
                'creation': QWebEngineScript.DocumentCreation,
                'ready': QWebEngineScript.DocumentReady,
            }
            script = QWebEngineScript()
            script.setInjectionPoint(injection_points[injection])
            script.setSourceCode(js_code)
            script.setWorldId(world)
            script.setRunsOnSubFrames(subframes)
            script.setName('_qute_{}_{}'.format(name, injection))
            scripts.insert(script)
Пример #9
0
    def _inject_userscripts(self):
        """Register user JavaScript files with the global profiles."""
        # The Greasemonkey metadata block support in QtWebEngine only starts at
        # Qt 5.8. With 5.7.1, we need to inject the scripts ourselves in
        # response to urlChanged.
        if not qtutils.version_check('5.8'):
            return

        # Since we are inserting scripts into profile.scripts they won't
        # just get replaced by new gm scripts like if we were injecting them
        # ourselves so we need to remove all gm scripts, while not removing
        # any other stuff that might have been added. Like the one for
        # stylesheets.
        greasemonkey = objreg.get('greasemonkey')
        scripts = self._widget.page().scripts()
        for script in scripts.toList():
            if script.name().startswith("GM-"):
                log.greasemonkey.debug('Removing script: {}'.format(
                    script.name()))
                removed = scripts.remove(script)
                assert removed, script.name()

        # Then add the new scripts.
        for script in greasemonkey.all_scripts():
            # @run-at (and @include/@exclude/@match) is parsed by
            # QWebEngineScript.
            new_script = QWebEngineScript()
            new_script.setWorldId(QWebEngineScript.MainWorld)
            new_script.setSourceCode(script.code())
            new_script.setName("GM-{}".format(script.name))
            new_script.setRunsOnSubFrames(script.runs_on_sub_frames)
            log.greasemonkey.debug('adding script: {}'.format(
                new_script.name()))
            scripts.insert(new_script)
Пример #10
0
    def _initPreviewToTextSync(self):
        """Initialize the system per items 1, 2, and 4 above."""
        # When a web page finishes loading, reinsert our JavaScript.
        page = self._dock._widget.webEngineView.page()

        # Insert our scripts into every loaded page.
        qwebchannel_js = QFile(':/qtwebchannel/qwebchannel.js')
        if not qwebchannel_js.open(QIODevice.ReadOnly):
            raise SystemExit('Failed to load qwebchannel.js with error: %s' %
                             qwebchannel_js.errorString())
        qwebchannel_js = bytes(qwebchannel_js.readAll()).decode('utf-8')

        # Set up the QWebChannel. See http://doc.qt.io/qt-5/qtwebchannel-javascript.html.
        # Run the script containing QWebChannel.js first.
        beforeScript = QWebEngineScript()
        beforeScript.setSourceCode(qwebchannel_js + self._jsPreviewSync +
                                   self._qtJsInit)
        beforeScript.setName('qwebchannel.js, previewSync')
        # Run this JavaScript separated from any JavaScript present in the loaded web page. This provides better security (rogue pages can't access the QWebChannel) and better isolation (handlers, etc. won't conflict, I hope).
        beforeScript.setWorldId(QWebEngineScript.ApplicationWorld)
        beforeScript.setInjectionPoint(QWebEngineScript.DocumentCreation)
        # Per `setWebChannel <http://doc.qt.io/qt-5/qwebenginepage.html#setWebChannel>`_, only one channel is allowed per page. So, don't run this on sub-frames, since it will attempt the creation of more channels for each subframe.
        beforeScript.setRunsOnSubFrames(False)
        page.scripts().insert(beforeScript)

        # Set up the web channel. See https://riverbankcomputing.com/pipermail/pyqt/2015-August/036346.html
        # and http://stackoverflow.com/questions/28565254/how-to-use-qt-webengine-and-qwebchannel.
        # For debug, ``set QTWEBENGINE_REMOTE_DEBUGGING=port`` then browse to
        # http://127.0.0.1:port, where port=60000 works for me. See https://riverbankcomputing.com/pipermail/pyqt/2015-August/036346.html.
        self.channel = QWebChannel(page)
        self.channel.registerObject("previewSync", self)
        # Expose the ``qt.webChannelTransport`` object in the world where these scripts live.
        page.setWebChannel(self.channel, QWebEngineScript.ApplicationWorld)
Пример #11
0
def _createWebengineScript(path: Url, name: str, injectionPoint=None, isStylesheet: bool = False) -> QWebEngineScript:

    if injectionPoint is None:
        injectionPoint = QWebEngineScript.DocumentCreation

    script = QWebEngineScript()
    script_file = QFile(path)

    if script_file.open(QFile.ReadOnly):
        script_string = str(script_file.readAll(), 'utf-8')
        script.setInjectionPoint(injectionPoint)
        script.setName(name)
        script.setRunsOnSubFrames(True)
        script.setWorldId(QWebEngineScript.MainWorld)
        if isStylesheet:
            source = ("(function(){"
                      ""
                      "const css = document.createElement('style');\n"
                      "css.type = 'text/css';\n"
                      "css.innerText = `" + script_string.strip() + "`\n"
                      "document.head.appendChild(css);\n"
                      "})()")
            script.setSourceCode(source)
        else:
            script.setSourceCode(script_string)

    return script
Пример #12
0
    def setupUserScripts(self):
        # WebChannel for SafeJsWorld
        script = QWebEngineScript()
        script.setName('_app_webchannel')
        script.setInjectionPoint(QWebEngineScript.DocumentCreation)
        script.setWorldId(WebPage.SafeJsWorld)
        script.setRunsOnSubFrames(True)
        script.setSourceCode(Scripts.setupWebChannel())
        self._webProfile.scripts().insert(script)

        # app:restore
        appRestore = QWebEngineScript()
        appRestore.setWorldId(WebPage.SafeJsWorld)
        appRestore.setSourceCode(
            gVar.appTools.readAllFileContents(':html/restore.user.js'))
        self._webProfile.scripts().insert(appRestore)

        # app:speeddial
        appSpeedDial = QWebEngineScript()
        appSpeedDial.setWorldId(WebPage.SafeJsWorld)
        appSpeedDial.setSourceCode(Scripts.setupSpeedDial())
        self._webProfile.scripts().insert(appSpeedDial)

        # document.window object addons
        documentWindowAddons = QWebEngineScript()
        documentWindowAddons.setName('_app_window_object')
        documentWindowAddons.setInjectionPoint(
            QWebEngineScript.DocumentCreation)
        documentWindowAddons.setWorldId(WebPage.UnsafeJsWorld)
        documentWindowAddons.setRunsOnSubFrames(True)
        documentWindowAddons.setSourceCode(Scripts.setupWindowObject())
        self._webProfile.scripts().insert(documentWindowAddons)
    def load_css(self, path, name):
        path = QFile(path)
        if not path.open(QFile.ReadOnly | QtCore.QFile.Text):
            return
        css = path.readAll().data().decode("utf-8")
        SCRIPT = """
        (function() {
        try {
        css = document.createElement('style');
        css.type = 'text/css';
        css.id = "%s";
        document.head.appendChild(css);
        css.innerText = `%s`;
        } catch(e) {}
        })()
        """ % (name, css)

        script = QWebEngineScript()
        self.web_page.runJavaScript(SCRIPT, QWebEngineScript.ApplicationWorld)
        script.setName(name)
        script.setSourceCode(SCRIPT)
        script.setInjectionPoint(QWebEngineScript.DocumentReady)
        script.setRunsOnSubFrames(True)
        script.setWorldId(QWebEngineScript.ApplicationWorld)
        self.web_page.scripts().insert(script)
Пример #14
0
 def injectJs(self, source, name):
     js = QWebEngineScript()
     js.setName(name)
     js.setSourceCode(source)
     js.setInjectionPoint(QWebEngineScript.DocumentCreation)
     js.setWorldId(QWebEngineScript.MainWorld)
     js.setRunsOnSubFrames(True)
     self.scripts().insert(js)
Пример #15
0
 def create_script(src, name):
     s = QWebEngineScript()
     s.setName(name)
     s.setInjectionPoint(QWebEngineScript.DocumentReady)
     s.setWorldId(QWebEngineScript.ApplicationWorld)
     s.setRunsOnSubFrames(False)
     s.setSourceCode(src)
     return s
Пример #16
0
 def injectJS(self, sourceCode, name):
     script = QWebEngineScript()
     script.setSourceCode(sourceCode)
     script.setName(name)
     script.setInjectionPoint(QWebEngineScript.DocumentCreation)
     script.setWorldId(QWebEngineScript.MainWorld)
     script.setRunsOnSubFrames(True)
     self.view.page.scripts().insert(script)
     self.page.scripts().insert(script)
Пример #17
0
 def runScript(self) -> None:
     # self.page().runJavaScript(navi_scripts, QWebEngineScript.ApplicationWorld)
     script = QWebEngineScript()
     script.setName("atcoder-optimizer")
     script.setSourceCode(navi_script)
     script.setInjectionPoint(QWebEngineScript.DocumentReady)
     script.setRunsOnSubFrames(True)
     script.setWorldId(QWebEngineScript.ApplicationWorld)
     self.page().scripts().insert(script)
Пример #18
0
 def injectJS(self,sourceCode,name):
     script = QWebEngineScript();
     script.setSourceCode(sourceCode)
     script.setName(name)
     script.setInjectionPoint(QWebEngineScript.DocumentCreation)
     script.setWorldId(QWebEngineScript.MainWorld)
     script.setRunsOnSubFrames(True)
     self.view.page.scripts().insert(script)
     self.page.scripts().insert(script)
Пример #19
0
 def _add_script(script, injection_point):
     new_script = QWebEngineScript()
     new_script.setInjectionPoint(injection_point)
     new_script.setWorldId(QWebEngineScript.MainWorld)
     new_script.setSourceCode(script.code())
     new_script.setName("GM-{}".format(script.name))
     new_script.setRunsOnSubFrames(script.runs_on_sub_frames)
     log.greasemonkey.debug("Adding script: {}"
                            .format(new_script.name()))
     scripts.insert(new_script)
Пример #20
0
    def inject_script(self):
        script = QWebEngineScript()
        script.setSourceCode('''
            console.log("hi javascript!");

        ''')
        script.setWorldId(QWebEngineScript.MainWorld)
        script.setInjectionPoint(QWebEngineScript.DocumentReady)
        script.setRunsOnSubFrames(False)
        self.page().scripts().insert(script)
Пример #21
0
def create_script(name, src, world=QWebEngineScript.ApplicationWorld, injection_point=QWebEngineScript.DocumentReady, on_subframes=True):
    script = QWebEngineScript()
    if isinstance(src, bytes):
        src = src.decode('utf-8')
    script.setSourceCode(src)
    script.setName(name)
    script.setWorldId(world)
    script.setInjectionPoint(injection_point)
    script.setRunsOnSubFrames(on_subframes)
    return script
Пример #22
0
 def _add_script(script, injection_point):
     new_script = QWebEngineScript()
     new_script.setInjectionPoint(injection_point)
     new_script.setWorldId(QWebEngineScript.MainWorld)
     new_script.setSourceCode(script.code())
     new_script.setName("GM-{}".format(script.name))
     new_script.setRunsOnSubFrames(script.runs_on_sub_frames)
     log.greasemonkey.debug("Adding script: {}".format(
         new_script.name()))
     scripts.insert(new_script)
Пример #23
0
 def _onloadJS(self,
               code,
               name='',
               injection_point=QWebEngineScript.DocumentReady):
     script = QWebEngineScript()
     script.setName(name or ('script_' + str(random())[2:]))
     script.setSourceCode(code)
     script.setInjectionPoint(injection_point)
     script.setWorldId(script.MainWorld)
     script.setRunsOnSubFrames(False)
     self.page().scripts().insert(script)
     self.loadStarted.connect(lambda: self.page().scripts().insert(script))
Пример #24
0
 def webScript(self):
     """
     Public method to create a script object.
     
     @return prepared script object
     @rtype QWebEngineScript
     """
     script = QWebEngineScript()
     script.setSourceCode("{0}\n{1}".format(bootstrap_js, self.__script))
     script.setName(self.fullName())
     script.setWorldId(WebBrowserPage.SafeJsWorld)
     script.setRunsOnSubFrames(not self.__noFrames)
     return script
Пример #25
0
 def getProfile(self):
     profile=QWebEngineProfile("myProfile")
     profile.cachePath="/home/yyk/Desktop/cache"
     jsFile = constants.QTWEBCHANNELJS_FILE
     with open(jsFile, encoding="UTF-8") as file:
         js = file.read()
     script = QWebEngineScript();
     script.setSourceCode(js)
     script.setName('qwebchannel.js')
     script.setInjectionPoint(QWebEngineScript.DocumentCreation)
     script.setWorldId(QWebEngineScript.MainWorld)
     script.setRunsOnSubFrames(False)
     profile.scripts().insert(script)
     return profile
Пример #26
0
        def inject_js(filepath,
                      ipoint=QWebEngineScript.DocumentCreation,
                      iid=QWebEngineScript.ApplicationWorld,
                      sub_frames=False):
            f = QFile(filepath)
            assert f.open(QFile.ReadOnly | QFile.Text)
            src = QTextStream(f).readAll()

            script = QWebEngineScript()
            script.setInjectionPoint(ipoint)
            script.setSourceCode(src)
            script.setWorldId(iid)
            script.setRunsOnSubFrames(sub_frames)
            self.q_profile.scripts().insert(script)
Пример #27
0
    def inject(page, options: dict) -> None:
        if bindings() == "PyQt5":
            from PyQt5.QtWebEngineWidgets import QWebEngineScript
        else:
            from PySide2.QtWebEngineWidgets import QWebEngineScript

        script = QWebEngineScript()
        script.setName(options["name"])
        script.setWorldId(QWebEngineScript.MainWorld)
        script.setInjectionPoint(QWebEngineScript.DocumentCreation)
        script.setRunsOnSubFrames(True)
        script.setSourceCode(options["JavaScript"])
        print(f"Injecting JavaScript {options['name']}")
        page.profile().scripts().insert(script)
Пример #28
0
 def getProfile(self):
     profile = QWebEngineProfile("myProfile")
     profile.cachePath = "/home/yyk/Desktop/cache"
     jsFile = constants.QTWEBCHANNELJS_FILE
     with open(jsFile, encoding="UTF-8") as file:
         js = file.read()
     script = QWebEngineScript()
     script.setSourceCode(js)
     script.setName('qwebchannel.js')
     script.setInjectionPoint(QWebEngineScript.DocumentCreation)
     script.setWorldId(QWebEngineScript.MainWorld)
     script.setRunsOnSubFrames(False)
     profile.scripts().insert(script)
     return profile
Пример #29
0
 def __init__(self):
     self.log = default_log
     self.current_frag = None
     self.com_id = unicode_type(uuid4())
     QWebEnginePage.__init__(self)
     secure_webengine(self.settings(), for_viewer=True)
     self.titleChanged.connect(self.title_changed)
     self.loadFinished.connect(self.show_frag)
     s = QWebEngineScript()
     s.setName('toc.js')
     s.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentReady)
     s.setRunsOnSubFrames(True)
     s.setWorldId(QWebEngineScript.ScriptWorldId.ApplicationWorld)
     s.setSourceCode(P('toc.js', allow_user_override=False, data=True).decode('utf-8').replace('COM_ID', self.com_id))
     self.scripts().insert(s)
Пример #30
0
    def inject_javascript_file(self, javascript_file: str) -> NoReturn:
        if not os.path.exists(javascript_file):
            raise Exception("javascript file is not exists, filename=%s" % javascript_file)

        script = QWebEngineScript()
        with codecs.open(javascript_file, "r", "utf-8") as f:
            script.setSourceCode(f.read())
        script.setName(os.path.basename(javascript_file))
        script.setWorldId(QWebEngineScript.MainWorld)
        script.setInjectionPoint(QWebEngineScript.DocumentCreation)
        script.setRunsOnSubFrames(False)

        profile = self._web_engine_view.page().profile() # type: QWebEngineProfile
        scripts = profile.scripts() # type: QWebEngineScriptCollection
        scripts.insert(script)
        logger.info("inject javascript file %s" % javascript_file)
Пример #31
0
    def getProfile(self):

        path = constants.QWEBENGINECACHE_PATH
        tryMkdir(path)
        profile = QWebEngineProfile()
        profile.setCachePath(path)
        profile.clearHttpCache()
        jsFile = constants.QTWEBCHANNELJS_FILE
        with open(jsFile, encoding="UTF-8") as file:
            js = file.read()
        script = QWebEngineScript()
        script.setSourceCode(js)
        script.setName('qwebchannel.js')
        script.setInjectionPoint(QWebEngineScript.DocumentCreation)
        script.setWorldId(QWebEngineScript.MainWorld)
        script.setRunsOnSubFrames(False)
        profile.scripts().insert(script)
        return profile
Пример #32
0
def orbitScripts(connParams):
    scripts = []
    jsFile = QFile(':/share/js/orbit-db/orbitdb.js')
    if not jsFile.open(QFile.ReadOnly):
        return
    scriptJsIpfs = QWebEngineScript()
    scriptJsIpfs.setName('orbit-db')
    scriptJsIpfs.setSourceCode(jsFile.readAll().data().decode('utf-8'))
    scriptJsIpfs.setWorldId(QWebEngineScript.MainWorld)
    scriptJsIpfs.setInjectionPoint(QWebEngineScript.DocumentCreation)
    scriptJsIpfs.setRunsOnSubFrames(True)
    scripts.append(scriptJsIpfs)

    script = QWebEngineScript()
    script.setSourceCode('\n'.join([
        "document.addEventListener('DOMContentLoaded', function () {",
        "window.orbitdb = new OrbitDB(window.ipfs)", "})"
    ]))
    script.setWorldId(QWebEngineScript.MainWorld)
    return scripts
Пример #33
0
def ipfsClientScripts(connParams):
    scripts = []
    jsFile = QFile(':/share/js/ipfs-http-client/index.min.js')
    if not jsFile.open(QFile.ReadOnly):
        return

    scriptJsIpfs = QWebEngineScript()
    scriptJsIpfs.setName('ipfs-http-client')

    libCode = jsFile.readAll().data().decode('utf-8')
    libCode += "\n"
    libCode += ipfsInjScript.format(host=connParams.host,
                                    port=connParams.apiPort)

    scriptJsIpfs.setSourceCode(libCode)
    scriptJsIpfs.setWorldId(QWebEngineScript.MainWorld)
    scriptJsIpfs.setInjectionPoint(QWebEngineScript.DocumentCreation)
    scriptJsIpfs.setRunsOnSubFrames(True)
    scripts.append(scriptJsIpfs)
    return scripts
Пример #34
0
    def insertStyleSheet(self):
        name = "table_style"
        s = """(function() {
    css = document.createElement('style');
    css.type = 'text/css';
    css.id = '%s';
    document.head.appendChild(css);
    css.innerText = '%s';
})()""" % (
            name,
            table_style,
        )
        self.page().runJavaScript(s, QWebEngineScript.ApplicationWorld)
        script = QWebEngineScript()
        script.setName(name)
        script.setSourceCode(s)
        script.setInjectionPoint(QWebEngineScript.DocumentReady)
        script.setRunsOnSubFrames(True)
        script.setWorldId(QWebEngineScript.ApplicationWorld)
        self.page().scripts().insert(script)
Пример #35
0
    def __init__(self):
        QWebEngineView.__init__(self)
        url = QDir().current().filePath(
            "html/QWebEngineView_07_print_request.html")
        url = QUrl.fromLocalFile(url)
        self.setUrl(url)
        web_channel_js = QWebEngineScript()
        web_channel_js.setInjectionPoint(QWebEngineScript.DocumentCreation)
        web_channel_js.setWorldId(QWebEngineScript.MainWorld)
        web_channel_js.setRunsOnSubFrames(True)
        web_channel_js_file = QFile(":/qwebchannel")
        web_channel_js_file.open(QFile.ReadOnly)
        js = str(web_channel_js_file.readAll(), 'utf-8')
        web_channel_js.setSourceCode(js)
        self.page().scripts().insert(web_channel_js)

        # Javascript window.print()를 오버라이드
        # 기본 window.print()는 웹브라우져에게 프린터 다이얼로그를 요청하지만
        # 받을 수 없기 때문에 QWebChannel을 통해 받는다.
        override_js_print = QWebEngineScript()
        override_js_print.setInjectionPoint(QWebEngineScript.DocumentCreation)
        override_js_print.setWorldId(QWebEngineScript.MainWorld)
        override_js_print.setName("oveerridejsprint.js")
        override_js_print.setRunsOnSubFrames(True)
        override_js_print.setSourceCode(
            "window.print = function() { "
            "   new QWebChannel(qt.webChannelTransport, function(channel) { "
            "       handler = channel.objects.handler; "
            "       handler.webToAppRequestPrint(); "
            "   });"
            "};")
        self.page().scripts().insert(override_js_print)
        channel = QWebChannel(self.page())
        self.page().setWebChannel(channel)
        self.handler = CallHandler()
        channel.registerObject('handler', self.handler)

        # 시그널슬롯 처리
        self.handler.request_print.connect(self.printer)
Пример #36
0
    def setUserStyleSheet(self, filePath):
        userCss = ''
        if not const.OS_UNIX and not const.OS_MACOS:
            # Don't grey out selection on losing focus (to prevent graying out
            # found text)
            highlightColor = ''
            highlightedTextColor = ''
            if const.OS_MACOS:  # TODO: ? can not enter this branch
                highlightColor = '#b6d6fc'
                highlightedTextColor = '#000'
            else:
                pal = self.style().standardPalette()
                highlightColor = pal.color(QPalette.Highlight).name()
                highlightedTextColor = pal.color(
                    QPalette.HighlightedText).name()
            userCss += "::selection {background: %s; color: %s;} " % \
                (highlightColor, highlightedTextColor)

        userCss += gVar.appTools.readAllFileContents(filePath).replace(
            '\n', '')

        name = '_app_userstylesheet'

        oldScript = self._webProfile.scripts().findScript(name)
        if not oldScript.isNull():
            self._webProfile.scripts().remove(oldScript)

        if not userCss:
            return

        script = QWebEngineScript()
        script.setName(name)
        script.setInjectionPoint(QWebEngineScript.DocumentReady)
        script.setWorldId(WebPage.SafeJsWorld)
        script.setRunsOnSubFrames(True)
        script.setSourceCode(Scripts.setCss(userCss))
        self._webProfile.scripts().insert(script)
Пример #37
0
class BalanceView(QWebEngineView):
	def __init__(self):
		QWebEngineView.__init__(self)
		self.index = 0
		self.page().loadFinished.connect(self.loadFinished)
		self.page().loadProgress.connect(self.loadProgress)

		self.script = QWebEngineScript()
		self.script.setInjectionPoint(QWebEngineScript.Deferred)
		self.script.setRunsOnSubFrames(True)

		self.urlLogin = QUrl("http://reg.163.com/Logout.jsp?url=http://ecard.163.com/account/query_balance")
		self.urlVerify = QUrl("http://ecard.163.com/handle_login")
		self.urlBalance = QUrl("http://ecard.163.com/handle_query_verify")

		self.queryCallBack = None
		self.setProgress = None

	def loadFinished(self, isOk):
		if isOk:
			requestedUrl = self.page().requestedUrl()
			if requestedUrl == self.urlLogin:
				self.page().runJavaScript("""
					if(typeof($) != "undefined"){
						//#footer,#header,#urs_tab,#phone_tab删掉
						$('#footer').remove()
						$('#header').remove()
						$('#urs_tab').remove()
						$('#phone_tab').remove()
						//.nowbg的class属性删掉
						$('.nowbg').removeAttr('class')
						//#wrap的min-width属性去掉
						$('#wrap').css('min-width', 'initial');
						//#center的width属性去掉
						$('#center').css('width', 'initial');
						//#login的border-top,height,position,right,top属性去掉,加margin:auto;
						$('#login').css('border-top', 'initial');
						$('#login').css('height', 'initial');
						$('#login').css('position', 'initial');
						$('#login').css('right', 'initial');
						$('#login').css('top', 'initial');
						$('#login').css('margin', 'auto');
					}
				""")
			elif requestedUrl == self.urlVerify:
				self.page().runJavaScript("""
					//#footer,#header,.situation删掉
					$('#footer').remove()
					$('#header').remove()
					$('.situation').remove()
					//#wrap的min-width属性去掉
					$('#wrap').css('min-width', 'initial');
					//#main的width属性去掉
					$('#main').css('width', 'initial');
					//.mb-content的margin: 100px auto 78px auto;
					$('.mb-content').css('margin', '100px auto 78px auto')
					function verifyFunc(){
						if($('.TxtStatus').children().length == 1){
							clearInterval(verifyId);
							$('#query_verify_form').submit();
						}
					}
					var verifyId = setInterval(verifyFunc, 100);
				""")
			elif requestedUrl == self.urlBalance:
				self.page().runJavaScript("document.getElementsByClassName('red bold')[2].innerText", self.jsCallback)
		else:
			print("load error")

	def loadProgress(self, progress):
		if self.setProgress:
			self.setProgress(progress)

	def setLoadProgress(self, callback):
		self.setProgress = callback

	def jsCallback(self, balance):
		if self.queryCallBack:
			self.queryCallBack(self.accounts[self.index][0], balance)
		self.index = self.index + 1
		self.startQuery()

	def setAccounts(self, accounts):
		self.accounts = accounts

	def startQuery(self, queryCallBack=None):
		if queryCallBack:
			self.queryCallBack = queryCallBack

		if self.index < len(self.accounts):
			account = self.accounts[self.index]
			self.script.setSourceCode("""
				var box = document.getElementById('cnt-box');
				if(box){
					function loginFunc(){
						if(box.children.length == 4 && document.getElementById('nerror').children.length == 2){
							clearInterval(loginId);
							email = document.getElementsByName('email');email[0].value='%s';
							password = document.getElementsByName('password');password[0].value='%s';
							document.getElementById('dologin').click();
						}
					}
					var loginId = setInterval(loginFunc, 100);
					function verifyFunc(){
						if(document.getElementsByClassName('u-suc').length == 2){
							clearInterval(verifyId);
							document.getElementById('dologin').click();
						}
					}
					var verifyId = setInterval(verifyFunc, 100);
				}
			""" % (account[0], account[1]))
			self.page().scripts().insert(self.script)
			self.load(self.urlLogin)
Пример #38
0
    def _build_app(self):
        if self._inspector:
            os.environ.setdefault('QTWEBENGINE_REMOTE_DEBUGGING', '8001')

        # build webengine container
        self._lensview = lv = _QWebView(inspector=self._inspector)
        self._page = p = _QWebPage()
        lv.setPage(self._page)

        # connect to Qt signals
        lv.loadFinished.connect(self._loaded_cb)
        self._app.lastWindowClosed.connect(self._last_window_closed_cb)

        # build webchannel script and inject
        qwebchannel_js = QFile(':/qtwebchannel/qwebchannel.js')

        if not qwebchannel_js.open(QIODevice.ReadOnly):
            raise SystemExit('Failed to load qwebchannel.js with error: %s' % qwebchannel_js.errorString())

        qwebchannel_js = bytes(qwebchannel_js.readAll()).decode('utf-8')

        script = QWebEngineScript()
        script.setSourceCode(qwebchannel_js + '''
                window.lens = window.lens || {};
                window.lens._channel = new QWebChannel(qt.webChannelTransport, function(channel) {
                    window.lens.emit = function() {
                        var args = Array.prototype.slice.call(arguments);
                        if (args.length > 0) {
                            channel.objects.bridge._from_bridge(args);
                        }
                    };

                    channel.objects.bridge._emit_js_signal.connect(function(name, args) {
                        window.lens.__broadcast(name, args);
                    });
                });
            ''')
        script.setName('lens-bridge')
        script.setWorldId(QWebEngineScript.MainWorld)
        script.setInjectionPoint(QWebEngineScript.DocumentCreation)
        script.setRunsOnSubFrames(True)

        p.profile().scripts().insert(script)

        self._channel = QWebChannel(p)
        p.setWebChannel(self._channel)
        self._channel.registerObject('bridge', self)

        # set up scheme handlers for app:// and lens://
        self._app_scheme_handler = AppSchemeHandler()
        self._lens_scheme_handler = LensSchemeHandler()

        self._interceptor = RequestInterceptor()

        self._profile = QWebEngineProfile().defaultProfile()
        self._profile.installUrlSchemeHandler('app'.encode(), self._app_scheme_handler)
        self._profile.installUrlSchemeHandler('lens'.encode(), self._lens_scheme_handler)

        self._profile.setRequestInterceptor(self._interceptor)

        # connect to Lens signals
        self.on('__close_app', self._close_cb)

        # center on screen
        _frame_geometry = lv.frameGeometry()
        _active_screen = self._app.desktop().screenNumber(self._app.desktop().cursor().pos())
        _center = self._app.desktop().screenGeometry(_active_screen).center()
        _frame_geometry.moveCenter(_center)
        lv.move(_frame_geometry.topLeft())

        self.set_title(self._app_name)
        self.set_size(self._app_width, self._app_height)