forked from akai/readest
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
// localStorage mock
|
|
if (typeof window !== 'undefined' && !window.localStorage) {
|
|
const storage: Record<string, string> = {};
|
|
window.localStorage = {
|
|
getItem: (key: string) => storage[key] || null,
|
|
setItem: (key: string, value: string) => {
|
|
storage[key] = value;
|
|
},
|
|
removeItem: (key: string) => {
|
|
delete storage[key];
|
|
},
|
|
clear: () => {
|
|
Object.keys(storage).forEach((key) => delete storage[key]);
|
|
},
|
|
get length() {
|
|
return Object.keys(storage).length;
|
|
},
|
|
key: (index: number) => {
|
|
const keys = Object.keys(storage);
|
|
return keys[index] || null;
|
|
},
|
|
} as Storage;
|
|
} else if (typeof window !== 'undefined' && window.localStorage && !window.localStorage.getItem) {
|
|
// If localStorage exists but getItem is not a function, replace it
|
|
const storage: Record<string, string> = {};
|
|
window.localStorage = {
|
|
getItem: (key: string) => storage[key] || null,
|
|
setItem: (key: string, value: string) => {
|
|
storage[key] = value;
|
|
},
|
|
removeItem: (key: string) => {
|
|
delete storage[key];
|
|
},
|
|
clear: () => {
|
|
Object.keys(storage).forEach((key) => delete storage[key]);
|
|
},
|
|
get length() {
|
|
return Object.keys(storage).length;
|
|
},
|
|
key: (index: number) => {
|
|
const keys = Object.keys(storage);
|
|
return keys[index] || null;
|
|
},
|
|
} as Storage;
|
|
}
|
|
|
|
// matchMedia mock
|
|
if (typeof window !== 'undefined' && !window.matchMedia) {
|
|
window.matchMedia = (query: string) =>
|
|
({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addEventListener: () => {},
|
|
removeEventListener: () => {},
|
|
addListener: () => {},
|
|
removeListener: () => {},
|
|
dispatchEvent: () => false,
|
|
}) as MediaQueryList;
|
|
}
|