I'm completely new to Cinnamon and javascript (but am fairly competent on python).
I've been trying to update an existing desklet to show some football scores from a json feed on my network.
The problem I have is that the script won't refresh the data. I've narrowed this down to being caused by my web request. I have tried to prove this by having the desklet just show the time (in seconds) so I can see whether it is updating or not. When I comment out the two web request lines, the time updates as expected. When they're left in, the time doesn't change. The trouble is, I don't know why this stops the update.
desklet.js
:
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const St = imports.gi.St;
const Json = imports.gi.Json;
const Soup = imports.gi.Soup;
const Desklet = imports.ui.desklet;
const Settings = imports.ui.settings;
const _httpSession = new Soup.SessionAsync();
function MyDesklet(metadata, desklet_id){
this._init(metadata, desklet_id);
}
MyDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,
_init: function(metadata, desklet_id){
Desklet.Desklet.prototype._init.call(this, metadata);
this._date = new St.Label({style_class: "football-desklet-label"});
this.setContent(this._date);
this.setHeader(_("Football scores"));
this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], desklet_id);
this.settings.bindProperty(Settings.BindingDirection.IN,
"date-format",
"format",
function() {},
null);
this.settings.bindProperty(Settings.BindingDirection.IN,
"font-size",
"size",
this._onSettingsChanged,
null);
this._onSettingsChanged();
this.getJSON("http://www.bbc.co.uk");
},
getJSON: function(url) {
// If I leave these two lines in, the time doesn't update.
let message = Soup.Message.new('GET', url);
_httpSession.send_message (message);
let displayDate = new Date();
this._date.set_text(displayDate.toLocaleFormat("%s"));
this.timeout = Mainloop.timeout_add_seconds(1, Lang.bind(this,this.getJSON));
},
_onSettingsChanged: function(){
this._date.style="font-size: " + this.size + "pt; font-style: italic";
},
on_desklet_removed: function() {
Mainloop.source_remove(this.timeout);
},
_updateDate: function(){
let displayDate = new Date();
this._date.set_text("Hello:" + displayDate.toLocaleFormat("%s"));
this.timeout = Mainloop.timeout_add_seconds(1, Lang.bind(this, this._updateDate));
}
}
function main(metadata, desklet_id){
let desklet = new MyDesklet(metadata, desklet_id);
return desklet;
}
If you want to test this yourself, the other two files are
settings-schema.json
:
{
"font-size": {
"type": "spinbutton",
"default": 50,
"min": 8,
"max": 50,
"units": "",
"description" : "Font size:",
"step": 1
}
}
and metadata.json
:
{
"uuid": "dev@elparaguayo",
"name": "Testing desklet",
"description": "Could do anything...",
"icon": "stock_calendar",
"prevent-decorations": false
}
Any help to debug this would be greatly appreciated. Thank you.