Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对更改进行Gnome外壳扩展查询_Javascript_Gnome 3_Gnome Shell Extensions - Fatal编程技术网

Javascript 如何对更改进行Gnome外壳扩展查询

Javascript 如何对更改进行Gnome外壳扩展查询,javascript,gnome-3,gnome-shell-extensions,Javascript,Gnome 3,Gnome Shell Extensions,我一直在与可怕的Gnome API文档作斗争,并提出了以下扩展: const St = imports.gi.St; const Main = imports.ui.main; const Tweener = imports.ui.tweener; const GLib = imports.gi.GLib; let label; function init() { label = new St.Bin({ style_class: 'panel-label' }); let

我一直在与可怕的Gnome API文档作斗争,并提出了以下扩展:

const St = imports.gi.St;
const Main = imports.ui.main;
const Tweener = imports.ui.tweener;
const GLib = imports.gi.GLib;

let label;

function init() {
    label = new St.Bin({ style_class: 'panel-label' });

    let stuff = GLib.spawn_command_line_sync("cat /home/user/temp/hello")[1].toString();
    let text = new St.Label({ text: stuff });

    label.set_child(text);
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(label, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(label);
}

这应该读取
hello
文件中的任何内容,并将其显示在顶部面板中。但是,如果我更改了
hello
文件的内容,我必须重新启动Gnome才能显示新内容。现在,当然有一种方法可以动态地实现这一点,但我在文档中找不到任何东西。面板中的消息基本上应该总是镜像文件中的任何内容。有什么办法吗?

您需要为您的
hello
文件获取一个
Gio.File
句柄,然后它:


这对我有用。它将每30秒刷新一次标签值

  • 添加以下导入

    const Mainloop=imports.Mainloop

  • 在您的init方法中

    Mainloop.timeout_add(30000, function () { 
     let stuff = GLib.spawn_command_line_sync("your_command")[1].toString();
     let label = new St.Label({ text: stuff });
     button.set_child(label);return true});
    

这太棒了!我希望这些东西能被清楚地记录在某个地方:(还有一个问题:您将如何监控命令的输出?假设我想运行ping并在顶部面板中显示每个数据包的时间?谢谢您的帮助!这本身就值得一个完整的问题和答案,但这段C代码可能会让您开始:说到C代码,不幸的是,您是正确的GJS文档的最佳替代品。)n表示读取C API:
Mainloop.timeout_add(30000, function () { 
 let stuff = GLib.spawn_command_line_sync("your_command")[1].toString();
 let label = new St.Label({ text: stuff });
 button.set_child(label);return true});