-
Notifications
You must be signed in to change notification settings - Fork 36
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
Showing
13 changed files
with
248 additions
and
10 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,5 @@ | ||
--- | ||
'connector': patch | ||
--- | ||
|
||
add logger |
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
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,73 @@ | ||
import { ProxyMessage } from '~messages/proxy-message' | ||
|
||
import type { RequestArguments } from '~lib/request-arguments' | ||
import type { PlasmoCSConfig } from 'plasmo' | ||
|
||
export const config: PlasmoCSConfig = { | ||
matches: ['https://*/*'], | ||
world: 'MAIN', | ||
run_at: 'document_start', | ||
all_frames: false, | ||
} | ||
|
||
Object.defineProperties(window, { | ||
connector: { | ||
value: { | ||
enableLogging: (value = true) => { | ||
// shared web storage with host page and content scripts (Web Storage API) | ||
window.localStorage.setItem('status:logging', JSON.stringify(value)) | ||
|
||
// service worker extension storage (extension storage API) | ||
request({ | ||
method: 'toggleLogging', | ||
params: [value], | ||
}) | ||
|
||
return value | ||
}, | ||
}, | ||
configurable: false, | ||
writable: false, | ||
}, | ||
}) | ||
|
||
async function request(args: RequestArguments) { | ||
const { method, params } = args | ||
|
||
const messageChannel = new MessageChannel() | ||
|
||
return new Promise((resolve, reject) => { | ||
messageChannel.port1.onmessage = ({ data }) => { | ||
try { | ||
const message = ProxyMessage.parse(data) | ||
|
||
messageChannel.port1.close() | ||
|
||
switch (message.type) { | ||
case 'status:proxy:success': { | ||
resolve(message.data) | ||
|
||
return | ||
} | ||
case 'status:proxy:error': { | ||
reject(new Error(message.error.message)) | ||
|
||
return | ||
} | ||
} | ||
} catch { | ||
return | ||
} | ||
} | ||
|
||
const mainMessage = { | ||
type: 'status:main', | ||
data: { | ||
method, | ||
params, | ||
}, | ||
} | ||
|
||
window.postMessage(mainMessage, window.origin, [messageChannel.port2]) | ||
}) | ||
} |
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
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
Oops, something went wrong.