mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
MV3 migration content script snippets (#626)
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
Sample code for the [Executing arbitrary strings][section] section of the MV3 migration documentation.
|
||||
|
||||
[section]: https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings
|
||||
@@ -0,0 +1 @@
|
||||
alert('File test alert');
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "MV3 Migration - content script example",
|
||||
"version": "0.1",
|
||||
"manifest_version": 3,
|
||||
"background": {
|
||||
"service_worker": "background.js"
|
||||
},
|
||||
"permissions": [
|
||||
"scripting",
|
||||
"activeTab"
|
||||
],
|
||||
"action": {
|
||||
"default_popup": "popup.html"
|
||||
}
|
||||
}
|
||||
19
reference/mv3/intro/mv3-migration/content-scripts/popup.css
Normal file
19
reference/mv3/intro/mv3-migration/content-scripts/popup.css
Normal file
@@ -0,0 +1,19 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
html,
|
||||
body,
|
||||
main {
|
||||
height: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
body {
|
||||
min-width: 20em;
|
||||
min-height: 10em;
|
||||
}
|
||||
main {
|
||||
padding: 1em .5em;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
21
reference/mv3/intro/mv3-migration/content-scripts/popup.html
Normal file
21
reference/mv3/intro/mv3-migration/content-scripts/popup.html
Normal file
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
<link rel="stylesheet" href="popup.css">
|
||||
<script src="popup.js" defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<div>
|
||||
<button id="inject-file">Inject file</button>
|
||||
</div>
|
||||
<div>
|
||||
<button id="inject-function">Inject function</button>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
32
reference/mv3/intro/mv3-migration/content-scripts/popup.js
Normal file
32
reference/mv3/intro/mv3-migration/content-scripts/popup.js
Normal file
@@ -0,0 +1,32 @@
|
||||
let injectFile = document.getElementById('inject-file');
|
||||
let injectFunction = document.getElementById('inject-function');
|
||||
|
||||
async function getCurrentTab() {
|
||||
let queryOptions = { active: true, currentWindow: true };
|
||||
let [tab] = await chrome.tabs.query(queryOptions);
|
||||
return tab;
|
||||
}
|
||||
|
||||
injectFile.addEventListener('click', async () => {
|
||||
let tab = await getCurrentTab();
|
||||
|
||||
chrome.scripting.executeScript({
|
||||
target: {tabId: tab.id},
|
||||
files: ['content-script.js']
|
||||
});
|
||||
});
|
||||
|
||||
function showAlert(givenName) {
|
||||
alert(`Hello, ${givenName}`);
|
||||
}
|
||||
|
||||
injectFunction.addEventListener('click', async () => {
|
||||
let tab = await getCurrentTab();
|
||||
|
||||
let name = 'World';
|
||||
chrome.scripting.executeScript({
|
||||
target: {tabId: tab.id},
|
||||
func: showAlert,
|
||||
args: [name]
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user