首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gjs/gnome-shell-extension:从url读取远程jpg图片并设置为图标

gjs/gnome-shell-extension:从url读取远程jpg图片并设置为图标
EN

Stack Overflow用户
提问于 2016-04-07 09:53:53
回答 2查看 678关注 0票数 0

我试图通过允许检索远程图像(jpg)并将其设置为某个小部件的图标来改进gnome-shell-extension。

以下是我到目前为止得到的结果,但由于数据类型不匹配,它不起作用:

代码语言:javascript
复制
// allow remote album art url
const GdkPixbuf = imports.gi.GdkPixbuf;
const Soup = imports.gi.Soup;
const _httpSession = new Soup.SessionAsync();
Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());
function getAlbumArt(url, callback) {
    var request = Soup.Message.new('GET', url);
    _httpSession.queue_message(request, function(_httpSession, message) {
        if (message.status_code !== 200) {
          callback(message.status_code, null);
          return;
        } else {
          var albumart = request.response_body_data;
          // this line gives error message:
          // JS ERROR: Error: Expected type guint8 for Argument 'data' 
          // but got type 'object'
          // getAlbumArt/<@~/.local/share/gnome-shell/extensions
          // /laine@knasher.gmail.com/streamMenu.js:42
          var icon = GdkPixbuf.Pixbuf.new_from_inline(albumart, true);
          callback(null, icon);
        };
    });

回调如下:

代码语言:javascript
复制
....
            log('try retrieve albumart: ' + filePath);
            if(GLib.file_test(iconPath, GLib.FileTest.EXISTS)){
                let file = Gio.File.new_for_path(iconPath)
                let icon = new Gio.FileIcon({file:file});
                this._albumArt.gicon = icon;
            } else if (filePath.indexOf('http') == 0) {
                log('try retrieve from url: ' + filePath);
                getAlbumArt(filePath, function(code, icon){
                    if (code) {
                        this._albumArt.gicon = icon;
                    } else {
                        this._albumArt.hide();
                    }
                });
            }

....

我的问题是,如何解析响应,这是一个jpg图像,以便我可以用它设置小部件图标?非常感谢!

EN

回答 2

Stack Overflow用户

发布于 2020-03-30 01:41:59

我能够做到这一点只需简单地执行以下操作:

代码语言:javascript
复制
const St = imports.gi.St;
const Gio = imports.gi.Gio;
// ...

this.icon = new St.Icon()

// ...
let url = 'https://some.url'
let icon = Gio.icon_new_for_string(url);
this.icon.set_gicon(icon);

它会自动下载它。

我已经为这个问题挣扎了几个小时,直到我终于想出了一个使用本地图像缓存的方法(下载图像并将其存储在一个图标/文件夹中)。然后,我尝试了这种方法,只是为了好玩(只是想看看会发生什么,预计它会惨败),猜猜会发生什么?它就这么起作用了。在我能够找到的非常稀缺的文档中,没有提到这一点。

票数 1
EN

Stack Overflow用户

发布于 2020-10-11 08:24:30

对于仍然有同样问题的人,这里是我的解决方案:

代码语言:javascript
复制
_httpSession.queue_message(request, function(_httpSession, message) {
    
    let buffer = message.response_body.flatten();
    let bytes = buffer.get_data();
    let gicon = Gio.BytesIcon.new(bytes);

    // your code here

});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36465340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档