Skip to content

Commit

Permalink
improve performace in safari
Browse files Browse the repository at this point in the history
  • Loading branch information
niu541412 committed Jun 20, 2024
1 parent cf3e7ef commit 048717c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
25 changes: 19 additions & 6 deletions js/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,16 @@ var OneClick = Class(ObjectClass, {
browserStorage.set({ 'shortcut': s });
},
setIogo: function (tab, path) {
// const cvs = new OffscreenCanvas(32, 32);
// const ctx = cvs.getContext('2d');
browserStorage.get('buttonicon', (item) => {
if (item.buttonicon) {
// 图标按钮的样式,可以避免和其他扩展图标相同而分不清。
// console.log("setting logo...");
const cvs = new OffscreenCanvas(32, 32);
const ctx = cvs.getContext('2d');

fetchImageBitmap(path, isFirefox, (imgBitmap) => {
fetchImageBitmap(path, notChrome, (imgBitmap) => {
if (!imgBitmap) {
console.error('Failed to load image');
return;
Expand Down Expand Up @@ -369,9 +371,9 @@ var OneClick = Class(ObjectClass, {
// })
// }
// )
const faviconPath = isFirefox ? "https://t3.gstatic.cn/faviconV2?client=SOCIAL&size=32&url="
const faviconPath = notChrome ? "https://t3.gstatic.cn/faviconV2?client=SOCIAL&size=32&url="
+ from_url.origin : faviconURL(from_url.origin, 32);
fetchImageBitmap(faviconPath, isFirefox, (oldImgBitmap) => {
fetchImageBitmap(faviconPath, notChrome, (oldImgBitmap) => {
if (!oldImgBitmap) {
console.error('Failed to load small image');
return;
Expand Down Expand Up @@ -408,6 +410,14 @@ var OneClick = Class(ObjectClass, {
}
});
} else {
// fetchImageBitmap(path, notChrome, (imgBitmap) => {
// ctx.drawImage(imgBitmap, 0, 0, 32, 32);
// const imageData = ctx.getImageData(0, 0, 32, 32);
// chrome.action.setIcon({
// imageData: { 32: imageData },
// tabId: tab.id
// });
// }) // safari
chrome.action.setIcon({
path: { 32: path },
tabId: tab.id
Expand Down Expand Up @@ -486,7 +496,7 @@ var OneClick = Class(ObjectClass, {
start: function () {
var the = this;
chrome.tabs.onUpdated.addListener(function (tabId, change) {
if (change.status === "loading") {
if (change.status === (isSafari ? "compelte" : "loading")) {
chrome.tabs.get(tabId, function (tab) {
the.updateAction(tab);
});
Expand Down Expand Up @@ -552,12 +562,15 @@ function faviconURL(u, s) {
return url.toString();
}

function fetchImageBitmap(url, isFirefox, callback) {
function fetchImageBitmap(url, isCors, callback) {
const pattern = /^(https?|chrome(-extension)?|file|filesystem):\/\//;
// check url is a local file or not
if (!pattern.test(url))
url = chrome.runtime.getURL(url);
fetch(url, { mode: isFirefox ? 'cors' : 'no-cors' })



fetch(url, { mode: isCors ? 'cors' : 'no-cors' })
.then(response => response.blob())
.then(blob => createImageBitmap(blob))
.then(bitmap => callback(bitmap))
Expand Down
1 change: 1 addition & 0 deletions js/easyswitch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const userAgent = navigator.userAgent.toLowerCase();
const isFirefox = userAgent.indexOf('firefox') > -1;
const isSafari = /^((?!chrome|android).)*safari/i.test(userAgent);
const notChrome = isFirefox || isSafari;
const browserStorage = isSafari ? chrome.storage.local : chrome.storage.sync;

if (!isSafari)
Expand Down
5 changes: 2 additions & 3 deletions js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ function addUserSiteClick() {
}
q = q[1];

// 判断是否为firefox
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
var favicon_url;

const ico_size = 32;
Expand All @@ -290,7 +288,7 @@ function addUserSiteClick() {
const favicon_url2 = `https://t3.gstatic.cn/faviconV2?client=SOCIAL&size=${ico_size}&url=${home}`;
const favicon_url3 = faviconURL(home, ico_size);

if (isFirefox) {
if (notChrome) {
const controller = new AbortController();
const signal = controller.signal;
const fetchTimeout = setTimeout(() => {
Expand Down Expand Up @@ -441,6 +439,7 @@ function initWays() {
const userAgent = navigator.userAgent.toLowerCase();
const isFirefox = userAgent.indexOf('firefox') > -1;
const isSafari = /^((?!chrome|android).)*safari/i.test(userAgent);
const notChrome = isFirefox || isSafari;
const browserStorage = isSafari ? chrome.storage.local : chrome.storage.sync;

document.addEventListener("DOMContentLoaded", init, false);
3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"content_security_policy": {
"extension_pages": "script-src 'self' ; object-src 'self'; img-src https://*.gstatic.com https://*.gstatic.cn;"
},
"author": "",
"description": "__MSG_description__",
"icons": {
"16": "asset/logo/icon_16x16.png",
"32": "asset/logo/icon_32x32.png",
"64": "asset/logo/icon_64x64.png",
"128": "asset/logo/icon_128x128.png",
"256": "asset/logo/icon_256x256.png"
},
Expand All @@ -23,6 +25,7 @@
"default_icon": {
"16": "asset/logo/icon_16x16.png",
"32": "asset/logo/icon_32x32.png",
"64": "asset/logo/icon_64x64.png",
"128": "asset/logo/icon_128x128.png",
"256": "asset/logo/icon_256x256.png"
},
Expand Down

0 comments on commit 048717c

Please sign in to comment.