Created WebExtension showing how to use the Cookies API (#114)

This commit is contained in:
Flávio da Silva Rodrigues Almeida
2016-10-31 15:34:16 -02:00
committed by wbamberg
parent 07375ca50a
commit b7048d83df
9 changed files with 101 additions and 0 deletions

9
list-cookies/README.md Normal file
View 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
View File

@@ -0,0 +1,11 @@
html, body {
width: 500px;
}
.panel {
padding: 5px;
}
li {
margin-bottom: 5px;
}

19
list-cookies/cookies.html Normal file
View 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
View 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);

Binary file not shown.

After

Width:  |  Height:  |  Size: 490 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 699 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View 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"]
}