feat: 新增包文件
This commit is contained in:
72
packages/electron-chrome-extensions/spec/fixtures/rpc/background.js
vendored
Normal file
72
packages/electron-chrome-extensions/spec/fixtures/rpc/background.js
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
/* global chrome */
|
||||
|
||||
const sendIpc = ({ tabId, name }) => {
|
||||
chrome.tabs.sendMessage(tabId, { type: 'send-ipc', args: [name] })
|
||||
}
|
||||
|
||||
const transformArgs = (args, sender) => {
|
||||
const tabId = sender.tab.id
|
||||
|
||||
const transformArg = (arg) => {
|
||||
if (arg && typeof arg === 'object') {
|
||||
// Convert object to function that sends IPC
|
||||
if ('__IPC_FN__' in arg) {
|
||||
return () => {
|
||||
sendIpc({ tabId, name: arg.__IPC_FN__ })
|
||||
}
|
||||
} else {
|
||||
// Deep transform objects
|
||||
for (const key of Object.keys(arg)) {
|
||||
if (arg.hasOwnProperty(key)) {
|
||||
arg[key] = transformArg(arg[key])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arg
|
||||
}
|
||||
|
||||
return args.map(transformArg)
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener((message, sender, reply) => {
|
||||
switch (message.type) {
|
||||
case 'api': {
|
||||
const { method, args } = message
|
||||
|
||||
const [apiName, subMethod] = method.split('.')
|
||||
|
||||
if (typeof chrome[apiName][subMethod] === 'function') {
|
||||
const transformedArgs = transformArgs(args, sender)
|
||||
chrome[apiName][subMethod](...transformedArgs, reply)
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
case 'event-once': {
|
||||
const { name } = message
|
||||
|
||||
const [apiName, eventName] = name.split('.')
|
||||
|
||||
if (typeof chrome[apiName][eventName] === 'object') {
|
||||
const event = chrome[apiName][eventName]
|
||||
event.addListener(function callback(...args) {
|
||||
if (chrome.runtime.lastError) {
|
||||
reply(chrome.runtime.lastError)
|
||||
} else {
|
||||
reply(args)
|
||||
}
|
||||
|
||||
event.removeListener(callback)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Respond asynchronously
|
||||
return true
|
||||
})
|
||||
|
||||
console.log('background-script-evaluated')
|
||||
60
packages/electron-chrome-extensions/spec/fixtures/rpc/content-script.js
vendored
Normal file
60
packages/electron-chrome-extensions/spec/fixtures/rpc/content-script.js
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function evalInMainWorld(fn) {
|
||||
const script = document.createElement('script')
|
||||
script.textContent = `((${fn})())`
|
||||
document.documentElement.appendChild(script)
|
||||
}
|
||||
|
||||
function sendIpc(name, ...args) {
|
||||
const jsonArgs = [name, ...args].map((arg) => JSON.stringify(arg))
|
||||
const funcStr = `() => { electronTest.sendIpc(${jsonArgs.join(', ')}) }`
|
||||
evalInMainWorld(funcStr)
|
||||
}
|
||||
|
||||
async function exec(action) {
|
||||
const send = async () => {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.runtime.sendMessage(action, (result) => {
|
||||
if (chrome.runtime.lastError) {
|
||||
reject(chrome.runtime.lastError.message)
|
||||
} else {
|
||||
resolve(result)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Retry logic - the connection doesn't seem to always be available when
|
||||
// attempting to send. This started when upgrading to Electron 22 from 15.
|
||||
let result
|
||||
for (let i = 0; i < 3; i++) {
|
||||
try {
|
||||
result = await send()
|
||||
break
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
await new Promise((resolve) => setTimeout(resolve, 100)) // sleep
|
||||
}
|
||||
}
|
||||
|
||||
sendIpc('success', result)
|
||||
}
|
||||
|
||||
window.addEventListener('message', (event) => {
|
||||
exec(event.data)
|
||||
})
|
||||
|
||||
evalInMainWorld(() => {
|
||||
window.exec = (json) => window.postMessage(JSON.parse(json))
|
||||
})
|
||||
|
||||
chrome.runtime.onMessage.addListener((message) => {
|
||||
switch (message.type) {
|
||||
case 'send-ipc': {
|
||||
const [name] = message.args
|
||||
sendIpc(name)
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
BIN
packages/electron-chrome-extensions/spec/fixtures/rpc/icon_16.png
vendored
Normal file
BIN
packages/electron-chrome-extensions/spec/fixtures/rpc/icon_16.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
26
packages/electron-chrome-extensions/spec/fixtures/rpc/manifest.json
vendored
Normal file
26
packages/electron-chrome-extensions/spec/fixtures/rpc/manifest.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "chrome-rpc",
|
||||
"version": "1.0",
|
||||
"browser_action": {
|
||||
"default_title": "RPC"
|
||||
},
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["content-script.js"],
|
||||
"run_at": "document_end"
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"scripts": ["background.js"],
|
||||
"persistent": true
|
||||
},
|
||||
"manifest_version": 2,
|
||||
"permissions": [
|
||||
"contextMenus",
|
||||
"nativeMessaging",
|
||||
"webRequest",
|
||||
"webRequestBlocking",
|
||||
"<all_urls>"
|
||||
]
|
||||
}
|
||||
2
packages/electron-chrome-extensions/spec/fixtures/rpc/popup.html
vendored
Normal file
2
packages/electron-chrome-extensions/spec/fixtures/rpc/popup.html
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
<!DOCTYPE html>
|
||||
<h1>browserAction</h1>
|
||||
Reference in New Issue
Block a user