Skip to content

Commit

Permalink
add MOD: bing-wallpaper-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Cp0204 committed May 1, 2024
1 parent 893a8ca commit dd1ac7b
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/mod/bing-wallpaper-v2/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// name: bing-wallpaper-v2
// author: Cp0204
// date: 2024-05-02
// description: 自动设置必应壁纸,右键显示壁纸描述,支持多语言
// description: Automatically set Bing wallpaper, right-click display wallpaper description, support multi-language

(function () {
const observedAnchor = '#background';
async function moduleFunction() {
// 官方API无法跨域
// The official API cannot cross the domain
// https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN
const mkt = mapLanguageCode(localStorage.getItem('lang'));
const apiUrl = `https://bing.biturl.top/?resolution=1920&format=json&index=0&mkt=${mkt}`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
// 改壁纸
// Change wallpaper
const imageUrl = data.url;
console.log("Bing Wallpaper URL:", imageUrl);
const bgElement = document.querySelector(observedAnchor);
bgElement.style.backgroundImage = `url("${imageUrl}")`;
bgElement.style.filter = 'blur(5px) brightness(0.8)'; // 模糊和压暗效果

// 添加右键菜单
// Add right-click menu
const menuElement = document.querySelector(".dropdown-content");
const htmlString = `<a class="dropdown-item is-flex is-align-items-center" target="_blank" style="width:250px;white-space:normal;" href="${data.copyright_link}">${data.copyright}</a>`;
menuElement.insertAdjacentHTML("beforeend", htmlString)
})
.catch(error => {
console.error("Get Bing Wallpaper failed:", error);
});

function mapLanguageCode(code) {
const languagesMaps = {
"en_us": "en-US",
"zh_cn": "zh-CN",
"ja_jp": "ja-JP",
"de_de": "de-DE",
"fr_fr": "fr-FR"
};
return languagesMaps[code] || "en-US";
}
}


// ================================================
// 观察,等待 vue 渲染后执行
// Observe and wait for Vue rendering to complete.
// ================================================
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.target.querySelector(observedAnchor)) {
observer.disconnect();
debounced();
}
});
});
observer.observe(document.body, { childList: true, subtree: true, once: true });
function debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(this, args), wait);
};
}
const debounced = debounce(moduleFunction, 1);
})();

0 comments on commit dd1ac7b

Please sign in to comment.