Forum Archive

Pythonista Webbrowser

Sebastian

I started making a webbrowser with the ui module. It's not much now, but it works.
https://github.com/SebastianJarsve/Pythonista-Webbrowser

JonB

Lovely!
I'd suggest opening as a panel on ipad, and/or having some way to close it.
(Personally, I prefer panel, since it allows one to switch back and forth, with slight loss of real estate.).

Sebastian

@JonB Thanks! That sounds like a great idea :) I'll look into that, and maybe in the future add some settings to control how the web browser should be presented.

To close the web browser now, you can slide down with two fingers, but that can probably sometimes interfere with zooming and other two-finger gestures.

Sebastian

Does anyone have any suggestions on how to implement tabs?

techteej

I don't have much experience with the UI module, but couldn't you just make another view for each tab?

dgelessus

Exactly, you can use an array of ui.Views with identical position for the tabs and call bring_to_front() to switch to a tab.

I'm not sure how the ui module (or UIKit, for that matter) works internally, but it may be more efficient to move the inactive views out of the screen to avoid them being drawn unnecessarily under the active tab.

LawAbidingCactus

Sorry if this has already been answered, but what are you guys using to download .pyui files?

SpotlightKid

github_download.py (Works for arbitray URLs as well.)

ccc

Or GitHubGet.py, Shellista or file_downloader. See for the discussion http://omz-forums.appspot.com/pythonista/post/5340168324120576

These are all slicker tools but often I still use a five-liner called pyui_from_clipboard.py to copy from GitHub and create a local .pyui file.

Sebastian

Yeah, maybe I can just use multiple ui.WebViews and hide the ones that aren't showing.

But I'm still wondering about this though.

JonB

Were you asking about the menu that comes up with long press?
Seems like since your top level view is a custom view, you could override touch handling, no?
Though it might be hard to figure out what the user is clicking from the root view...maybe via javascript?

Sebastian

Yes, I meant the little popup menu that comes up with a long press.

I tried adding a touch_began function in the custom view, but the function would only run if I touched something else than the webview, buttons or the textfield (i.e. if I touched nothing). It seems like the other views has its own way of recognizing touch gestures and then overrides the custom view's touch events or something.

beer2011

@Sebastian
It is very good.(^^
Since the change of 'Safari' and 'Pythonista' becomes unnecessary, it is convenient.

I am also looking forward to future evolution.
Thanks!

JonB

Hmm, yes, it seems like events are handled from bottom up. I don't see any way to register an event listener.

It is possible to create a transparent view that sits on top of your webview, which captures touch events before they go to the webview. .. But there is no way in python that I can tell to rethrow such events so the webview also gets a copy.

One option would be to simulate touch events using javascript after deciding whether you want to handle it or not. I have not tried this, also not sure if javascript and ui use same coordinate conventions, so some conversions /offsets might be required. The following javascript would dispatch a click. It probably would take a lot of thought about how to dispatch events such that they don't break sites that use javascript. For instance you might dispatch mousedown, mouseup, mousemove as you get them. Not sure if click gets generated automatically, or you'd have to dispatch your own on mouseup.

function click(x,y){
    var ev = document.createEvent("MouseEvent");
    var el = document.elementFromPoint(x,y);
    ev.initMouseEvent(
        "click",
        true /* bubble */, true /* cancelable */,
        window, null,
        x, y, 0, 0, /* coordinates */
        false, false, false, false, /* modifier keys */
        0 /*left*/, null
    );
    el.dispatchEvent(ev);
}

Another option might be to not use an overlay, but inject some js event handling code after the page loads, and display the menu entirely in js. The javascript can interact with your python using a custom uri handling approach, to send back results. Probably similarly likely to clobber any existing js.

Omega0

If you want to use the transparent top view maybe you can call MyWebView.touch_began(touch) once you decide that it should be sent to the WebView?

JonB

Tried it. webview does not have touch_began, or the like, hence the javascript ideas.

Sebastian

It is explained how to use a custom contextual menu with a uiwebview here, but since we don't have a UiWindow we can't really catch the touch events before they are sent to the other views.

Implementing tabs seems kinda pointless if you can't use 'open-in new tab'.