Exemplo n.º 1
0
    def get(self, url, check_cache=True):
        if check_cache:
            img = self.get_no_download(url)
            if img is not None:
                return img

        # otherwise, find an existing image tag loading this url, or create one.
        html = '''
(function() {

var img = document.getElementById(%(hashed_url)s);
if (!img) {
    img = document.createElement('img');
    document.body.appendChild(img);
    window.allImages.push(img);
    img.setAttribute('id', %(hashed_url)s);
    img.setAttribute('onLoad', 'onLoad(this);');
    img.setAttribute('onError', 'onError(this);');
    img.src = %(url)s;
} else {
    if (img.load_state === 'loaded')
        onLoad(img);
    else if (img.load_state === 'error')
        onError(img);
}

img.lastAccessTime = new Date().getTime();
if (window.allImages.length > window.MAX_IMAGES)
    evictOld();

})();
    ''' % dict(url = jsenc(url), hashed_url=jsenc(urlhash(url)))

        self.GetWebView().RunScript(html)
Exemplo n.º 2
0
    def get(self, url, check_cache=True):
        if check_cache:
            img = self.get_no_download(url)
            if img is not None:
                return img

        # otherwise, find an existing image tag loading this url, or create one.
        html = '''
(function() {

var img = document.getElementById(%(hashed_url)s);
if (!img) {
    img = document.createElement('img');
    document.body.appendChild(img);
    window.allImages.push(img);
    img.setAttribute('id', %(hashed_url)s);
    img.setAttribute('onLoad', 'onLoad(this);');
    img.setAttribute('onError', 'onError(this);');
    img.src = %(url)s;
} else {
    if (img.load_state === 'loaded')
        onLoad(img);
    else if (img.load_state === 'error')
        onError(img);
}

img.lastAccessTime = new Date().getTime();
if (window.allImages.length > window.MAX_IMAGES)
    evictOld();

})();
    ''' % dict(url=jsenc(url), hashed_url=jsenc(urlhash(url)))

        self.GetWebView().RunScript(html)
Exemplo n.º 3
0
    def GetWebView(self):
        if self.webview is not None:
            return self.webview

        self.frame = wx.Frame(None)
        w = self.webview = WebKitWindow(self.frame)
        w.js_to_stderr = True

        self.bridge = bridge = JSPythonBridge(self.webview)
        bridge.on_call += self._webview_on_call

        import gui.skin
        jslib = lambda name: jsenc((gui.skin.resourcedir() / 'html' / name).url())

        html = '''<!doctype html>
<html>
    <head>
        <script type="text/javascript" src=%(utils)s></script>
        <script type="text/javascript" src=%(pythonbridgelib)s></script>
        <script type="text/javascript">
function onLoad(img) {
    img.load_state = 'loaded';
    D.notify('onLoad', {src: img.src});
}

function onError(img) {
    img.load_state = 'error';
    D.notify('onError', {src: img.src});
}

function cmp(a, b) {
    if (a < b) return -1;
    else if (b > a) return 1;
    else return 0;
}

function _byTime(a, b) {
    return cmp(a.lastAccessTime, b.lastAccessTime);
}

function evictOld() {
    // TODO: an insertion sort at access time would be more efficient, but this array is small so...eh
    allImages.sort(_byTime);
    var numToDelete = allImages.length - MAX_IMAGES;
    var deleted = allImages.slice(0, numToDelete);
    allImages = allImages.slice(numToDelete);

    for (var i = 0; i < deleted.length; ++i) {
        var img = deleted[i];
        img.parentNode.removeChild(img);
    }
}

window.allImages = [];
window.MAX_IMAGES = 30;

        </script>
    </head>
    <body>
    </body>
</html>''' % dict(pythonbridgelib=jslib('pythonbridge.js'),
                  utils=jslib('utils.js'))

        self.bridge.SetPageSource(html, 'file://imageloader')
        return self.webview
Exemplo n.º 4
0
    def GetWebView(self):
        if self.webview is not None:
            return self.webview

        self.frame = wx.Frame(None)
        w = self.webview = WebKitWindow(self.frame)
        w.js_to_stderr = True

        self.bridge = bridge = JSPythonBridge(self.webview)
        bridge.on_call += self._webview_on_call

        import gui.skin
        jslib = lambda name: jsenc(
            (gui.skin.resourcedir() / 'html' / name).url())

        html = '''<!doctype html>
<html>
    <head>
        <script type="text/javascript" src=%(utils)s></script>
        <script type="text/javascript" src=%(pythonbridgelib)s></script>
        <script type="text/javascript">
function onLoad(img) {
    img.load_state = 'loaded';
    D.notify('onLoad', {src: img.src});
}

function onError(img) {
    img.load_state = 'error';
    D.notify('onError', {src: img.src});
}

function cmp(a, b) {
    if (a < b) return -1;
    else if (b > a) return 1;
    else return 0;
}

function _byTime(a, b) {
    return cmp(a.lastAccessTime, b.lastAccessTime);
}

function evictOld() {
    // TODO: an insertion sort at access time would be more efficient, but this array is small so...eh
    allImages.sort(_byTime);
    var numToDelete = allImages.length - MAX_IMAGES;
    var deleted = allImages.slice(0, numToDelete);
    allImages = allImages.slice(numToDelete);

    for (var i = 0; i < deleted.length; ++i) {
        var img = deleted[i];
        img.parentNode.removeChild(img);
    }
}

window.allImages = [];
window.MAX_IMAGES = 30;

        </script>
    </head>
    <body>
    </body>
</html>''' % dict(pythonbridgelib=jslib('pythonbridge.js'),
                  utils=jslib('utils.js'))

        self.bridge.SetPageSource(html, 'file://imageloader')
        return self.webview