Skip to content

Commit

Permalink
✨ Support unsplash
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Sep 13, 2023
1 parent e3782ff commit 7ff89d4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bookmark/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Maple Bookmarks",
"description": "Let you navigate smoothly while hiding the bookmark bar.",
"version": "1.9",
"version": "1.10",
"action": {
"default_popup": "popup.html"
},
Expand Down
7 changes: 6 additions & 1 deletion bookmark/manifest_firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Maple Bookmarks",
"description": "Let you navigate smoothly while hiding the bookmark bar.",
"version": "1.9",
"version": "1.10",
"action": {
"default_popup": "popup.html"
},
Expand All @@ -13,6 +13,11 @@
}
}
},
"browser_specific_settings": {
"gecko": {
"id": "[email protected]"
}
},
"optional_host_permissions": ["*://*/*"],
"permissions": ["bookmarks", "favicon"],
"icons": {
Expand Down
19 changes: 16 additions & 3 deletions bookmark/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,28 @@ function createBookmarkItem(bookmarkNode, parent) {
let linkTitle = createElement("p", "", bookmarkNode.title ? bookmarkNode.title : getTitleFromUrl(bookmarkNode.url));
bookItem.appendChild(linkTitle);

let hideTimeout = null;

let mouseleaveHandler = function () {
// 在mouseleave事件中,设置一个延时,然后隐藏Notification
hideTimeout = setTimeout(() => {
Notification.hide();
}, 500);
};

bookItem.addEventListener("mouseover", function () {
// 如果已经计划了隐藏通知的操作,取消它
if (hideTimeout) {
clearTimeout(hideTimeout);
hideTimeout = null;
}

if (checkOverflow(linkTitle)) {
Notification.show(bookmarkNode.title);
}
});

bookItem.addEventListener("mouseleave", function () {
Notification.hide();
});
bookItem.addEventListener("mouseleave", mouseleaveHandler);

parent.appendChild(bookItem);
}
Expand Down
2 changes: 1 addition & 1 deletion newtab/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "Maple NewTab",
"description": "Enhance your new tab experience with a comfortable and refreshing design.",
"version": "1.8",
"version": "1.9",
"chrome_url_overrides": {
"newtab": "newtab.html"
},
Expand Down
3 changes: 2 additions & 1 deletion newtab/newtab.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@
<body>
<div class="selector-container">
<select id="bg-selector">
<option value="bing">必应图</option>
<option value="unsplash">Unsplash</option>
<option value="blank">空白页</option>
<option value="random">随机图</option>
<option value="bing">必应图</option>
</select>
</div>
<a id="bg-description"></a>
Expand Down
45 changes: 38 additions & 7 deletions newtab/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ window.onload = function () {
blank: "空白页",
random: "随机图",
bing: "必应图",
unsplash: "Unsplash",
},
en: {
title: "New Tab",
blank: "Blank",
random: "Image",
bing: "Bing",
unsplash: "Unsplash",
},
};

Expand All @@ -33,6 +35,7 @@ window.onload = function () {
document.querySelector('#bg-selector option[value="blank"]').textContent = mapping.blank;
document.querySelector('#bg-selector option[value="random"]').textContent = mapping.random;
document.querySelector('#bg-selector option[value="bing"]').textContent = mapping.bing;
document.querySelector('#bg-selector option[value="unsplash"]').textContent = mapping.unsplash;

// 根据浏览器类型和颜色模式设置背景色
function setBackgroundColor() {
Expand All @@ -44,12 +47,16 @@ window.onload = function () {
}

function convertToLinkElement(data) {
bgDescription.textContent = `${data.title} · ${data.date}`;
bgDescription.href = "#";
bgDescription.onclick = function (e) {
e.preventDefault();
chrome.tabs.create({ url: data.url });
};
if (!title) {
bgDescription.textContent = "";
} else {
bgDescription.textContent = `${data.title} · ${data.date}`;
bgDescription.href = "#";
bgDescription.onclick = function (e) {
e.preventDefault();
chrome.tabs.create({ url: data.url });
};
}
}

// 随机设置背景图片
Expand Down Expand Up @@ -104,6 +111,24 @@ window.onload = function () {
});
}

function setUnsplashBackgroundImage() {
const apiBaseUrl = "https://ai.xrender.fun/photos/3200/1800";

fetch(apiBaseUrl)
.then((r) => {
const imageUrl = r.url.replace("https://fastly.picsum.photos", "https://ai.xrender.fun/picsum");
body.style.backgroundImage = `url(${imageUrl})`;
localStorage.setItem("bgUnsplashUrl", imageUrl);
localStorage.setItem("bgUnsplashDate", new Date().toISOString().slice(0, 10));
localStorage.setItem("bgUnsplashInfo", {
title: "",
});
})
.catch((error) => {
console.error("Error:", error);
});
}

/**
* @description 处理选择非空白背景时的逻辑
* @param type 选择的背景类型,random 或 bing
Expand All @@ -120,12 +145,16 @@ window.onload = function () {

if (imageUrl && imageDate === currentDate) {
body.style.backgroundImage = `url(${imageUrl})`;
convertToLinkElement(JSON.parse(imageInfo));
if (type !== "unsplash") {
convertToLinkElement(JSON.parse(imageInfo));
}
} else {
if (type === "random") {
setRandomBackgroundImage();
} else if (type === "bing") {
setBingBackgroundImage();
} else if (type === "unsplash") {
setUnsplashBackgroundImage();
}
}
}
Expand All @@ -144,6 +173,8 @@ window.onload = function () {
handleSetBackground(bgType, "bgImageUrl", "bgImageDate", "bgImageInfo");
} else if (bgType === "bing") {
handleSetBackground(bgType, "bgBingUrl", "bgBingDate", "bgBingInfo");
} else if (bgType === "unsplash") {
handleSetBackground(bgType, "bgUnsplashUrl", "bgUnsplashDate", "bgUnsplashInfo");
}
}

Expand Down

0 comments on commit 7ff89d4

Please sign in to comment.