欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > GNOME扩展:Bing壁纸

GNOME扩展:Bing壁纸

2025/12/17 4:29:25 来源:https://blog.csdn.net/sonichty/article/details/147701743  浏览:    关键词:GNOME扩展:Bing壁纸

难点

网络请求(Soup)

下载文件(Soup)

读写设置(Gio.Settings)

源码

import GLib from "gi://GLib";
import Gio from 'gi://Gio';
import St from 'gi://St';
import Soup from 'gi://Soup';import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';export default class BingWallpaperExtension extends Extension {enable() {this._indicator = new PanelMenu.Button(0.0, this.metadata.name, false);let gicon = Gio.icon_new_for_string(this.dir.get_path() + '/bing-symbolic.svg');const icon = new St.Icon({gicon: gicon,style_class: 'system-status-icon'});this._indicator.add_child(icon);//this._indicator.connect('pressed', () => this.getWallpaper());Main.panel.addToStatusArea(this.uuid, this._indicator);this.imagePath = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_PICTURES) + '/BingWallpaper';const menu_browse = new PopupMenu.PopupImageMenuItem('Browse', 'folder-directory-symbolic', {});menu_browse.connect('activate', () => {let uri = 'file://' + this.imagePath;Gio.app_info_launch_default_for_uri(uri, global.create_app_launch_context(0, -1));});this._indicator.menu.addMenuItem(menu_browse);const schema = 'org.gnome.desktop.background';        this.gsettings = new Gio.Settings({ schema: schema });let uri = this.gsettings.get_string('picture-uri');let filename = uri.substring(uri.lastIndexOf('/') + 1)this.menu_update = new PopupMenu.PopupImageMenuItem(filename, 'info-symbolic', {});this.menu_update.connect('activate', () => this.getWallpaper());this._indicator.menu.addMenuItem(this.menu_update);}disable() {this._indicator?.destroy();this._indicator = null;this.gsettings = null;}getWallpaper() {        var url = 'http://cn.bing.com/HPImageArchive.aspx';let params = { format: 'js', idx: '0' , n: '1' } ;let httpSession = new Soup.Session();let request = Soup.Message.new_from_encoded_form('GET', url, Soup.form_encode_hash(params));httpSession.send_and_read_async(request, GLib.PRIORITY_DEFAULT, null, (httpSession, message) => {            const decoder = new TextDecoder();var data = decoder.decode(httpSession.send_and_read_finish(message).get_data());var json = JSON.parse(data);let imgUrl = "http://www.bing.com" + json.images[0].url;var filename = json.images[0].enddate + '_' + json.images[0].urlbase.replace('/th?id=OHR.', '') + '.jpg';this.menu_update.label.text = json.images[0].copyright;var filepath = this.imagePath + '/' + filename;let file = Gio.file_new_for_path(filepath);let request = Soup.Message.new('GET', imgUrl);httpSession.send_and_read_async(request, GLib.PRIORITY_DEFAULT, null, (httpSession, message) => {data = httpSession.send_and_read_finish(message).get_data();file.replace_contents_bytes_async(data, null, false, Gio.FileCreateFlags.REPLACE_DESTINATION, null, (file, res) => {let uri = 'file://' + filepath;this.gsettings.set_string('picture-uri', uri);this.gsettings.set_string('picture-uri-dark', uri);Gio.Settings.sync();this.gsettings.apply();});});            });}}

安装

Bing Wallpaper - GNOME Shell Extensions

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词