feat: support chrome.scripting
extension APIs (#39395)
feat: support chrome.scripting extension APIs
This commit is contained in:
parent
5078cae861
commit
f0ad357af2
16 changed files with 2087 additions and 15 deletions
51
spec/fixtures/extensions/chrome-scripting/background.js
vendored
Normal file
51
spec/fixtures/extensions/chrome-scripting/background.js
vendored
Normal file
|
@ -0,0 +1,51 @@
|
|||
/* global chrome */
|
||||
|
||||
const handleRequest = async (request, sender, sendResponse) => {
|
||||
const { method } = request;
|
||||
const tabId = sender.tab.id;
|
||||
|
||||
switch (method) {
|
||||
case 'executeScript': {
|
||||
chrome.scripting.executeScript({
|
||||
target: { tabId },
|
||||
function: () => {
|
||||
document.title = 'HEY HEY HEY';
|
||||
return document.title;
|
||||
}
|
||||
}).then(() => {
|
||||
console.log('success');
|
||||
}).catch((err) => {
|
||||
console.log('error', err);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'registerContentScripts': {
|
||||
await chrome.scripting.registerContentScripts([{
|
||||
id: 'session-script',
|
||||
js: ['content.js'],
|
||||
persistAcrossSessions: false,
|
||||
matches: ['<all_urls>'],
|
||||
runAt: 'document_start'
|
||||
}]);
|
||||
|
||||
chrome.scripting.getRegisteredContentScripts().then(sendResponse);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'insertCSS': {
|
||||
chrome.scripting.insertCSS({
|
||||
target: { tabId },
|
||||
css: 'body { background-color: red; }'
|
||||
}).then(() => {
|
||||
sendResponse({ success: true });
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
handleRequest(request, sender, sendResponse);
|
||||
return true;
|
||||
});
|
0
spec/fixtures/extensions/chrome-scripting/content.js
vendored
Normal file
0
spec/fixtures/extensions/chrome-scripting/content.js
vendored
Normal file
30
spec/fixtures/extensions/chrome-scripting/main.js
vendored
Normal file
30
spec/fixtures/extensions/chrome-scripting/main.js
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* global chrome */
|
||||
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
sendResponse(request);
|
||||
});
|
||||
|
||||
const map = {
|
||||
executeScript () {
|
||||
chrome.runtime.sendMessage({ method: 'executeScript' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
registerContentScripts () {
|
||||
chrome.runtime.sendMessage({ method: 'registerContentScripts' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
},
|
||||
insertCSS () {
|
||||
chrome.runtime.sendMessage({ method: 'insertCSS' }, response => {
|
||||
console.log(JSON.stringify(response));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const dispatchTest = (event) => {
|
||||
const { method, args = [] } = JSON.parse(event.data);
|
||||
map[method](...args);
|
||||
};
|
||||
|
||||
window.addEventListener('message', dispatchTest, false);
|
17
spec/fixtures/extensions/chrome-scripting/manifest.json
vendored
Normal file
17
spec/fixtures/extensions/chrome-scripting/manifest.json
vendored
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"name": "execute-script",
|
||||
"version": "1.0",
|
||||
"permissions": [
|
||||
"scripting"
|
||||
],
|
||||
"host_permissions": ["<all_urls>"],
|
||||
"content_scripts": [{
|
||||
"matches": [ "<all_urls>"],
|
||||
"js": ["main.js"],
|
||||
"run_at": "document_start"
|
||||
}],
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"manifest_version": 3
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue