Make a chrome extension that looks up a selected word’s definition using the Wordnik API.
javascript:(function(){})()
위 코드와 같이 IIFE를 사용하면 bookmarklet을 만들 수 있음.
(function() {
var elts = document.getElementsByTagName('p');
for (var i = 0; i < elts.length; i++) {
elts[i].style['background-color'] = '#C036F3';
}
})();
위 코드를
bookapplet.html의<a href="">안에 넣어보자.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BookMarklet</title>
</head>
<body>
<h1>BookMarklet</h1>
<p>
This is a paragraph of <a href="javascript:(function() {
var elts = document.getElementsByTagName('p');
for (var i = 0; i < elts.length; i++) {
elts[i].style['background-color'] = '#C036F3';
}
})()">Bookmarklet!</a>
</p>
</body>
</html>
페이지의 모든 paragraph 태그의 background-color가 퍼플로 바뀐다.
또한, 이 링크를 즐겨찾기로 등록하면, Bookmarklet이 즐겨찾기에 등록이 된다.
(function () {
var script = document.createElement('script');
script.src = 'http://shiffman.net/a2z/js/bookmarklet.js';
document.body.appendChild(script);
})();
위와 같은 방식으로 자바스크립트를 추가할 수도 있으며,
(function () {
var script = document.createElement('script');
var url = 'http://shiffman.net/a2z/js/bookmarklet.js';
script.src = url + "?" + new Date().getTime();
document.body.appendChild(script);
})();
이런 방식으로 자동 업그레이드 적용도 가능할 것이다.
manifest.json파일이 필요함.permission, references of all other files 등{
"manifest_version": 2,
"name": "My Extension",
"version": "0.1"
}
manifest.json의 content_script에 자바스크립트 파일을 추가하고, 실제 파일 content.js를 추가함.
{
"manifest_version": 2,
"name": "My Extension",
"version": "0.1",
"content_scripts": [
{
"js": ["content.js"]
}
]
}
<all_urls> 또는 * wildcard를 사용할 수 있음.{
"manifest_version": 2,
"name": "My Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
]
}
{
"manifest_version": 2,
"name": "My Extension",
"version": "0.1",
"content_scripts": [
{
"matches": [
"http://github.com/*",
"https://github.com/*",
"http://*.github.com/*",
"https://*.github.com/*"
],
"js": ["content.js"]
}
]
}
chrome://extensions/ 페이지에서 개발자 모드를 활성화한 후, 압축해제된 확장 프로그램을 로드합니다.를 클릭하여 등록해보자.browser actions 또는 page actions를 이용.browser action은 브라우저 상단 우측에 버튼을 만들고, page action은 주소창에 나타나는 아이콘임.background script를 아래 코드와 같이 이용하여 추가할 수 있음. (background.js)// background.js
// Add a listener for the browser action
chrome.browserAction.onClicked.addListener(buttonClicked);
function buttonClicked(tab) {
// The user clicked the button!
// 'tab' is an object with information about the current open tab
}
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"],
}
chrome://extensions/ 페이지에서 제공하는 뷰 검사 (백그라운드 페이지)에서 디버깅이 가능함.{
"active": true,
"audible": false,
"autoDiscardable": true,
"discarded": false,
"groupId": -1,
"height": 900,
"highlighted": true,
"id": 831,
"incognito": false,
"index": 2,
"mutedInfo": {
"muted": false
},
"pinned": false,
"selected": true,
"status": "complete",
"width": 1182,
"windowId": 722
}
content script와 background script간 통신을 위해서는 Chrome message API를 사용해야 함.background script에서의 그 예시는 아래와 같음.function buttonClicked(tab) {
var msg = {
message: "user clicked!"
}
chrome.tabs.sendMessage(tab.id, msg);
}
content script에서 받는 부분의 예시는 아래와 같음.// Listen for messages
chrome.runtime.onMessage.addListener(receiver);
// Callback for when a message is received
function receiver(request, sender, sendResponse) {
if (request.message === "user clicked!") {
// Do something!
}
}
manifest.json에 참조를 하는 방식으로 가능content script나 background script와 messaging API를 통해 통신이 가능."browser_action": {
"default_title": "you can also add a tool tip here",
"default_popup": "popup.html"
}
background script의 변수에 접근할 수도 있음.var word = chrome.extension.getBackgroundPage().word;
manifest.json에 아래 옵션을 넣으면 됨.background script에서 처리 가능함. "omnibox": {
"keyword": "a2z"
}
// This event is fired when the user hits "enter"
chrome.omnibox.onInputEntered.addListener(omniChanged);
function omniChanged(text) {
// The variable "text" has the text the user typed
// You could open a new tab on a specific page that uses that text
// or send a message to a content script
// etc.
}
manifest.json에 등록 "chrome_url_overrides": {
"newtab": "newtab.html"
}