forked from webclipper/web-clipper
-
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
b8c0415
commit 0b06363
Showing
9 changed files
with
212 additions
and
23 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
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,3 +1,4 @@ | ||
import '@/main/background.main.common'; | ||
import '@/service/permissions/chrome/permissionsService'; | ||
import '@/service/webRequest/browser/background/tabService'; | ||
import '@/main/background.main'; |
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,3 +1,4 @@ | ||
import '@/main/background.main.common'; | ||
import '@/service/permissions/firefox/firefoxPermissionsService'; | ||
import '@/service/webRequest/firefox/background/tabService'; | ||
import '@/main/background.main'; |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Token } from 'typedi'; | ||
|
||
export interface WebBlockHeader { | ||
name: string; | ||
value: string; | ||
} | ||
|
||
export interface WebRequestBlockOption { | ||
requestHeaders: chrome.webRequest.HttpHeader[]; | ||
urls: string[]; | ||
} | ||
|
||
export interface IWebRequestService { | ||
startChangeHeader(option: WebRequestBlockOption): Promise<WebBlockHeader>; | ||
|
||
end(webBlockHeader: WebBlockHeader): Promise<void>; | ||
} | ||
|
||
export const IWebRequestService = new Token<IWebRequestService>(); |
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,62 @@ | ||
import { generateUuid } from '@web-clipper/shared/lib/uuid'; | ||
import { | ||
IWebRequestService, | ||
WebRequestBlockOption, | ||
WebBlockHeader, | ||
} from '@/service/common/webRequest'; | ||
|
||
import { Service } from 'typedi'; | ||
|
||
export const WEB_REQUEST_BLOCK_HEADER = 'web_clipper_web_request'; | ||
|
||
class BackgroundWebRequestService implements IWebRequestService { | ||
private handlerMap: Map<string, any>; | ||
|
||
constructor() { | ||
this.handlerMap = new Map<string, any>(); | ||
} | ||
|
||
async startChangeHeader(option: WebRequestBlockOption): Promise<WebBlockHeader> { | ||
const uuid = generateUuid(); | ||
const targetHeaders = option.requestHeaders.map(o => ({ | ||
name: o.name.toLocaleLowerCase(), | ||
value: o.value, | ||
})); | ||
|
||
const handler = (request: chrome.webRequest.WebRequestHeadersDetails) => { | ||
const originHeaders = request.requestHeaders ?? []; | ||
if (originHeaders.findIndex(o => o.name === WEB_REQUEST_BLOCK_HEADER) === -1) { | ||
return; | ||
} | ||
const headers = originHeaders | ||
.filter(header => { | ||
return !targetHeaders.find(o => o.name === header.name.toLocaleLowerCase()); | ||
}) | ||
.concat(targetHeaders); | ||
|
||
return { | ||
requestHeaders: headers, | ||
}; | ||
}; | ||
|
||
this.handlerMap.set(uuid, handler); | ||
chrome.webRequest.onBeforeSendHeaders.addListener(handler, { urls: option.urls }, [ | ||
'blocking', | ||
'requestHeaders', | ||
'extraHeaders', | ||
]); | ||
return { | ||
name: WEB_REQUEST_BLOCK_HEADER, | ||
value: uuid, | ||
}; | ||
} | ||
|
||
async end(webBlockHeader: WebBlockHeader): Promise<void> { | ||
const handler = this.handlerMap.get(webBlockHeader.value); | ||
this.handlerMap.delete(webBlockHeader.value); | ||
chrome.webRequest.onBeforeSendHeaders.removeListener(handler); | ||
chrome.webRequest.handlerBehaviorChanged(); | ||
} | ||
} | ||
|
||
Service(IWebRequestService)(BackgroundWebRequestService); |
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,38 @@ | ||
import { | ||
IWebRequestService, | ||
WebRequestBlockOption, | ||
WebBlockHeader, | ||
} from '@/service/common/webRequest'; | ||
import { IServerChannel, IChannel } from '@/service/common/ipc'; | ||
|
||
export class WebRequestChannel implements IServerChannel { | ||
constructor(private service: IWebRequestService) {} | ||
|
||
call = async ( | ||
_context: chrome.runtime.Port['sender'], | ||
command: string, | ||
arg: any | ||
): Promise<any> => { | ||
switch (command) { | ||
case 'end': | ||
return this.service.end(arg); | ||
case 'startChangeHeader': | ||
return this.service.startChangeHeader(arg); | ||
default: { | ||
throw new Error(`Call not found: ${command}`); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export class WebRequestChannelClient implements IWebRequestService { | ||
constructor(private channel: IChannel) {} | ||
|
||
startChangeHeader = async (option: WebRequestBlockOption): Promise<WebBlockHeader> => { | ||
return this.channel.call('startChangeHeader', option); | ||
}; | ||
|
||
end = async (webBlockHeader: WebBlockHeader): Promise<void> => { | ||
return this.channel.call('end', webBlockHeader); | ||
}; | ||
} |
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,61 @@ | ||
import { generateUuid } from '@web-clipper/shared/lib/uuid'; | ||
import { | ||
IWebRequestService, | ||
WebRequestBlockOption, | ||
WebBlockHeader, | ||
} from '@/service/common/webRequest'; | ||
|
||
import { Service } from 'typedi'; | ||
|
||
export const WEB_REQUEST_BLOCK_HEADER = 'web_clipper_web_request'; | ||
|
||
class BackgroundWebRequestService implements IWebRequestService { | ||
private handlerMap: Map<string, any>; | ||
|
||
constructor() { | ||
this.handlerMap = new Map<string, any>(); | ||
} | ||
|
||
async startChangeHeader(option: WebRequestBlockOption): Promise<WebBlockHeader> { | ||
const uuid = generateUuid(); | ||
const targetHeaders = option.requestHeaders.map(o => ({ | ||
name: o.name.toLocaleLowerCase(), | ||
value: o.value, | ||
})); | ||
|
||
const handler = (request: chrome.webRequest.WebRequestHeadersDetails) => { | ||
const originHeaders = request.requestHeaders ?? []; | ||
if (originHeaders.findIndex(o => o.name === WEB_REQUEST_BLOCK_HEADER) === -1) { | ||
return; | ||
} | ||
const headers = originHeaders | ||
.filter(header => { | ||
return !targetHeaders.find(o => o.name === header.name.toLocaleLowerCase()); | ||
}) | ||
.concat(targetHeaders); | ||
|
||
return { | ||
requestHeaders: headers, | ||
}; | ||
}; | ||
|
||
this.handlerMap.set(uuid, handler); | ||
chrome.webRequest.onBeforeSendHeaders.addListener(handler, { urls: option.urls }, [ | ||
'blocking', | ||
'requestHeaders', | ||
]); | ||
return { | ||
name: WEB_REQUEST_BLOCK_HEADER, | ||
value: uuid, | ||
}; | ||
} | ||
|
||
async end(webBlockHeader: WebBlockHeader): Promise<void> { | ||
const handler = this.handlerMap.get(webBlockHeader.value); | ||
this.handlerMap.delete(webBlockHeader.value); | ||
chrome.webRequest.onBeforeSendHeaders.removeListener(handler); | ||
chrome.webRequest.handlerBehaviorChanged(); | ||
} | ||
} | ||
|
||
Service(IWebRequestService)(BackgroundWebRequestService); |