desmondscifo / pywebkitgtk

Automatically exported from code.google.com/p/pywebkitgtk
Other
1 stars 0 forks source link

Should be able to add new URL handlers for WebKit to load #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

This is something Per Persson brought up in an email. Below is the email he
sent:

--
Hi,

with WebKit and python on Mac OS X, using the PyObjC bindings, I can implement
an URL scheme handler (e.g. myscheme:///foo/bar) by  subclassing
NSURLProtocol, like so:

class MyURLProtocol(NSURLProtocol):
    def canInitWithRequest_(klass, request):
        if request.URL().scheme() == u'myscheme':
            return True
        return False
    def canonicalRequestForRequest_(klass, request):
        return request
    def startLoading(self):
        client = self.client()
        request = self.request()
        data = mydata(request.URL())
        response = NSURLResponse.alloc
().initWithURL_MIMEType_expectedContentLength_textEncodingName_(
                request.URL(),
                u'text/html',
                len(data),
                u'utf-8',
        )
        client.URLProtocol_didLoadData_(self, buffer(data))
        client.URLProtocolDidFinishLoading_(self)
    def stopLoading(self):
        pass
def setup():
    NSURLProtocol.registerClass_(MyURLProtocol)

For a full example, see e.g. http://pyobjc.sourceforge.net/examples/
pyobjc-framework-WebKit/PyDocURLProtocol/index.html

I guess it is possible to do something similar using PyWebKitGtk, but I have a
hard time finding how to go about it.
Any hints or pointers to examples/docs to get me started in the right
direction would be greatly appreciated.

Best regards,
Per

Original issue reported on code.google.com by jmalo...@gmail.com on 22 Jun 2008 at 8:50

GoogleCodeExporter commented 9 years ago
its a bit of a hack but you can do this by adding a handler for the
navigation-requested event on the WebView....

def _navigation_requested_cb(view, frame, networkRequest):
    # get uri from request object
    uri=networkRequest.get_uri()
    print "request to go to %s" % uri
    # load the page somehow.....
    page=urllib.urlopen(networkRequest.get_uri())
    # load into associated view, passing in uri
    view.load_string(page.read(), "text/html", "iso-8859-15", uri)
    # return 1 to stop any other handlers running
    # eg. the default uri handler...
    return 1

this would be pretty easy to pick out the desired protocols and return False 
for the
rest (leaving the default behaviour)

Original comment by mbull.pe...@gmail.com on 22 May 2009 at 11:23

GoogleCodeExporter commented 9 years ago
mbull, Thanks for that. I added your suggestion to the FAQ
(http://code.google.com/p/pywebkitgtk/wiki/HowDoI?ts=1243548256&updated=HowDoI).

Original comment by jmalo...@gmail.com on 28 May 2009 at 10:06

GoogleCodeExporter commented 9 years ago
I just quickly tried this in my app, and it works for the main page, but this 
event
is not triggered for resources referenced by the page (an external javascript 
file in
my case).

Original comment by uhm...@gmail.com on 29 Jul 2009 at 1:49

GoogleCodeExporter commented 9 years ago
Is there a way to intercept a request for resources referenced by a page? I 
want to
handle those resources from my program, similar to what uhmmmm wanted to do, 
but I
can not find the correct API from webkitgtk reference manual. I guess it will 
be hard
to add this functionality to webkit.

Original comment by smartke...@gmail.com on 12 Feb 2010 at 11:42

GoogleCodeExporter commented 9 years ago
There is - have a read at http://webkitgtk.org/reference/webkitgtk-
WebKitWebView.html#WebKitWebView-resource-request-starting. 

Original comment by jmalo...@gmail.com on 12 Feb 2010 at 9:39

GoogleCodeExporter commented 9 years ago
as I said in webkit-gtk ML <
https://lists.webkit.org/pipermail/webkit-gtk/2010-April/000193.html

this is very trivial in PyQt, we need to make it so in gtk version too!
http://diotavelli.net/PyQtWiki/Using%20a%20Custom%20Protocol%20with%20QtWebKit

Original comment by als...@gmail.com on 17 Apr 2010 at 3:12

GoogleCodeExporter commented 9 years ago
my pywebkitgtk version 1.0.1 and the docs says it's in "Since 1.1.14"

so can we say that why I asked (which includes being able to handle ajax 
requests) is
already solved.

if not, should I re-open this or port a new issue ?

Original comment by als...@gmail.com on 17 Apr 2010 at 3:21

GoogleCodeExporter commented 9 years ago
For anyone arriving at this ticket and still unsure how to go about this, see 
my comment on the HowDoI page regarding resource-request-starting.

Original comment by mackst...@gmail.com on 24 Jul 2010 at 5:32