Split favicon into two separate examples (#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
This commit is contained in:
amysteamdev 2023-01-19 08:08:44 -06:00 коммит произвёл GitHub
Родитель 49728cd4ae
Коммит c7bd96fc51
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 76 добавлений и 27 удалений

Просмотреть файл

@ -1,2 +0,0 @@
<!DOCTYPE html>
<script src="content.js"></script>

Просмотреть файл

@ -1,12 +0,0 @@
function faviconURL(u) {
const url = new URL(chrome.runtime.getURL("/_favicon/"));
url.searchParams.set("pageUrl", u); // this encodes the URL as well
url.searchParams.set("size", "64");
return url.toString();
}
window.onload = e => {
const img = document.createElement('img');
img.src = faviconURL("https://google.com");
document.body.appendChild(img);
}

Просмотреть файл

@ -1,17 +1,9 @@
{
"name": "Favicon",
"version": "1",
"name": "Favicon API in a popup",
"version": "1.1",
"manifest_version": 3,
"permissions": ["favicon"],
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_idle"
}],
"web_accessible_resources": [{
"resources": [ "_favicon/*" ],
"matches": ["<all_urls>"],
"extension_ids": [ "*" ]
}],
"action": { "default_popup": "content.html" }
"action": {
"default_popup": "popup.html"
}
}

6
api/favicon/popup.html Normal file
Просмотреть файл

@ -0,0 +1,6 @@
<!DOCTYPE html>
<html lang="en">
<body>
<script src="popup.js"></script>
</body>
</html>

11
api/favicon/popup.js Normal file
Просмотреть файл

@ -0,0 +1,11 @@
function faviconURL(u) {
const url = new URL(chrome.runtime.getURL("/_favicon/"));
url.searchParams.set("pageUrl", u); // this encodes the URL as well
url.searchParams.set("size", "32");
return url.toString();
}
const img = document.createElement('img');
// chrome-extension://EXTENSION_ID/_favicon/?pageUrl=https%3A%2F%2Fwww.google.com&size=32
img.src = faviconURL("https://www.google.com")
document.body.appendChild(img);

Просмотреть файл

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

Просмотреть файл

@ -0,0 +1,12 @@
function faviconURL(u) {
const url = new URL(chrome.runtime.getURL("/_favicon/"));
url.searchParams.set("pageUrl", u); // this encodes the URL as well
url.searchParams.set("size", "32");
return url.toString();
}
const imageOverlay = document.createElement('img');
imageOverlay.src = faviconURL("https://www.google.com");
imageOverlay.alt = "Google's favicon";
imageOverlay.classList.add('favicon-overlay');
document.body.appendChild(imageOverlay);

Просмотреть файл

@ -0,0 +1,19 @@
{
"name": "Favicon API in content scripts",
"version": "1.1",
"manifest_version": 3,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"css": ["style.css"]
}
],
"permissions": ["favicon"],
"web_accessible_resources": [
{
"resources": ["_favicon/*"],
"matches": ["<all_urls>"]
}
]
}

Просмотреть файл

@ -0,0 +1,7 @@
.favicon-overlay {
all: initial !important;
position: fixed !important;
top: 0 !important;
left: 0 !important;
z-index: 9999 !important;
}