Ejemplo n.º 1
0
    def __init__(self):
        wx.Frame.__init__(self,
                          parent=None,
                          id=wx.ID_ANY,
                          title='wxPython example',
                          size=(800, 600))
        self.CreateMenu()

        # Cannot attach browser to the main frame as this will cause
        # the menu not to work.
        self.mainPanel = wx.Panel(self)

        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(self.mainPanel.GetGtkWidget())
        # Linux requires adding "file://" for local files,
        # otherwise /home/some will be replaced as http://home/some
        self.browser = cefpython.CreateBrowserSync(
            windowInfo,
            # Flash will crash app in CEF 1 on Linux, setting
            # plugins_disabled to True.
            browserSettings={"plugins_disabled": True},
            navigateUrl="file://" + GetApplicationPath("wxpython.html"))

        self.browser.SetClientHandler(ClientHandler())

        jsBindings = cefpython.JavascriptBindings(bindToFrames=False,
                                                  bindToPopups=False)
        jsBindings.SetObject("external", JavascriptBindings(self.browser))
        self.browser.SetJavascriptBindings(jsBindings)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE:
            # Bind EVT_IDLE only for the main application frame.
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 2
0
 def set_js_bindings(self):
     if not self._js_bindings:
         self._js_bindings = cefpython.JavascriptBindings(bindToFrames=True,
                                                          bindToPopups=True)
         self._js_bindings.SetFunction("__kivy__request_keyboard",
                                       self.request_keyboard)
         self._js_bindings.SetFunction("__kivy__release_keyboard",
                                       self.release_keyboard)
     self.browser.SetJavascriptBindings(self._js_bindings)
Ejemplo n.º 3
0
def CefAdvanced():
    sys.excepthook = ExceptHook
    InitDebugging()

    appSettings = dict()
    appSettings["log_file"] = GetApplicationPath("debug.log")

    # LOGSEVERITY_INFO - less debug oput.
    # LOGSEVERITY_DISABLE - will not create "debug.log" file.
    appSettings["log_severity"] = cefpython.LOGSEVERITY_VERBOSE

    # Enable only when debugging, otherwise performance might hurt.
    appSettings["release_dcheck_enabled"] = True

    # Must be set so that OnUncaughtException() is called.
    appSettings["uncaught_exception_stack_size"] = 100

    cefpython.Initialize(applicationSettings=appSettings)

    # Closing main window quits the application as we define
    # WM_DESTOROY message.
    wndproc = {
        win32con.WM_CLOSE: CloseWindow,
        win32con.WM_DESTROY: QuitApplication,
        win32con.WM_SIZE: cefpython.WindowUtils.OnSize,
        win32con.WM_SETFOCUS: cefpython.WindowUtils.OnSetFocus,
        win32con.WM_ERASEBKGND: cefpython.WindowUtils.OnEraseBackground
    }

    windowHandle = cefwindow.CreateWindow(
            title="CefAdvanced", className="cefadvanced",
            width=900, height=710, icon="icon.ico", windowProc=wndproc)

    browserSettings = dict()
    browserSettings["history_disabled"] = False
    browserSettings["universal_access_from_file_urls_allowed"] = True
    browserSettings["file_access_from_file_urls_allowed"] = True

    javascriptBindings = cefpython.JavascriptBindings(
            bindToFrames=False, bindToPopups=True)
    windowInfo = cefpython.WindowInfo()
    windowInfo.SetAsChild(windowHandle)
    browser = cefpython.CreateBrowserSync(
            windowInfo, browserSettings=browserSettings,
            navigateUrl=GetApplicationPath("cefadvanced.html"))
    browser.SetUserData("outerWindowHandle", windowInfo.parentWindowHandle)
    browser.SetClientHandler(ClientHandler())
    browser.SetJavascriptBindings(javascriptBindings)

    javascriptRebindings = JavascriptRebindings(javascriptBindings, browser)
    javascriptRebindings.Bind()
    browser.SetUserData("javascriptRebindings", javascriptRebindings)

    cefpython.MessageLoop()
    cefpython.Shutdown()
