electron/spec/fixtures/extensions/chrome-scripting/background.js
Shelley Vohr 7032c0d03c
test: add test and api_feature definition for chrome.scripting.globalParams (#41685)
chore: add test and api_feature for chrome.scripting.globalParams
2024-03-26 12:33:47 +01:00

72 lines
1.7 KiB
JavaScript

/* 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 'globalParams' : {
await chrome.scripting.executeScript({
target: { tabId },
func: () => {
chrome.scripting.globalParams.changed = true;
},
world: 'ISOLATED'
});
const results = await chrome.scripting.executeScript({
target: { tabId },
func: () => JSON.stringify(chrome.scripting.globalParams),
world: 'ISOLATED'
});
const result = JSON.parse(results[0].result);
sendResponse(result);
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;
});