Skip to content

Commit 467b192

Browse files
committed
Add chrome extension example
1 parent 33f3d03 commit 467b192

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# How To Make Chrome Extensions
2+
3+
> [https://youtu.be/Ipa58NVGs_c](https://youtu.be/Ipa58NVGs_c)
4+
5+
* Go to `chrome://extensions/`
6+
* Enable Developer mode
7+
* Load unpacked and select the `bear` folder from this project.
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
window.bears = {}
2+
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
3+
window.bears[request.url] = request.count
4+
})
5+
6+
chrome.browserAction.onClicked.addListener(function (tab) {
7+
chrome.tabs.create({url: 'popup.html'})
8+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//alert('Grrr.')
2+
// chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
3+
// const re = new RegExp('bear', 'gi')
4+
// const matches = document.documentElement.innerHTML.match(re)
5+
// sendResponse({count: matches.length})
6+
// })
7+
8+
const re = new RegExp('bear', 'gi')
9+
const matches = document.documentElement.innerHTML.match(re)
10+
chrome.runtime.sendMessage({
11+
url: window.location.href,
12+
count: matches.length
13+
})
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "bear",
3+
"version": "1.0",
4+
"manifest_version": 2,
5+
"content_scripts": [
6+
{
7+
"matches": ["<all_urls>"],
8+
"js": ["content.js"]
9+
}
10+
],
11+
"browser_action": {
12+
"default_title": "Bear"
13+
},
14+
"background": {
15+
"scripts": ["background.js"]
16+
},
17+
"permissions": ["tabs"]
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<!-- <button>Count Bears</button> -->
9+
<script src="popup.js" charset="utf-8"></script>
10+
</body>
11+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
document.addEventListener('DOMContentLoaded', function () {
2+
3+
const bg = chrome.extension.getBackgroundPage()
4+
Object.keys(bg.bears).forEach(function (url) {
5+
const div = document.createElement('div')
6+
div.textContent = `${url}: ${bg.bears[url]}`
7+
document.body.appendChild(div)
8+
})
9+
10+
// document.querySelector('button').addEventListener('click', onclick, false)
11+
//
12+
// function onclick () {
13+
// chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
14+
// chrome.tabs.sendMessage(tabs[0].id, 'hi', setCount)
15+
// })
16+
// }
17+
//
18+
// function setCount (res) {
19+
// const div = document.createElement('div')
20+
// div.textContent = `${res.count} bears`
21+
// document.body.appendChild(div)
22+
// }
23+
}, false)

0 commit comments

Comments
 (0)