Ejemplo n.º 4
0
    def __init__(self):
        wx.Frame.__init__(self,
                          parent=None,
                          id=wx.ID_ANY,
                          title='wxPython CEF 3 example',
                          size=(800, 600))
        self.CreateMenu()

        # Cannot attach browser to the main frame as this will cause
        # the menu not to work.
        # --
        # You also have to set the wx.WANTS_CHARS style for
        # all parent panels/controls, if it's deeply embedded.
        self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS)

        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(self.mainPanel.GetGtkWidget())
        # Linux requires adding "file://" for local files,
        # otherwise /home/some will be replaced as http://home/some
        self.browser = cefpython.CreateBrowserSync(
            windowInfo,
            # If there are problems with Flash you can disable it here,
            # by disabling all plugins.
            browserSettings={"plugins_disabled": False},
            navigateUrl="file://" + GetApplicationPath("index.html"))

        clientHandler = ClientHandler()
        self.browser.SetClientHandler(clientHandler)
        cefpython.SetGlobalClientCallback("OnCertificateError",
                                          clientHandler._OnCertificateError)
        cefpython.SetGlobalClientCallback("OnBeforePluginLoad",
                                          clientHandler._OnBeforePluginLoad)

        jsBindings = cefpython.JavascriptBindings(bindToFrames=False,
                                                  bindToPopups=True)
        jsBindings.SetFunction("PyPrint", PyPrint)
        jsBindings.SetProperty("pyProperty", "This was set in Python")
        jsBindings.SetProperty("pyConfig", [
            "This was set in Python", {
                "name": "Nested dictionary",
                "isNested": True
            }, [1, "2", None]
        ])
        jsBindings.SetObject("external", JavascriptExternal(self.browser))
        self.browser.SetJavascriptBindings(jsBindings)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE:
            # Bind EVT_IDLE only for the main application frame.
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 5
0
 def __init__(self, parent=None):
     super(MainFrame, self).__init__(parent)
     windowInfo = cefpython.WindowInfo()
     windowInfo.SetAsChild(int(self.winId()))
     self.browser = cefpython.CreateBrowserSync(
         windowInfo,
         browserSettings={},
         navigateUrl=GetApplicationPath("example.html"))
     jsBindings = cefpython.JavascriptBindings(bindToFrames=False,
                                               bindToPopups=True)
     jsBindings.SetFunction("py_multiply", funcs.multiply)
     self.browser.SetJavascriptBindings(jsBindings)
     self.browser.GetMainFrame().ExecuteJavascript(
         'alert("direct call from python")')
     self.show()
Ejemplo n.º 6
0
 def set_js_bindings(self):
     # Needed to introduce set_js_bindings again because the freeze of sites at load took over.
     # As an example 'http://www.htmlbasix.com/popup.shtml' freezed every time. By setting the js
     # bindings again, the freeze rate is at about 35%. Check git to see how it was done, before using
     # this function ...
     # I (jegger) have to be honest, that I don't have a clue why this is acting like it does!
     # I hope simon (REN-840) can resolve this once in for all...
     #
     # ORIGINAL COMMENT:
     # When browser.Navigate() is called, some bug appears in CEF
     # that makes CefRenderProcessHandler::OnBrowserDestroyed()
     # is being called. This destroys the javascript bindings in
     # the Render process. We have to make the js bindings again,
     # after the call to Navigate() when OnLoadingStateChange()
     # is called with isLoading=False. Problem reported here:
     # http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=11009
     if not self._js_bindings:
         self._js_bindings = cefpython.JavascriptBindings(bindToFrames=True, bindToPopups=True)
         self._js_bindings.SetFunction("__kivy__keyboard_update", self.keyboard_update)
     self.browser.SetJavascriptBindings(self._js_bindings)
Ejemplo n.º 7
0
    def __init__(self):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
                          title='wxPython example', size=(800,600))
        self.CreateMenu()

        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(self.GetHandle())
        self.browser = cefpython.CreateBrowserSync(windowInfo,
                browserSettings={},
                navigateUrl=GetApplicationPath("wxpython.html"))

        self.browser.SetClientHandler(ClientHandler())
        jsBindings = cefpython.JavascriptBindings(
            bindToFrames=False, bindToPopups=False)
        jsBindings.SetObject("external", JavascriptBindings(self.browser))
        self.browser.SetJavascriptBindings(jsBindings)

        self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE:
            # Bind EVT_IDLE only for the main application frame.
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 8
0
    def __init__(self, uri, DataPool):
        wx.Frame.__init__(self,
                          parent=None,
                          id=wx.ID_ANY,
                          title='LliureX Resource Browser',
                          size=wx.DisplaySize())
        self.icon = wx.Icon("/usr/share/icons/lliurex-neu/48/apps/box.png",
                            wx.BITMAP_TYPE_PNG)
        self.SetIcon(self.icon)

        wx.Frame.ShowFullScreen(self, True, style=wx.FULLSCREEN_ALL)

        self.cefWindow = chrome.ChromeWindow(self, url=uri, useTimer=True)
        #windowInfo = cefpython.WindowInfo()
        #windowInfo.SetAsChild(self.GetHandle())
        #self.cefWindow.browser = cefpython.CreateBrowserSync(windowInfo=None, browserSettings={}, navigateURL=uri)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.SetSizeHints(self)
        sizer.Add(self.cefWindow, 1, wx.EXPAND, 0)
        self.SetSizer(sizer)
        self.Fit()
        self.Show()

        #bindings = cefpython.JavascriptBindings(bindToFrames=True, bindToPopups=True)

        #bindings = cefpython.JavascriptBindings(bindToFrames=False, bindToPopups=True)
        #bindings.SetFunction("getString", self.getString)
        #self.cefWindow.browser.SetJavascriptBindings(bindings)

        self.MyBindingsObject = MyBindings(self.cefWindow, DataPool)
        bindings = cefpython.JavascriptBindings(bindToFrames=False,
                                                bindToPopups=True)
        bindings.SetObject("MyBindings", self.MyBindingsObject)
        self.cefWindow.browser.SetJavascriptBindings(bindings)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
