|
1 | 1 | import LoggerCore from "@App/app/logger/core";
|
2 | 2 | import Logger from "@App/app/logger/logger";
|
3 | 3 | import { Channel } from "@App/app/message/channel";
|
4 |
| -import { Script } from "@App/app/repo/scripts"; |
| 4 | +import { SCRIPT_STATUS_ENABLE, Script } from "@App/app/repo/scripts"; |
5 | 5 | import { isFirefox } from "@App/pkg/utils/utils";
|
6 | 6 | import MessageCenter from "@App/app/message/center";
|
7 | 7 | import IoC from "@App/app/ioc";
|
8 | 8 | import { Request } from "./gm_api";
|
| 9 | +import Runtime from "./runtime"; |
9 | 10 |
|
10 | 11 | export const unsafeHeaders: { [key: string]: boolean } = {
|
11 | 12 | // 部分浏览器中并未允许
|
@@ -173,6 +174,37 @@ export function listenerWebRequest(headerFlag: string) {
|
173 | 174 | chrome.webRequest.onHeadersReceived.addListener(
|
174 | 175 | (details) => {
|
175 | 176 | if (!isExtensionRequest(details)) {
|
| 177 | + // 判断是否为页面请求 |
| 178 | + if (!(details.type === "main_frame" || details.type === "sub_frame")) { |
| 179 | + return {}; |
| 180 | + } |
| 181 | + // 判断页面上是否有脚本会运行,如果有判断是否有csp,有则移除csp策略 |
| 182 | + const runtime = IoC.instance(Runtime) as Runtime; |
| 183 | + // 这块代码与runtime里的pageLoad一样,考虑后面要不要优化 |
| 184 | + const result = runtime.matchUrl(details.url, (script) => { |
| 185 | + // 如果是iframe,判断是否允许在iframe里运行 |
| 186 | + if (details.type === "sub_frame") { |
| 187 | + if (script.metadata.noframes) { |
| 188 | + return true; |
| 189 | + } |
| 190 | + return script.status !== SCRIPT_STATUS_ENABLE; |
| 191 | + } |
| 192 | + return script.status !== SCRIPT_STATUS_ENABLE; |
| 193 | + }); |
| 194 | + if (result.length > 0 && details.responseHeaders) { |
| 195 | + // 移除csp |
| 196 | + for (let i = 0; i < details.responseHeaders.length; i += 1) { |
| 197 | + if ( |
| 198 | + details.responseHeaders[i].name.toLowerCase() === |
| 199 | + "content-security-policy" |
| 200 | + ) { |
| 201 | + details.responseHeaders[i].value = ""; |
| 202 | + } |
| 203 | + } |
| 204 | + return { |
| 205 | + responseHeaders: details.responseHeaders, |
| 206 | + }; |
| 207 | + } |
176 | 208 | return {};
|
177 | 209 | }
|
178 | 210 | const appendHeaders: chrome.webRequest.HttpHeader[] = [];
|
|
0 commit comments