forked from NottPeak/skiovox-breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
149 lines (138 loc) · 6.56 KB
/
sw.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const target = { targetId: 'browser' }
function injection() {
if (!self.isRunning) {
self.isRunning = true;
webkitRequestFileSystem(TEMPORARY, 1024 * 1024 * 300, async function (fs) {
function writeFile(name, data) {
return new Promise((resolve) => {
fs.root.getFile(name, { create: true }, function (entry) {
entry.createWriter(function (writer) {
writer.write(new Blob([data]));
writer.onwriteend = function () {
resolve(entry)
}
});
})
})
}
function removeFile(name) {
return new Promise(function (resolve) {
fs.root.getFile(name, { create: true }, function (entry) {
entry.remove(resolve);
})
})
}
function filemain() {
function buildBlobWithScript(script) {
var fullHTML = `<script src="index.js"></script>`;
return new Promise((resolve, reject) => {
webkitRequestFileSystem(TEMPORARY, 1024 * 1024 * 300, function (fs) {
function writeFileInDirectory(dir, name, data) {
return new Promise((resolve) => {
dir.getFile(name, { create: true }, function (entry) {
entry.createWriter(function (writer) {
writer.write(new Blob([data]));
writer.onwriteend = function () {
resolve(entry)
}
});
})
})
}
function removeFileInDirectory(dir, name) {
return new Promise(function (resolve) {
dir.getFile(name, { create: true }, function (entry) {
entry.remove(resolve);
})
})
}
fs.root.getDirectory('evaluations', { create: true }, async function (entry) {
await removeFileInDirectory(entry, 'index.js');
await writeFileInDirectory(entry, 'index.js', script);
await removeFileInDirectory(entry, 'index.html');
var handle = await writeFileInDirectory(entry, 'index.html', fullHTML);
resolve(handle.toURL());
}, reject)
}, reject)
})
}
document.querySelector('button').onclick = async () => {
var url = await buildBlobWithScript(document.querySelector('textarea').value);
// unbelievable, why can't we just use open
await chrome.tabs.create({ url: url })
};
}
await removeFile('shim.html');
await removeFile('shim.js');
var entry = await writeFile("shim.html", "<textarea></textarea><br/><button>Evaluate</button><script src=\"shim.js\"></script>");
await writeFile("shim.js", `(${filemain.toString()})()`);
alert("Save this in your bookmarks: " + entry.toURL());
})
}
}
var onInjectionFinished;
var extPrefixContext;
var payload;
async function searchForBackgroundPage() {
var { targetInfos: infos } = await chrome.debugger.sendCommand(target, 'Target.getTargets');
var result = [];
infos.forEach(function (info) {
if (info.url.startsWith("chrome-extension://" + extPrefixContext) && info.type.includes('background')) {
console.log(info);
result.push(info);
}
})
return result;
}
function isManifestV3Extension({ url, type: requestType }) {
return new Promise((resolve, reject) => {
if (!url.startsWith("chrome-extension://") || requestType.indexOf("service")) return resolve(false); // New service workers can't be created on MV2 and services workers are created on mv3 by default.
resolve(true);
});
};
async function onNetEvent(_, _, event) {
// That is not how you make mv3 targetable just go with the mv2 flow you need to wait for and identity the mv3 service worker target id which shouldn't be done on netEvent.
if (!event.request.url.startsWith("chrome-extension://" + extPrefixContext)) {
await chrome.debugger.sendCommand(target, "Fetch.continueRequest", {
requestId: event.requestId
});
return;
};
await chrome.debugger.sendCommand(target, "Fetch.fulfillRequest", {
requestId: event.requestId,
responseCode: 200,
body: btoa(`(${payload.toString()})()`)
})
}
async function start() {
await chrome.debugger.attach(target, '1.3');
// let { targetInfos } = await chrome.debugger.sendCommand(target, 'Target.getTargets');
if (extPrefixContext && extPrefixContext !== '') {
// var {url: pageURL, browserContextId} = await searchForBackgroundPage();
// await chrome.debugger.sendCommand(target, 'Target.createTarget', {url: pageURL, browserContextId});
}
chrome.debugger.onEvent.addListener(onNetEvent)
await chrome.debugger.sendCommand(target, 'Fetch.enable');
onInjectionFinished({ status: `success visit chrome-extension://${extPrefixContext}/_generated_background_page.html to commence further with the code execution process.` });
}
async function stop() {
chrome.debugger.onEvent.addListener(async function (_, _, event) {
await chrome.debugger.sendCommand(target, "Fetch.continueRequest", {
requestId: event.requestId,
});
});
await chrome.debugger.detach(target);
}
chrome.runtime.onMessage.addListener(async function (msg, sender, respondWith) {
const { prefix } = msg;
if (msg.type === "start-inspect") {
extPrefixContext = msg.prefix;
payload = msg.payload ? 'function () {' + msg.payload + '}' : injection;
onInjectionFinished = respondWith;
await start();
}
if (msg.type === "cancel-inspect") {
await stop();
respondWith({ status: 'success' });
}
});