Skip to content

Commit c7bd96f

Browse files
authored
Split favicon into two separate examples (GoogleChrome#806)
* Favicon split examples draft * Fix content script * Tweak popup * Add alt to img Tweak css * Add readme * Tweak img * Tweak img * Apply @oliverdunk's tech review
1 parent 49728cd commit c7bd96f

File tree

9 files changed

+76
-27
lines changed

9 files changed

+76
-27
lines changed

api/favicon/content.html

Lines changed: 0 additions & 2 deletions
This file was deleted.

api/favicon/content.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

api/favicon/manifest.json

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
{
2-
"name": "Favicon",
3-
"version": "1",
2+
"name": "Favicon API in a popup",
3+
"version": "1.1",
44
"manifest_version": 3,
55
"permissions": ["favicon"],
6-
"content_scripts": [{
7-
"matches": ["<all_urls>"],
8-
"js": ["content.js"],
9-
"run_at": "document_idle"
10-
}],
11-
"web_accessible_resources": [{
12-
"resources": [ "_favicon/*" ],
13-
"matches": ["<all_urls>"],
14-
"extension_ids": [ "*" ]
15-
}],
16-
"action": { "default_popup": "content.html" }
6+
"action": {
7+
"default_popup": "popup.html"
8+
}
179
}

api/favicon/popup.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<body>
4+
<script src="popup.js"></script>
5+
</body>
6+
</html>

api/favicon/popup.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function faviconURL(u) {
2+
const url = new URL(chrome.runtime.getURL("/_favicon/"));
3+
url.searchParams.set("pageUrl", u); // this encodes the URL as well
4+
url.searchParams.set("size", "32");
5+
return url.toString();
6+
}
7+
8+
const img = document.createElement('img');
9+
// chrome-extension://EXTENSION_ID/_favicon/?pageUrl=https%3A%2F%2Fwww.google.com&size=32
10+
img.src = faviconURL("https://www.google.com")
11+
document.body.appendChild(img);

examples/favicon-cs/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Fetching a favicon in a content script
2+
3+
This example fetches the favicon from www.google.com and inserts it at the top left of every page.
4+
5+
Note: This extension does not work on `chrome://extensions`.
6+
7+
See [Fetching favicons](https://developer.chrome.com/docs/extensions/mv3/favicon) to learn more.
8+
9+
## Testing the extension
10+
11+
1. Follow the instructions to load an [unpacked extension](https://developer.chrome.com/docs/extensions/mv3/getstarted/development-basics/#load-unpacked).
12+
2. Navigate to [www.example.com](https://www.example.com/).
13+
14+
It should look like this:
15+
16+
<img src="https://wd.imgix.net/image/BhuKGJaIeLNPW9ehns59NfwqKxF2/3Q1glvnzbWhraXRtnGOy.png" alt="Content script using the Favicon API" width="400"/>

examples/favicon-cs/content.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function faviconURL(u) {
2+
const url = new URL(chrome.runtime.getURL("/_favicon/"));
3+
url.searchParams.set("pageUrl", u); // this encodes the URL as well
4+
url.searchParams.set("size", "32");
5+
return url.toString();
6+
}
7+
8+
const imageOverlay = document.createElement('img');
9+
imageOverlay.src = faviconURL("https://www.google.com");
10+
imageOverlay.alt = "Google's favicon";
11+
imageOverlay.classList.add('favicon-overlay');
12+
document.body.appendChild(imageOverlay);

examples/favicon-cs/manifest.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "Favicon API in content scripts",
3+
"version": "1.1",
4+
"manifest_version": 3,
5+
"content_scripts": [
6+
{
7+
"matches": ["<all_urls>"],
8+
"js": ["content.js"],
9+
"css": ["style.css"]
10+
}
11+
],
12+
"permissions": ["favicon"],
13+
"web_accessible_resources": [
14+
{
15+
"resources": ["_favicon/*"],
16+
"matches": ["<all_urls>"]
17+
}
18+
]
19+
}

examples/favicon-cs/style.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.favicon-overlay {
2+
all: initial !important;
3+
position: fixed !important;
4+
top: 0 !important;
5+
left: 0 !important;
6+
z-index: 9999 !important;
7+
}

0 commit comments

Comments
 (0)