background
chrome APIにアクセス可能
DOMへのアクセスは出来ない
popup
アイコンをクリックした時に
表示される領域
content script
DOMの操作やDOMイベントの
アクセスが可能
chrome APIのアクセスは出来ない
選択範囲の色を青色にしてくれる君
chrome.browserAction.onClicked.addListener(tab => {
chrome.tabs.sendMessage(tab.id,
{
action: "clickedIcon"
});
});chrome.extension.onMessage.addListener(msg => {
if (msg.action === "clickedIcon") {
const textarea = document.activeElement;
});chrome.extension.onMessage.addListener(msg => {
if (msg.action === "clickedIcon") {
const textarea = document.activeElement;
const sentence = textarea.value;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
//substring: 開始位置と終了位置を指定して文字列を切り出す
const before = sentence.substring(0, start);
const target = sentence.substring(start, end);
const after = sentence.substring(end);
if (target !== '') {
textarea.value = before +
"<font color=\"" + "blue" + "\">" + target + "</font>" + after;
}
}
});{
"manifest_version": 2,
"name": "markdown blue helper",
"version": "1.0",
"description": "Simple tool to insert blue font tag.",
"author": "tjmtmmnk",
"permissions": ["tabs"],
"icons": {"32": "icons/icon32.png"},
"browser_action": {
"default_icon": { "32": "icons/icon32.png"},
"default_title": "color-changer"
},
"content_scripts": [
{
"js": ["js/change_color.js"],
"matches": ["<all_urls>"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": false
}
}