forked from GoogleChrome/chrome-extensions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MV3 migration content script snippets (GoogleChrome#626)
- Loading branch information
Simeon Vincent
authored
Aug 11, 2021
1 parent
9f282ef
commit 582677f
Showing
7 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
1 change: 1 addition & 0 deletions
1
reference/mv3/intro/mv3-migration/content-scripts/content-script.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
alert('File test alert'); |
15 changes: 15 additions & 0 deletions
15
reference/mv3/intro/mv3-migration/content-scripts/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
reference/mv3/intro/mv3-migration/content-scripts/popup.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
reference/mv3/intro/mv3-migration/content-scripts/popup.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
reference/mv3/intro/mv3-migration/content-scripts/popup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}); | ||
}); |