【问题标题】:gjs/gnome-shell-extension: read remote jpg image from url and set as icongjs/gnome-shell-extension:从 url 读取远程 jpg 图像并设置为图标
【发布时间】:2016-04-07 01:53:53
【问题描述】:

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

这是我目前得到的,但由于数据类型不匹配,它不起作用:

// 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);
        };
    });

这里是回调:

....
            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 图像,以便我可以用它设置小部件图标? 非常感谢!

【问题讨论】:

  • 只是一个幸运的猜测,但它是 var albumart = request.response_body_data; 还是 var albumart = message.response_body.data; 一个点而不是另一个下划线?
  • 感谢您的建议,我实际尝试过:response_body.data 将图像作为 unicode 字符串返回,如错误消息所述。
  • 使用 roojs.org/seed/gir-1.2-gtk-3.0/seed/Soup.RequestFile.html 代替 Soup.Message.New 是否也有意义?

标签: glib gnome-shell-extensions gjs


【解决方案1】:

我可以通过简单地做到这一点:

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);

它会自动下载它。

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

【讨论】:

    【解决方案2】:

    对于仍然遇到相同问题的任何人,这是我的解决方案:

    _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
    
    });
    

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2022-01-19
      • 2019-03-30
      • 2017-01-28
      • 2012-07-17
      • 1970-01-01
      • 2011-11-12
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多