mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
Created WebExtension showing how to use the Cookies API (#114)
This commit is contained in:
committed by
wbamberg
parent
07375ca50a
commit
b7048d83df
9
list-cookies/README.md
Normal file
9
list-cookies/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# list-cookies
|
||||
|
||||
## What it does
|
||||
|
||||
This extensions list the cookies in the active tab.
|
||||
|
||||
# What it shows
|
||||
|
||||
Demonstration of the getAll() function in the cookie API
|
||||
11
list-cookies/cookies.css
Normal file
11
list-cookies/cookies.css
Normal file
@@ -0,0 +1,11 @@
|
||||
html, body {
|
||||
width: 500px;
|
||||
}
|
||||
|
||||
.panel {
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
li {
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
19
list-cookies/cookies.html
Normal file
19
list-cookies/cookies.html
Normal file
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<link rel="stylesheet" href="cookies.css"/>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="panel">
|
||||
<div class="panel-section panel-section-header">
|
||||
<div class="text-section-header" id="header-title"></div>
|
||||
</div>
|
||||
<ul id="cookie-list">
|
||||
</ul>
|
||||
<script src="cookies.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
35
list-cookies/cookies.js
Normal file
35
list-cookies/cookies.js
Normal file
@@ -0,0 +1,35 @@
|
||||
function showCookiesForTab(tabs) {
|
||||
//get the first tab object in the array
|
||||
tab = tabs.pop();
|
||||
|
||||
//get all cookies in the domain
|
||||
chrome.cookies.getAll({url: tab.url}, (cookies) => {
|
||||
|
||||
//set the header of the panel
|
||||
var activeTabUrl = document.getElementById('header-title');
|
||||
var text = document.createTextNode("Cookies at: "+tab.title);
|
||||
var cookieList = document.getElementById('cookie-list');
|
||||
activeTabUrl.appendChild(text);
|
||||
|
||||
if (cookies.length > 0) {
|
||||
//add an <li> item with the name and value of the cookie to the list
|
||||
for (cookie of cookies) {
|
||||
var li = document.createElement("li");
|
||||
var content = document.createTextNode(cookie.name + ": "+ cookie.value);
|
||||
li.appendChild(content);
|
||||
cookieList.appendChild(li);
|
||||
}
|
||||
} else {
|
||||
var p = document.createElement("p");
|
||||
var content = document.createTextNode("No cookies in this tab.");
|
||||
var parent = cookieList.parentNode;
|
||||
|
||||
p.appendChild(content);
|
||||
parent.appendChild(p);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
//get active tab to run an callback function.
|
||||
//it sends to our callback an array of tab objects
|
||||
chrome.tabs.query({currentWindow: true, active: true}, showCookiesForTab);
|
||||
BIN
list-cookies/icons/cookie.png
Normal file
BIN
list-cookies/icons/cookie.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 490 B |
BIN
list-cookies/icons/cookie@2x.png
Normal file
BIN
list-cookies/icons/cookie@2x.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.0 KiB |
BIN
list-cookies/icons/default19.png
Normal file
BIN
list-cookies/icons/default19.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 699 B |
BIN
list-cookies/icons/default38.png
Normal file
BIN
list-cookies/icons/default38.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.7 KiB |
27
list-cookies/manifest.json
Normal file
27
list-cookies/manifest.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"id": "list-cookies@mozilla.org",
|
||||
"strict_min_version": "47.0a1"
|
||||
}
|
||||
},
|
||||
"browser_action": {
|
||||
"browser_style": true,
|
||||
"default_title": "List cookies in the active tab",
|
||||
"default_popup": "cookies.html",
|
||||
"default_icon": {
|
||||
"19": "icons/default19.png",
|
||||
"38": "icons/default38.png"
|
||||
}
|
||||
},
|
||||
"description": "List cookies in the active tab.",
|
||||
"icons": {
|
||||
"48": "icons/cookie.png",
|
||||
"96": "icons/cookie@2x.png"
|
||||
},
|
||||
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/list-cookies",
|
||||
"manifest_version": 2,
|
||||
"name": "List cookies",
|
||||
"version": "1.0",
|
||||
"permissions": ["cookies","<all_urls>","tabs"]
|
||||
}
|
||||
Reference in New Issue
Block a user