Skip to content

Commit f492a7f

Browse files
committed
Add an example of chrome extension.
1 parent b2cb159 commit f492a7f

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

chrome-ext/ex1/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

chrome-ext/ex1/README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Example of target HOST
2+
3+
http://weather.livedoor.com/forecast/webservice/json/v1?city=130010

chrome-ext/ex1/background.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
chrome.browserAction.onClicked.addListener(function() {
2+
window.open("ui.html");
3+
});

chrome-ext/ex1/main.css

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
#url-box {
3+
width: 50rem;
4+
}
5+
6+
.main-button {
7+
width: 8rem;
8+
height: 3rem;
9+
color: white;
10+
background: #0070ff;
11+
border: none;
12+
outline: none;
13+
margin: 0.5rem 0rem;
14+
}
15+
.main-button:active {
16+
background: #00b0ff;
17+
}

chrome-ext/ex1/main.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
(() => {
2+
console.log('Start');
3+
4+
const getElement = (id) => {
5+
const elem = document.getElementById(id);
6+
if (elem == null) {
7+
console.log('Failed to find: ' + id);
8+
}
9+
return elem;
10+
}
11+
12+
const getTimeStamp = () => {
13+
const now = new Date();
14+
const ret =
15+
('0' + now.getHours()).slice(-2) + ':' +
16+
('0' + now.getMinutes()).slice(-2) + ':' +
17+
('0' + now.getSeconds()).slice(-2) + '.' +
18+
('0' + now.getMilliseconds()).slice(-3);
19+
return ret;
20+
}
21+
22+
const log = (msg) => {
23+
const area = getElement('info-area');
24+
const msgElem = document.createElement('div');
25+
msgElem.innerHTML = '<tt>' + getTimeStamp() + '</tt> ' + msg;
26+
area.insertBefore(msgElem, area.firstChild);
27+
}
28+
29+
const logError = (msg) => {
30+
log('[ERROR] ' + msg);
31+
}
32+
33+
const readURL = (url, successCb) => {
34+
log('Trying to connect: ' + url);
35+
const xhr = new XMLHttpRequest();
36+
xhr.onload = () => {
37+
successCb({
38+
res: JSON.parse(xhr.responseText),
39+
});
40+
}
41+
xhr.onerror = () => {
42+
logError('status: ' + xhr.status + ' (' + xhr.statusText + ')');
43+
}
44+
xhr.open('GET', url);
45+
xhr.send(null);
46+
}
47+
48+
document.addEventListener('DOMContentLoaded', () => {
49+
const runButton = getElement('run-button');
50+
runButton.onclick = () => {
51+
readURL(getElement('url-box').value, (param) => {
52+
log(param.res);
53+
});
54+
return false;
55+
};
56+
});
57+
})();

chrome-ext/ex1/manifest.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "Example 1",
4+
"description": "This is an example of Chrome extension",
5+
"version": "0.1",
6+
"browser_action": {
7+
},
8+
"background": {
9+
"scripts": ["background.js"]
10+
},
11+
"permissions": [
12+
"http://*/*",
13+
"https://*/*"
14+
]
15+
}

chrome-ext/ex1/ui.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<link rel="stylesheet" type="text/css" href="main.css">
4+
<script type="text/javascript" src="main.js"></script>
5+
</head>
6+
<body>
7+
<form>
8+
Host
9+
<input type="text" id="url-box">
10+
<br>
11+
<button type="submit" class="main-button" id="run-button">Start</button>
12+
</form>
13+
<div id="info-area">
14+
</div>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)