feat: 新增包文件
This commit is contained in:
40
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/background.js
vendored
Normal file
40
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/background.js
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
/* global chrome */
|
||||
|
||||
const eventNames = [
|
||||
'onBeforeNavigate',
|
||||
'onCommitted',
|
||||
'onCompleted',
|
||||
'onCreatedNavigationTarget',
|
||||
'onDOMContentLoaded',
|
||||
'onErrorOccurred',
|
||||
'onHistoryStateUpdated',
|
||||
'onReferenceFragmentUpdated',
|
||||
'onTabReplaced',
|
||||
]
|
||||
|
||||
let activeTabId
|
||||
|
||||
let eventLog = []
|
||||
const logEvent = (eventName) => {
|
||||
if (eventName) eventLog.push(eventName)
|
||||
if (typeof activeTabId === 'undefined') return
|
||||
|
||||
eventLog.forEach((eventName) => {
|
||||
chrome.tabs.sendMessage(activeTabId, { name: 'logEvent', args: eventName })
|
||||
})
|
||||
|
||||
eventLog = []
|
||||
}
|
||||
|
||||
eventNames.forEach((eventName) => {
|
||||
chrome.webNavigation[eventName].addListener(() => {
|
||||
logEvent(eventName)
|
||||
})
|
||||
})
|
||||
|
||||
chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, ([tab]) => {
|
||||
activeTabId = tab.id
|
||||
logEvent()
|
||||
})
|
||||
|
||||
console.log('background-script-evaluated')
|
||||
12
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/content-script.js
vendored
Normal file
12
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/content-script.js
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function evalInMainWorld(fn) {
|
||||
const script = document.createElement('script')
|
||||
script.textContent = `((${fn})())`
|
||||
document.documentElement.appendChild(script)
|
||||
}
|
||||
|
||||
chrome.runtime.onMessage.addListener(({ name, args }) => {
|
||||
const funcStr = `() => { electronTest.sendIpc(${JSON.stringify(name)}, ${JSON.stringify(args)}) }`
|
||||
evalInMainWorld(funcStr)
|
||||
})
|
||||
16
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/manifest.json
vendored
Normal file
16
packages/electron-chrome-extensions/spec/fixtures/chrome-webNavigation/manifest.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "chrome-webNavigation",
|
||||
"version": "1.0",
|
||||
"manifest_version": 2,
|
||||
"content_scripts": [
|
||||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["content-script.js"],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"scripts": ["background.js"],
|
||||
"persistent": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user