gpt4 book ai didi

javascript - Cinnamon javascript desklet 调用网页时不会刷新

转载 作者:行者123 更新时间:2023-11-28 01:23:11 24 4
gpt4 key购买 nike

我对 Cinnamon 和 javascript 完全陌生(但对 Python 相当有能力)。

我一直在尝试更新现有的桌面,以显示来 self 网络上的 json feed 的一些足球比分。

我遇到的问题是脚本不会刷新数据。我已将其范围缩小为由我的网络请求引起的。我试图通过让桌面只显示时间(以秒为单位)来证明这一点,这样我就可以看到它是否正在更新。当我注释掉两个 Web 请求行时,时间会按预期更新。当他们留在里面时,时间不会改变。问题是,我不知道为什么这会停止更新。

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

如果您想自己测试一下,其他两个文件是

settings-schema.json:

{
"font-size": {
"type": "spinbutton",
"default": 50,
"min": 8,
"max": 50,
"units": "",
"description" : "Font size:",
"step": 1
}
}

metadata.json:

{
"uuid": "dev@elparaguayo",
"name": "Testing desklet",
"description": "Could do anything...",
"icon": "stock_calendar",
"prevent-decorations": false
}

任何调试此问题的帮助将不胜感激。谢谢。

最佳答案

我不知道原因(所以,如果有人可以解释,请在此处的评论部分中解释),但答案是超时函数必须位于不同的函数中。

因此,代码如下所示:

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,
"font-size",
"size",
this._onSettingsChanged,
null);

this._onSettingsChanged();
this._updateScore();
},

getJSON: function(url) {

let message = Soup.Message.new('GET', url);
_httpSession.send_message (message);
if (message.status_code!== Soup.KnownStatusCode.OK) {
this._date.set_text("Unable to contact server.");
var sleep = 30
} else {

let jp = new Json.Parser();
jp.load_from_data(message.response_body.data.toString(), -1);
match=jp.get_root().get_object();
let found=match.get_boolean_member('matchfound');
if (found != true) {
this._date.set_text("Chelsea are not playing today.");
var sleep = 30
} else {
var sleep = match.get_int_member('sleep');
match = match.get_object_member('match');
let hometeam = match.get_string_member('hometeam');
let awayteam = match.get_string_member('awayteam');
let homescore = match.get_int_member('homescore').toString();
let awayscore = match.get_int_member('awayscore').toString();
this._date.set_text(hometeam + ' ' + homescore + '-' + awayscore + ' ' + awayteam);
}
}
return sleep;


},


_onSettingsChanged: function(){
this._date.style="font-size: " + this.size + "pt;";
},

on_desklet_removed: function() {
Mainloop.source_remove(this.timeout);
},

_updateScore: function(){
let sleep = this.getJSON("PATH-TO-PRIVATE-JSON-FEED");
let sleep = ~~(sleep/4)
this.timeout = Mainloop.timeout_add_seconds(sleep, Lang.bind(this, this._updateScore));
}
}

function main(metadata, desklet_id){
let desklet = new MyDesklet(metadata, desklet_id);
return desklet;
}

关于javascript - Cinnamon javascript desklet 调用网页时不会刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040388/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com