Skip to content

Commit a4ab65f

Browse files
committed
first pass at chrome
1 parent 08a6b2a commit a4ab65f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

contrib/chrome/background.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const STATE_DISABLED = 0
2+
const STATE_ENABLED = 1
3+
const STATE_ACTIVE = 2
4+
5+
const ICON_SIZE = 48
6+
7+
var enabled = false;
8+
var loaded = false;
9+
10+
var canvas = document.createElement('canvas')
11+
var icon = new Image()
12+
icon.src = chrome.extension.getURL('img/annotator-icon-sprite.png')
13+
icon.alt = 'Annotate'
14+
icon.onload = setIcon
15+
16+
function setIcon() {
17+
var state = (loaded && enabled) ? STATE_ENABLED : STATE_DISABLED;
18+
var ctx = canvas.getContext('2d');
19+
ctx.drawImage(icon, ICON_SIZE * state, 0, ICON_SIZE, ICON_SIZE, 0, 0, 19, 19)
20+
chrome.browserAction.setIcon({
21+
imageData: ctx.getImageData(0, 0, 19, 19)
22+
})
23+
}
24+
25+
chrome.browserAction.onClicked.addListener(function(tab) {
26+
var message = loaded ? (enabled ? 'hide' : 'show') : 'load'
27+
chrome.tabs.sendRequest(tab.id, {annotator: message}, function (response) {
28+
if (response.error) {
29+
throw response.error
30+
} else {
31+
if (!loaded) loaded = true
32+
enabled = !enabled;
33+
}
34+
setIcon()
35+
})
36+
})

contrib/chrome/content.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Listen for requests from the background scripts. Since the annotator code is
3+
* loaded in the page context, background events which interact with the
4+
* annotator user interface, such as showing or hiding the annotator need to be
5+
* handled here.
6+
*/
7+
chrome.extension.onRequest.addListener(
8+
function (request, sender, sendResponse) {
9+
var command = request.annotator
10+
if (command) {
11+
if (command === 'load') {
12+
$(document.body).annotator().annotator('setupPlugins')
13+
sendResponse({ok: true})
14+
} else {
15+
sendResponse({error: new TypeError("not implemented: " + command)})
16+
}
17+
}
18+
}
19+
)

manifest.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "Annotator",
3+
"version": "0.0.9",
4+
"manifest_version": 2,
5+
"description": "Inline annotation for the web. Select text, images, or (nearly) anything else, and add your notes.",
6+
"background": {
7+
"scripts": ["contrib/chrome/background.js"]
8+
},
9+
"browser_action": {
10+
"scripts": []
11+
},
12+
"content_scripts": [
13+
{
14+
"matches": ["<all_urls>"],
15+
"js": [
16+
"lib/vendor/jquery.js",
17+
"pkg/annotator-full.min.js",
18+
"contrib/chrome/content.js"
19+
],
20+
"css": [
21+
"pkg/annotator.min.css"
22+
]
23+
}
24+
],
25+
"permissions": [
26+
"tabs", "http://*/", "https://*/"
27+
]
28+
}

0 commit comments

Comments
 (0)