Ejemplo n.º 9
0
    def __init__(self, url=None, popup=False, params=None):
        if popup:
            title = os.path.basename(url)
        else:
            title = "Kam1n0"
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY, title=title)
        size = (800, 500)

        # icon
        #setup icon object
        #icon = wx.Icon(GetApplicationPath("www/img/favicon.ico"), wx.BITMAP_TYPE_ICO)

        #setup taskbar icon
        #tbicon = wx.TaskBarIcon()
        #tbicon.SetIcon(icon, "McGill Icon")
        loc = wx.IconLocation(GetApplicationPath("www/img/favicon.ico"), 0)
        self.SetIcon(wx.IconFromLocation(loc))

        # This is an optional code to enable High DPI support.
        if "auto_zooming" in g_applicationSettings \
                and g_applicationSettings["auto_zooming"] == "system_dpi":
            # This utility function will adjust width/height using
            # OS DPI settings. For 800/600 with Win7 DPI settings
            # being set to "Larger 150%" will return 1200/900.
            size = cefpython.DpiAware.CalculateWindowSize(size[0], size[1])

        self.SetSize(size)

        global gdata
        if gdata is None:
            gdata = GetData()

        gdp = json.loads(gdata)

        if not url:
            url = gdp['url']
            # Test hash in url.
            # url += "#test-hash"

        # self.CreateMenu()

        if TEST_EMBEDDING_IN_PANEL:
            print("Embedding in a wx.Panel!")
            # You also have to set the wx.WANTS_CHARS style for
            # all parent panels/controls, if it's deeply embedded.
            self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS)

        # Global client callbacks must be set before browser is created.
        self.clientHandler = ClientHandler()
        cefpython.SetGlobalClientCallback(
            "OnCertificateError", self.clientHandler._OnCertificateError)
        cefpython.SetGlobalClientCallback(
            "OnBeforePluginLoad", self.clientHandler._OnBeforePluginLoad)
        cefpython.SetGlobalClientCallback("OnAfterCreated",
                                          self.clientHandler._OnAfterCreated)

        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(self.GetHandleForBrowser())
        self.browser = cefpython.CreateBrowserSync(
            windowInfo, browserSettings=g_browserSettings, navigateUrl=url)

        self.clientHandler.mainBrowser = self.browser
        self.browser.SetClientHandler(self.clientHandler)

        jsBindings = cefpython.JavascriptBindings(bindToFrames=False,
                                                  bindToPopups=True)

        self.javascriptExternal = JavascriptExternal(self.browser, gdata)
        jsBindings.SetObject("external", self.javascriptExternal)
        jsBindings.SetProperty("GData", gdata)
        if not params is None:
            jsBindings.SetProperty("params", params)
        self.browser.SetJavascriptBindings(jsBindings)

        if self.mainPanel:
            self.mainPanel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
            self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize)
        else:
            self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
            self.Bind(wx.EVT_SIZE, self.OnSize)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE and not popup:
            # Bind EVT_IDLE only for the main application frame.
            print("Using EVT_IDLE to execute the CEF message loop work")
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 10
0
    def __init__(self,
                 parent,
                 title,
                 pos,
                 size,
                 style,
                 name,
                 url=None,
                 popup=True):

        wx.Frame.__init__(self,
                          parent=parent,
                          id=wx.ID_ANY,
                          title=title,
                          pos=pos,
                          size=size,
                          style=style,
                          name=name)
        size = (1000, 600)

        # This is an optional code to enable High DPI support.
        if "auto_zooming" in g_applicationSettings \
                and g_applicationSettings["auto_zooming"] == "system_dpi":
            # This utility function will adjust width/height using
            # OS DPI settings. For 800/600 with Win7 DPI settings
            # being set to "Larger 150%" will return 1200/900.
            size = cefpython.DpiAware.CalculateWindowSize(size[0], size[1])

        self.SetSize(size)
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(wx.WHITE)
        if not url:
            url = "file://" + GetApplicationPath("example2.html")
            # Test hash in url.
            # url += "#test-hash"

        #self.CreateMenu()

        if TEST_EMBEDDING_IN_PANEL:
            print("Embedding in a wx.Panel!")
            # You also have to set the wx.WANTS_CHARS style for
            # all parent panels/controls, if it's deeply embedded.
            self.mainPanel = wx.Panel(self.panel,
                                      size=(700, 635),
                                      pos=(600, 400))
        # Global client callbacks must be set before browser is created.
        self.clientHandler = ClientHandler()
        cefpython.SetGlobalClientCallback(
            "OnCertificateError", self.clientHandler._OnCertificateError)
        cefpython.SetGlobalClientCallback(
            "OnBeforePluginLoad", self.clientHandler._OnBeforePluginLoad)
        cefpython.SetGlobalClientCallback("OnAfterCreated",
                                          self.clientHandler._OnAfterCreated)

        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsChild(self.GetHandleForBrowser())
        self.browser = cefpython.CreateBrowserSync(
            windowInfo, browserSettings=g_browserSettings, navigateUrl=url)

        self.clientHandler.mainBrowser = self.browser
        self.browser.SetClientHandler(self.clientHandler)

        jsBindings = cefpython.JavascriptBindings(bindToFrames=True,
                                                  bindToPopups=True)
        jsBindings.SetFunction("PyPrint", PyPrint)
        jsBindings.SetProperty("pyProperty", "This was set in Python")
        jsBindings.SetProperty("pyConfig", [
            "This was set in Python", {
                "name": "Nested dictionary",
                "isNested": True
            }, [1, "2", None]
        ])
        self.javascriptExternal = JavascriptExternal(self.browser)
        jsBindings.SetObject("external", self.javascriptExternal)
        jsBindings.SetProperty("sources", GetSources())
        self.browser.SetJavascriptBindings(jsBindings)

        if self.mainPanel:
            self.mainPanel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
            self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize)
        else:
            self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
            self.Bind(wx.EVT_SIZE, self.OnSize)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE and not popup:
            # Bind EVT_IDLE only for the main application frame.
            print("Using EVT_IDLE to execute the CEF message loop work")
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 11
0
    def __init__(self, url=None):
        wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
                title='wxPython CEF 3 example', size=(800,600))

        global g_countWindows
        g_countWindows += 1

        if not url:
            url = "file://"+GetApplicationPath("wxpython.html")
            # Test hash in url.
            # url += "#test-hash"

        self.CreateMenu()

        # Cannot attach browser to the main frame as this will cause
        # the menu not to work.
        # --
        # You also have to set the wx.WANTS_CHARS style for
        # all parent panels/controls, if it's deeply embedded.
        self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS)

        # Global client callbacks must be set before browser is created.
        self.clientHandler = ClientHandler()
        cefpython.SetGlobalClientCallback("OnCertificateError",
                self.clientHandler._OnCertificateError)
        cefpython.SetGlobalClientCallback("OnBeforePluginLoad",
                self.clientHandler._OnBeforePluginLoad)
        cefpython.SetGlobalClientCallback("OnAfterCreated",
                self.clientHandler._OnAfterCreated)

        windowInfo = cefpython.WindowInfo()
        (width, height) = self.mainPanel.GetClientSizeTuple()
        windowInfo.SetAsChild(self.mainPanel.GetHandle(),
                              [0, 0, width, height])
        # Linux requires adding "file://" for local files,
        # otherwise /home/some will be replaced as http://home/some
        self.browser = cefpython.CreateBrowserSync(
            windowInfo,
            # If there are problems with Flash you can disable it here,
            # by disabling all plugins.
            browserSettings=g_browserSettings,
            navigateUrl=url)

        self.clientHandler.mainBrowser = self.browser
        self.browser.SetClientHandler(self.clientHandler)

        jsBindings = cefpython.JavascriptBindings(
            bindToFrames=False, bindToPopups=True)
        jsBindings.SetFunction("PyPrint", PyPrint)
        jsBindings.SetProperty("pyProperty", "This was set in Python")
        jsBindings.SetProperty("pyConfig", ["This was set in Python",
                {"name": "Nested dictionary", "isNested": True},
                [1,"2", None]])
        self.javascriptExternal = JavascriptExternal(self.browser)
        jsBindings.SetObject("external", self.javascriptExternal)
        jsBindings.SetProperty("sources", GetSources())
        self.browser.SetJavascriptBindings(jsBindings)

        self.Bind(wx.EVT_CLOSE, self.OnClose)
        if USE_EVT_IDLE:
            # Bind EVT_IDLE only for the main application frame.
            self.Bind(wx.EVT_IDLE, self.OnIdle)
