Skip to content

Commit

Permalink
fix: 浏览器扩展在chrome下的兼容性问题 + 默认Content-Type为json
Browse files Browse the repository at this point in the history
  • Loading branch information
SSmJaE committed Mar 31, 2023
1 parent 381f88d commit 8be145b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
8 changes: 1 addition & 7 deletions scripts/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@
]

manifest["content_security_policy"]["extension_pages"] = (
"default-src 'self'; " + # chrome强制要求,edge不需要
"script-src 'self'; " +
f"connect-src {' '.join(map(lambda host: 'http://'+host, PROJECT['connect']))};"
)


# manifest["icons"]["16"] = f"static/{PLATFORM}.png"
# manifest["icons"]["32"] = f"static/{PLATFORM}.png"
# manifest["icons"]["48"] = f"static/{PLATFORM}.png"
# manifest["icons"]["128"] = f"static/{PLATFORM}.png"

# manifest["action"]["default_icon"] = f"static/{PLATFORM}.png"

with open("scripts/manifest.json", "w", encoding="utf-8") as f2:
f2.write(json.dumps(manifest, indent=4, ensure_ascii=False))

Expand Down
19 changes: 14 additions & 5 deletions src/utils/polyfill/request/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import logger from "../../logger";
*
* 不然,则直接使用url,比如 https://www.baidu.com => https://www.baidu.com
*/
export function getFullUrl(url: string, query: any) {
for (const [, value] of Object.entries(query || {})) {
export function getFullUrl(url: string, query: any = {}) {
for (const [, value] of Object.entries(query)) {
if (typeof value === "object")
throw new Error("query params不应为嵌套对象,拍平或者手动序列化子对象");
}
Expand All @@ -34,7 +34,12 @@ export function getFullUrl(url: string, query: any) {
/**对crx sendMessage的封装,以实现一致的fetch风格的request通用接口 */
export async function requestForExtension<T = any>(
url: string,
{ body, query, ...realisticInit }: CustomRequestInit & { method: CustomRequestMethod } = {
{
method,
headers = {},
query,
body /* = {} */, // 允许undefined,JSON.stringify(undefined)也不会报错
}: CustomRequestInit & { method: CustomRequestMethod } = {
method: "GET",
headers: {},
body: undefined,
Expand All @@ -47,7 +52,11 @@ export async function requestForExtension<T = any>(
{
url: getFullUrl(url, query),
init: {
...realisticInit,
method,
headers: {
"Content-Type": "application/json;charset=UTF-8",
...headers,
},
body: JSON.stringify(body),
},
},
Expand Down Expand Up @@ -97,7 +106,7 @@ async function initializeXhr() {
/**对GM_xmlhttpRequest的封装,以实现一致的fetch风格的request通用接口 */
export function requestForUserscript<T = any>(
url: string,
{ method, headers, body, query }: CustomRequestInit & { method: CustomRequestMethod } = {
{ method, headers={}, query, body }: CustomRequestInit & { method: CustomRequestMethod } = {
method: "GET",
headers: {},
body: undefined,
Expand Down

1 comment on commit 8be145b

@mecty26
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

无法从以下源加载扩展

Please sign in to comment.