-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea9b98b
commit 97dea3d
Showing
5 changed files
with
137 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// chrome API utils | ||
|
||
/** | ||
* | ||
* @param {String} url | ||
*/ | ||
export function reloadPage(url) { | ||
chrome.runtime.sendMessage({ | ||
type: "pageReload", | ||
url, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import { showTips } from './utils.js' | ||
|
||
const store = new Proxy( | ||
{ | ||
url: location.href, | ||
pageUrl: location.href, // 当前页面url | ||
}, | ||
{ | ||
set(target, key, newVal) { | ||
if (key === "url") { | ||
target[key] = newVal; | ||
document.querySelector("#current-url").textContent = newVal; | ||
if (newVal !== target.pageUrl) { | ||
document.querySelector(".warn-text").style.display = 'inline' | ||
target.pageUrl = newVal | ||
} | ||
} | ||
{ | ||
url: location.href, | ||
pageUrl: location.href, // 当前页面url | ||
}, | ||
{ | ||
set(target, key, newVal) { | ||
if (key === "url") { | ||
target[key] = newVal; | ||
document.querySelector("#current-url").textContent = newVal; | ||
if (newVal !== target.pageUrl) { | ||
showTips() | ||
target.pageUrl = newVal | ||
} | ||
} | ||
|
||
return Reflect.set(arguments) | ||
}, | ||
} | ||
return Reflect.set(...arguments) | ||
}, | ||
} | ||
); | ||
|
||
export default store; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,57 @@ | ||
import store from "./store.js"; | ||
|
||
const queryToObj = (str) => { | ||
const obj = Object.create(null); | ||
if (!str || str === "") return obj; | ||
|
||
const params = new URLSearchParams(str); | ||
for (const [key, value] of params) { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
const obj = Object.create(null); | ||
if (!str || str === "") return obj; | ||
|
||
const params = new URLSearchParams(str); | ||
for (const [key, value] of params) { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
}; | ||
|
||
const objToQuery = (obj) => { | ||
const tmp = []; | ||
for (const key of Object.keys(obj)) { | ||
tmp.push(`${key}=${obj[key]}`); | ||
} | ||
return tmp.join("&"); | ||
const tmp = []; | ||
for (const key of Object.keys(obj)) { | ||
tmp.push(`${key}=${obj[key]}`); | ||
} | ||
return tmp.join("&"); | ||
}; | ||
|
||
export const rmQuery = (key, url = store.url) => { | ||
if (key === "") return; | ||
const [host, query] = url.split("?"); | ||
const queryObj = queryToObj(query); | ||
Reflect.deleteProperty(queryObj, key); | ||
return `${host}?${objToQuery(queryObj)}`; | ||
if (key === "") return; | ||
const [host, query] = url.split("?"); | ||
const queryObj = queryToObj(query); | ||
Reflect.deleteProperty(queryObj, key); | ||
return `${host}?${objToQuery(queryObj)}`; | ||
}; | ||
|
||
export const addQuery = (obj, url = store.url) => { | ||
const [host, query] = url.split("?"); | ||
const queryObj = queryToObj(query); | ||
Object.assign(queryObj, obj); | ||
return `${host}?${objToQuery(queryObj)}`; | ||
const [host, query] = url.split("?"); | ||
const queryObj = queryToObj(query); | ||
Object.assign(queryObj, obj); | ||
return `${host}?${objToQuery(queryObj)}`; | ||
}; | ||
|
||
export const updateQuery = (oldKey, newObj, url = store.url) => { | ||
if (oldKey === "") return; | ||
store.url = addQuery(newObj, rmQuery(oldKey, url)); | ||
if (oldKey === "") return; | ||
store.url = addQuery(newObj, rmQuery(oldKey, url)); | ||
}; | ||
|
||
export const throttle = (fn, timeout = 300) => { | ||
let timer = undefined; | ||
return () => { | ||
if (timer) return; | ||
timer = setTimeout(() => { | ||
fn(); | ||
clearTimeout(timer); | ||
timer = undefined; | ||
}, timeout); | ||
}; | ||
let timer = undefined; | ||
return () => { | ||
if (timer) return; | ||
timer = setTimeout(() => { | ||
fn(); | ||
clearTimeout(timer); | ||
timer = undefined; | ||
}, timeout); | ||
}; | ||
}; | ||
|
||
|
||
export const showTips = (show = true) => { | ||
document.querySelector(".warn-text").style.display = show ? 'inline' : 'none' | ||
} |