【发布时间】: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