Skip to content

Commit

Permalink
Add chrome extension example
Browse files Browse the repository at this point in the history
  • Loading branch information
shama committed Apr 11, 2018
1 parent 33f3d03 commit 467b192
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions how-to-make-chrome-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# How To Make Chrome Extensions

> [https://youtu.be/Ipa58NVGs_c](https://youtu.be/Ipa58NVGs_c)
* Go to `chrome://extensions/`
* Enable Developer mode
* Load unpacked and select the `bear` folder from this project.

8 changes: 8 additions & 0 deletions how-to-make-chrome-extensions/bear/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
window.bears = {}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
window.bears[request.url] = request.count
})

chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.create({url: 'popup.html'})
})
13 changes: 13 additions & 0 deletions how-to-make-chrome-extensions/bear/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//alert('Grrr.')
// chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// const re = new RegExp('bear', 'gi')
// const matches = document.documentElement.innerHTML.match(re)
// sendResponse({count: matches.length})
// })

const re = new RegExp('bear', 'gi')
const matches = document.documentElement.innerHTML.match(re)
chrome.runtime.sendMessage({
url: window.location.href,
count: matches.length
})
18 changes: 18 additions & 0 deletions how-to-make-chrome-extensions/bear/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "bear",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"browser_action": {
"default_title": "Bear"
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["tabs"]
}
11 changes: 11 additions & 0 deletions how-to-make-chrome-extensions/bear/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- <button>Count Bears</button> -->
<script src="popup.js" charset="utf-8"></script>
</body>
</html>
23 changes: 23 additions & 0 deletions how-to-make-chrome-extensions/bear/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
document.addEventListener('DOMContentLoaded', function () {

const bg = chrome.extension.getBackgroundPage()
Object.keys(bg.bears).forEach(function (url) {
const div = document.createElement('div')
div.textContent = `${url}: ${bg.bears[url]}`
document.body.appendChild(div)
})

// document.querySelector('button').addEventListener('click', onclick, false)
//
// function onclick () {
// chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
// chrome.tabs.sendMessage(tabs[0].id, 'hi', setCount)
// })
// }
//
// function setCount (res) {
// const div = document.createElement('div')
// div.textContent = `${res.count} bears`
// document.body.appendChild(div)
// }
}, false)

0 comments on commit 467b192

Please sign in to comment.