Skip to content

Commit

Permalink
MV3 migration content script snippets (GoogleChrome#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simeon Vincent authored Aug 11, 2021
1 parent 9f282ef commit 582677f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions reference/mv3/intro/mv3-migration/content-scripts/README.md
Original file line number Diff line number Diff line change
@@ -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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alert('File test alert');
15 changes: 15 additions & 0 deletions reference/mv3/intro/mv3-migration/content-scripts/manifest.json
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions reference/mv3/intro/mv3-migration/content-scripts/popup.css
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions reference/mv3/intro/mv3-migration/content-scripts/popup.html
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions reference/mv3/intro/mv3-migration/content-scripts/popup.js
Original file line number Diff line number Diff line change
@@ -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]
});
});

0 comments on commit 582677f

Please sign in to comment.