-
Notifications
You must be signed in to change notification settings - Fork 0
/
preInject.cjs
77 lines (67 loc) · 2.08 KB
/
preInject.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* 脚本注入的方式,
*/
const { writeFileSync, readdirSync, readFileSync } = require('fs');
const { join } = require('path');
const copydir = require('node-copydir');
contentStyleInsertToPage()
copyContentToPublic()
function copyContentToPublic() {
const dir = "./src/content/dist/assets"
const newDir = "./public/assets/content"
copydir(dir, newDir, () => { })
}
// 将content目录下的样式,写入content_scripts中,就能自动加载到页面中
function contentStyleInsertToPage() {
const cssPath2 = join(__dirname, "./src/content/dist/assets");
let cssFiles2 = readdirSync(`${cssPath2}`)
cssFiles2 = cssFiles2.map((file) => {
return `"assets/content/${file}"`
});
const cssList = cssFiles2.filter(item => {
return item.endsWith('.css"')
})
const jsList = cssFiles2.filter(item => item.endsWith('.js"'))
const injectionTSFile = `
export function injectScript () {
const cssList: string[] = [${cssList}];
const jsList: string[] = [${jsList}];
const css = cssList.map(item => {
return item
// return chrome.runtime.getURL(item);
})
const js = jsList.map(item => {
return item
// return chrome.runtime.getURL(item);
})
chrome.webNavigation.onDOMContentLoaded.addListener(async ({ tabId, url }) => {
console.log(tabId, url, '脚本注入过程')
// if (!url.startsWith('http')) {
// console.log('非http协议,不注入脚本')
// return false
// }
chrome.scripting.executeScript({
target: { tabId },
files: js,
})
.then(injectionResults => {
console.log(injectionResults, 'injectionResults')
})
.catch(e => {
console.log(e, 'js脚本注入过程错误')
});
chrome.scripting.insertCSS({
target: { tabId },
files: css,
})
.then(injectionResults => {
console.log(injectionResults, 'injectionResults')
})
.catch(e => {
console.log(e, 'css脚本注入过程错误')
});
});
}
`
writeFileSync('./src/background/injection.ts', injectionTSFile)
}