Ejemplo n.º 12
0
def snap(command, width=800, height=600):
    metadata = {}
    try:
        logging.info("Snapshot url: %s" % command['url'])
        metadata['timestamp'] = int(time())
        metadata['error'] = "0"
        parent_dir = os.path.dirname(command['file'])
        if parent_dir and not os.path.exists(parent_dir):
            os.makedirs(parent_dir)
        if 'screen_width' in command:
            width = command['screen_width']
        if 'screen_height' in command:
            height = command['screen_height']
        cefpython.g_debug = False
        commandLineSwitches = dict()
        if 'proxies' in command:
            proxy = command['proxies'][0]
            logging.info("Proxy server: %s:1080" % proxy)
            commandLineSwitches['proxy-server'] = "socks5://" + proxy + ":1080"
        settings = {
            "log_severity":
            cefpython.LOGSEVERITY_DISABLE,  # LOGSEVERITY_VERBOSE
            "log_file":
            "",
            "release_dcheck_enabled":
            False,  # Enable only when debugging.
            # This directories must be set on Linux
            "locales_dir_path":
            cefpython.GetModuleDirectory() + "/locales",
            "resources_dir_path":
            cefpython.GetModuleDirectory(),
            "multi_threaded_message_loop":
            False,
            "unique_request_context_per_browser":
            True,
            "browser_subprocess_path":
            "%s/%s" % (cefpython.GetModuleDirectory(), "subprocess")
        }
        cefpython.Initialize(settings, commandLineSwitches)
        windowInfo = cefpython.WindowInfo()
        windowInfo.SetAsOffscreen(0)
        browserSettings = {"default_encoding": "utf-8"}
        browser = cefpython.CreateBrowserSync(windowInfo,
                                              browserSettings,
                                              navigateUrl=command['url'])
        cookieManager = cefpython.CookieManager.CreateManager("")
        if 'cookie' in command:
            for k, v in command['cookie'].items():
                cookie = cefpython.Cookie()
                cookie.SetName(k)
                cookie.SetValue(v)
                cookie.SetHasExpires(False)
                cookieManager.SetCookie(command['url'], cookie)
        browser.SetUserData("cookieManager", cookieManager)
        #browser = cefpython.CreateBrowserSync(windowInfo, browserSettings, navigateUrl="about:blank")
        # TODO handle post data
        #req = cefpython.Request.CreateRequest()
        #req.SetUrl(command['url'])
        # req.SetMethod("POST")
        # a = req.SetPostData([])
        #browser.GetMainFrame().LoadRequest(req)

        browser.SendFocusEvent(True)
        browser.SetUserData("width", width)
        browser.SetUserData("height", height)
        browser.SetUserData("metadata", metadata)
        browser.SetClientHandler(ClientHandler(browser, command))
        jsBindings = cefpython.JavascriptBindings(bindToFrames=True,
                                                  bindToPopups=True)
        jsBindings.SetProperty("delay", int(command['delay']))
        jsBindings.SetProperty("flash_delay", int(command['flash_delay']))
        jsBindings.SetFunction("setPageSize", setPageSize)
        jsBindings.SetFunction("jsCallback", jsCallback)
        jsBindings.SetFunction("log", logging.info)
        browser.SetJavascriptBindings(jsBindings)
        #browser.WasResized()
        cefpython.MessageLoop()
        width = browser.GetUserData("width")
        height = browser.GetUserData("height")
        metadata = browser.GetUserData("metadata")
        image = browser.GetUserData("image")
        html = browser.GetUserData("html")

    except:
        logging.error(sys.exc_info())
        traceback.print_exc()
        metadata['error'] = str(sys.exc_info())
    finally:
        if metadata['error'] == "0":
            metadata['status'] = "OK"
            metadata['finished'] = int(time())
        else:
            metadata['status'] = "error"
            metadata['time_finished'] = int(time())
        cefpython.Shutdown()
        return width, height, image, html, metadata