{"id":936,"date":"2020-03-07T23:28:59","date_gmt":"2020-03-07T14:28:59","guid":{"rendered":"https:\/\/dalomo.net\/blog\/?p=936"},"modified":"2020-03-12T22:52:00","modified_gmt":"2020-03-12T13:52:00","slug":"google-chrome%e3%81%ae%e6%8b%a1%e5%bc%b5%e3%82%92%e4%bd%9c%e3%81%a3%e3%81%a6%e3%81%bf%e3%81%9f%e3%81%84","status":"publish","type":"post","link":"https:\/\/dalomo.net\/blog\/2020\/03\/07\/936\/","title":{"rendered":"Google Chrome\u306e\u62e1\u5f35\u3092\u4f5c\u3063\u3066\u307f\u305f\u3044 \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb"},"content":{"rendered":"<h1>Chrome\u306e\u62e1\u5f35<\/h1>\n<p>\u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001Chrome\u306e\u62e1\u5f35\u306e\u4f5c\u308a\u65b9\u3092\u8abf\u3079\u308b\u3002<\/p>\n<h2>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3084\u308b<\/h2>\n<p><a href=\"https:\/\/developer.chrome.com\/extensions\/getstarted\">https:\/\/developer.chrome.com\/extensions\/getstarted<\/a><\/p>\n<p>\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u3084\u308b\u3002<a href=\"https:\/\/developer.chrome.com\/\">\u3053\u3053<\/a>\u306e\u80cc\u666f\u8272\u3092\u5909\u3048\u308b\u3084\u3064\u307f\u305f\u3044\u3060\u3051\u3069\u3001\u305b\u3063\u304b\u304f\u306a\u306e\u3067\u3053\u306e\u30d6\u30ed\u30b0\u306e\u3092\u5909\u3048\u305f\u3044\u3002<\/p>\n<h2>manifest.json<\/h2>\n<pre><code class=\"json\">{\r\n  \"name\": \"Getting Started Example\",\r\n  \"version\": \"1.0\",\r\n  \"description\": \"Build an Extension!\",\r\n  \"manifest_version\": 2\r\n}<\/code><\/pre>\n<p>\u3053\u308c\u3060\u3051\u3067\u3082\u3046\u62e1\u5f35\u3068\u3057\u3066\u8a8d\u8b58\u3055\u308c\u308b\u307f\u305f\u3044\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-937\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten-300x217.png\" alt=\"\" width=\"300\" height=\"217\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten-300x217.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten.png 480w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u306a\u3063\u305f\u3002<\/p>\n<h2>background.js<\/h2>\n<p>\u843d\u3068\u3057\u3066\u304d\u3066\u3001\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u306b\u5165\u308c\u308b\u3002\u3042\u3068\u3001manifest\u306edescription\u4ee5\u4e0b\u306b<\/p>\n<pre><code class=\"json\">\"background\": {\r\n    \"scripts\": [\"background.js\"],\r\n    \"persistent\": false\r\n  },<\/code><\/pre>\n<p>\u8ffd\u8a18\u3059\u308b\u3053\u3068\u3067background.js\u3092\u4f7f\u3044\u307e\u3059\u3088\u30fc\u3068\u3044\u3046\u610f\u5473\u306b\u306a\u308b\u307f\u305f\u3044\u3002persistant\u306f\u77e5\u3089\u3093\u3002background.js\u306e\u4e2d\u8eab\u306f<\/p>\n<pre><code class=\"js\">'use strict';\r\n\r\nchrome.runtime.onInstalled.addListener(function() {\r\n  chrome.storage.sync.set({color: '#3aa757'}, function() {\r\n    console.log(\"The color is green.\");\r\n  });\r\n});<\/code><\/pre>\n<p>\u306a\u3093\u3067\u3059\u304c\u3001\u306f\u3063\u304d\u308a\u8a00\u3063\u3066\u3055\u3063\u3071\u308a\u610f\u5473\u304c\u5206\u304b\u3089\u3093\u3002\u3046\u30fc\u3093\u3068&#8217;use strict&#8217;;\u304c\u30b3\u30fc\u30c9\u3092\u53b3\u683c\u306b\u66f8\u304b\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u307f\u305f\u3044\u306a\u30e2\u30fc\u30c9\u306e\u6587\u8a00\u307f\u305f\u3044\u3002<a href=\"https:\/\/developer.chrome.com\/apps\/runtime#event-onInstalled\">chrome.runtime.onInstalled.addListener<\/a>\u304c\u62e1\u5f35\u304c\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u305f\u6642\u3068\u304b\u306b\u8d70\u308b\u307f\u305f\u3044\u306a\u3084\u3064\u3002<a href=\"https:\/\/developer.chrome.com\/extensions\/storage\">chrome.storage.sync.set<\/a>\u304cchrome\u5185\u306b\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3084\u3064\u3067\u3001sync\u304c\u540c\u671f\u53ef\u80fd\u306a\u611f\u3058\u3067\u4fdd\u5b58\u3001\u3053\u3053\u3067\u306fcolor\u3068\u3044\u3046\u30ad\u30fc\u306b\u3001#3aa757\u3068\u3044\u3046\u5024\u3092\u4fdd\u5b58\u3057\u3066\u308b\u3002\u305d\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u2026\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3082\u3088\u304f\u5206\u304b\u3063\u3066\u306a\u3044\u3051\u3069\u3001\u6210\u529f\u30fb\u5931\u6557\u6642\u306bconsole.log\u306b\u8868\u793a\u3059\u308b\u3002<\/p>\n<p>\u3053\u3053\u306e\u3001chrome.storage\u306fchrome\u3067\u7528\u610f\u3055\u308c\u3066\u308bAPI\u3067\u3001\u4f7f\u3046\u306b\u306fmanifest.json\u306bpermission\u3092\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\u306a\u306e\u3067\u3001description\u306e\u4e0b\u306b<\/p>\n<pre><code class=\"json\">\"permissions\": [\"storage\"],<\/code><\/pre>\n<p>\u3092\u8ffd\u8a18\u3059\u308b\u3002\u62e1\u5f35\u6a5f\u80fd\u3092\u518d\u8aad\u8fbc\u3059\u308b\u3068\u3001\u30d0\u30c3\u30af\u30b0\u30e9\u30a6\u30f3\u30c9\u30da\u30fc\u30b8\u306e\u30ea\u30f3\u30af\u304c\u3064\u3044\u3066\u3001\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u30b3\u30f3\u30bd\u30fc\u30eb\u304c\u958b\u304f\u3002<\/p>\n<h2>UserInterface (popup.html, etc)<\/h2>\n<h3>popup.html<\/h3>\n<p><a href=\"https:\/\/dalomo.net\/blog\/files\/popup.html\">https:\/\/dalomo.net\/blog\/files\/popup.html<\/a><\/p>\n<p>\u3053\u308c\u3092\u843d\u3068\u3057\u307e\u3057\u3066\u3002html\u3046\u307e\u304f\u8cbc\u308c\u306a\u3044\u3002style\u30bf\u30b0\u5185\u304ccss\u3063\u3066\u3084\u3064\u3067\u30dc\u30bf\u30f3\u4f5c\u3063\u3066\u308b\u3002button id\u3092changeColor\u3068\u3059\u308b\u3002script\u304c\u3088\u304f\u5206\u304b\u3093\u306a\u3044\u3002\u30dc\u30bf\u30f3\u62bc\u3057\u305f\u6642?html\u958b\u3044\u305f\u6642?\u3069\u3063\u3061\u3067\u5b9f\u884c\u3055\u308c\u3093\u3060\u308d\u3002popup.js\u3082\u4f5c\u3063\u3066\u306a\u3044\u3002\u3069\u3053\u306b\u3042\u308b\u3093\u3060\u3053\u308c\u3002\u3068\u308a\u3042\u3048\u305a\u5148\u306b\u9032\u3080\u3068\u3057\u3066\u3001popup.html\u3092\u4f7f\u3046\u305f\u3081\u306b\u3082\u3001manifest.json\u306b\u8ffd\u8a18\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<pre><code class=\"json\">\"page_action\": {\r\n    \"default_popup\": \"popup.html\"\r\n  },<\/code><\/pre>\n<p>\u3053\u3046\u304b\u3002page_action\u3063\u3066\u306e\u304c\u30e1\u30cb\u30e5\u30fc\u3093\u3068\u3053\u306b\u3042\u308b\u30a2\u30a4\u30b3\u30f3\u3092\u3069\u3046\u306e\u3053\u3046\u306e\u3059\u308b\u3084\u3064\u307f\u305f\u3044\u3002<\/p>\n<h3>icon<\/h3>\n<p>\u30a2\u30a4\u30b3\u30f3\u306e\u6307\u5b9a\u3082\u3053\u3053\u3067\u3067\u304d\u3066img\u3092\u843d\u3068\u3057\u3066\u304d\u3066\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u306b\u7f6e\u304d\u3001<\/p>\n<pre><code class=\"json\">,\r\n      \"default_icon\": {\r\n        \"16\": \"images\/get_started16.png\",\r\n        \"32\": \"images\/get_started32.png\",\r\n        \"48\": \"images\/get_started48.png\",\r\n        \"128\": \"images\/get_started128.png\"\r\n      }<\/code><\/pre>\n<p>\u307e\u305f\u8ffd\u8a18\u3002\u62e1\u5f35\u6a5f\u80fd\u7ba1\u7406\u30da\u30fc\u30b8\u3001\u30a2\u30af\u30bb\u30b9\u8a31\u53ef\u306e\u8b66\u544a\u3001\u304a\u3088\u3073\u30d5\u30a1\u30d3\u30b3\u30f3\u7528\u306b\u3082<\/p>\n<pre><code class=\"json\">\"icons\": {\r\n    \"16\": \"images\/get_started16.png\",\r\n    \"32\": \"images\/get_started32.png\",\r\n    \"48\": \"images\/get_started48.png\",\r\n    \"128\": \"images\/get_started128.png\"\r\n  },<\/code><\/pre>\n<p>\u3068\u8ffd\u8a18\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten02.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-938\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten02-300x214.png\" alt=\"\" width=\"300\" height=\"214\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten02-300x214.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten02.png 484w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u30a2\u30a4\u30b3\u30f3\u3064\u3044\u305f\u3002<\/p>\n<h3>declarativeContent<\/h3>\n<p>\u6307\u5b9aurl\u306b\u3088\u3063\u3066\u30a2\u30a4\u30b3\u30f3\u306e\u72b6\u614b\u3092\u5909\u5316\u3055\u305b\u308b\u305f\u3081\u306bbackground.js\u306e\u3055\u3063\u304d\u306e\u30ea\u30b9\u30ca\u5185\u306b<\/p>\n<pre><code class=\"js\">chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {\r\n    chrome.declarativeContent.onPageChanged.addRules([{\r\n      conditions: [new chrome.declarativeContent.PageStateMatcher({\r\n        pageUrl: {hostEquals: 'developer.chrome.com'},\r\n      })\r\n      ],\r\n          actions: [new chrome.declarativeContent.ShowPageAction()]\r\n    }]);\r\n  });<\/code><\/pre>\n<p>\u3053\u3046\u3002\u3046\u3046\u3046\u3001\u4f55\u3060\u3053\u308c\u306f\u2026\u3002<a href=\"https:\/\/developer.chrome.com\/extensions\/declarativeContent\">chrome.declarativeContent<\/a>\u304c<\/p>\n<blockquote><p>to take actions depending on the content of a page, without requiring permission to read the page&#8217;s content.<\/p>\n<p>\u30da\u30fc\u30b8\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u3092\u8aad\u307f\u53d6\u308b\u8a31\u53ef\u3092\u5fc5\u8981\u3068\u305b\u305a\u306b\u3001\u30da\u30fc\u30b8\u306e\u30b3\u30f3\u30c6\u30f3\u30c4\u306b\u5fdc\u3058\u3066\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p><\/blockquote>\n<p>\u3046\u3093\u3002\u3067\u3001<a href=\"https:\/\/developer.chrome.com\/extensions\/declarativeContent#event-onPageChanged\">onPageChanged<\/a>\u3063\u3066\u3044\u3046\u3050\u3089\u3044\u3060\u304b\u3089\u3001\u30da\u30fc\u30b8\u3092\u9077\u79fb\u3057\u305f\u6642\u306b\u81ea\u5206\u3067\u8a2d\u5b9a\u3057\u305f\u30eb\u30fc\u30eb\u3068\u30de\u30c3\u30c1\u3059\u308b\u304b\u3001\u307f\u305f\u3044\u306a\u611f\u3058\u3060\u308d\u304b\u3002\u305d\u3044\u3067\u305d\u306e\u30eb\u30fc\u30eb\u306e\u7de8\u96c6\u304c<a href=\"https:\/\/developer.chrome.com\/extensions\/events#method-Event-removeRules\">removeRules<\/a>\u3068<a href=\"https:\/\/developer.chrome.com\/extensions\/events#method-Event-addRules\">addRules<\/a>\u304b\u306a\u3002<\/p>\n<p><a href=\"https:\/\/developer.chrome.com\/extensions\/events\">https:\/\/developer.chrome.com\/extensions\/events<\/a><\/p>\n<p>\u3053\u3053\u306e<\/p>\n<blockquote><p>Event objects may support rules. These event objects don&#8217;t call a callback function when events happen but test whether any registered rule has at least one fulfilled condition and execute the actions associated with this rule. Event objects supporting the declarative API have three relevant methods: events.Event.addRules, events.Event.removeRules, and events.Event.getRules.<\/p>\n<p>\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u30eb\u30fc\u30eb\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u3089\u306e\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u3001\u30a4\u30d9\u30f3\u30c8\u304c\u767a\u751f\u3057\u305f\u3068\u304d\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u307e\u305b\u3093\u304c\u3001\u767b\u9332\u3055\u308c\u305f\u30eb\u30fc\u30eb\u306b\u5c11\u306a\u304f\u3068\u30821\u3064\u306e\u6761\u4ef6\u3092\u6e80\u305f\u3059\u6761\u4ef6\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u30c6\u30b9\u30c8\u3057\u3001\u3053\u306e\u30eb\u30fc\u30eb\u306b\u95a2\u9023\u4ed8\u3051\u3089\u308c\u305f\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u5ba3\u8a00\u578bAPI\u3092\u30b5\u30dd\u30fc\u30c8\u3059\u308b\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u306f\u3001events.Event.addRules\u3001 events.Event.removeRules\u3001\u304a\u3088\u3073events.Event.getRules\u306e 3\u3064\u306e\u95a2\u9023\u30e1\u30bd\u30c3\u30c9\u304c\u3042\u308a \u307e\u3059\u3002<\/p><\/blockquote>\n<p>\u3046\u3093\u3002\u305d\u306e\u30eb\u30fc\u30eb\u306e\u66f8\u304d\u65b9\u304c<\/p>\n<pre><code class=\"js\">var rule = {\r\n    conditions: [ \/* my conditions *\/ ],\r\n    actions: [ \/* my actions *\/ ]\r\n};<\/code><\/pre>\n<p>\u304c\u57fa\u672c\u5f62\u3067\u3001\u6700\u4f4e\u3067\u3082conditions\u3068actions\u304c\u5fc5\u8981\u3002<\/p>\n<p><a href=\"https:\/\/developer.chrome.com\/extensions\/events#type-Rule\">https:\/\/developer.chrome.com\/extensions\/events#type-Rule<\/a><\/p>\n<ul>\n<li>conditions\n<ul>\n<li>List of conditions that can trigger the actions.<\/li>\n<\/ul>\n<\/li>\n<li>actions\n<ul>\n<li>List of actions that are triggered if one of the conditions is fulfilled.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u3053\u3053\u3067\u306fconditions\u306b<a href=\"https:\/\/developer.chrome.com\/extensions\/declarativeContent#type-PageStateMatcher\">chrome.declarativeContent.PageStateMatcher<\/a>\u3092\u4f7f\u3063\u3066\u3001pageUrl(<a href=\"https:\/\/developer.chrome.com\/extensions\/events#type-UrlFilter\">UrlFilter<\/a>\u30da\u30fc\u30b8\u306e\u30c8\u30c3\u30d7\u30ec\u30d9\u30ebURL\u306e \u6761\u4ef6\u304c\u6e80\u305f\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306b\u4e00\u81f4)\u306ehostEquals(URL\u306e\u30db\u30b9\u30c8\u540d\u304c\u6307\u5b9a\u3055\u308c\u305f\u6587\u5b57\u5217\u3068\u7b49\u3057\u3044\u5834\u5408\u306b\u4e00\u81f4)\u3067developer.chrome.com\u3092\u6307\u5b9a\u3057\u3066\u308b\u3002\u3053\u306eURL\u3092dalomo.net\u306b\u5909\u3048\u308a\u3083\u3044\u3044\u3093\u3060\u306a\u3002\u307e\u3041\u5f8c\u306b\u3057\u3088\u3002\u3067\u3001actions\u306b<a href=\"https:\/\/developer.chrome.com\/extensions\/declarativeContent#type-ShowPageAction\">chrome.declarativeContent.ShowPageAction<\/a>\u3067\u30e1\u30cb\u30e5\u30fc\u30d0\u30fc\u306e\u30a2\u30a4\u30b3\u30f3\u306b\u8272\u4ed8\u3044\u3066\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3057\u3001\u30a2\u30af\u30c6\u30a3\u30d6\u30bf\u30d6\u3078\u306e\u30a2\u30af\u30bb\u30b9\u304c\u8a31\u53ef\u3055\u308c\u308b\u3002\u5206\u304b\u308a\u3065\u3089\u3044\u3088\u3045\u3002\u6163\u308c\u3066\u306a\u3044\u304b\u3089\u306a\u306e\u304b\u306a\u3041\u2026\u3002<\/p>\n<h3>popup.js<\/h3>\n<p>\u304a\u3049\u3001\u3055\u3063\u304d\u4f5c\u3063\u305fpopup.html\u3067\u547c\u3070\u308c\u308b\u5916\u90e8\u30b9\u30af\u30ea\u30d7\u30c8\u306epopup.js\u3060\u3002<\/p>\n<pre><code class=\"js\">'use strict';\r\n\r\nlet changeColor = document.getElementById('changeColor');\r\nchrome.storage.sync.get('color', function(data) {\r\n  changeColor.style.backgroundColor = data.color;\r\n  changeColor.setAttribute('value', data.color);\r\n});<\/code><\/pre>\n<p>\u3048\u30fc\u3063\u3068\u3001let\u306f\u5909\u6570\u306e\u30b9\u30b3\u30fc\u30d7\u304c\u30d6\u30ed\u30c3\u30af\u5185\u306b\u306a\u308b\u3084\u3064\u3002\u305d\u3053\u306bgetElementById(&#8216;changeColor&#8217;)\u3067\u30a8\u30ec\u30e1\u30f3\u30c8\u3092\u53d6\u5f97\u3002changeColor\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u308b\u30bf\u30b0\u306fButton\u306a\u306e\u3067\u305d\u308c\u304c\u53d6\u5f97\u3055\u308c\u308b\u3002<a href=\"https:\/\/developer.chrome.com\/extensions\/storage#method-StorageArea-get\">chrome.storage.sync.get<\/a>\u3067\u3053\u306e\u524d\u4fdd\u5b58\u3057\u305f\u30ad\u30fc\u306ecolor\u3092\u4f7f\u3063\u3066\u5024\u3092\u53d6\u308a\u51fa\u3059\u3002\u305d\u306e\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3067\u3001\u53d6\u5f97\u3057\u305f\u5024\u3092\u5f15\u6570\u3068\u3057\u3066\u3001button\u3067\u3042\u308bchangeColor\u306estyle.backgroundColor\u3092\u5024\u3067\u8a2d\u5b9a\u3057\u3066\u3001setAttribute\u3067value\u5c5e\u6027\u306b\u5024\u3092\u8a2d\u5b9a\u3059\u308b\u3002\u3067\u3044\u3044\u306e\u304b\u306a\u3002data.color\u3063\u3066\u3044\u3046\u66f8\u304d\u65b9\u3067\u306a\u3093\u3067\u5024\u304c\u5f15\u3063\u5f35\u308c\u308b\u306e\u304b\u5206\u304b\u3089\u3093\u3002style.color\u3063\u3066\u3044\u3046\u306e\u304c\u3042\u3063\u3066\u3001\u305d\u308c\u306e\u623b\u308a\u5024\u304c\u305d\u308c\u3063\u307d\u3044\u611f\u3058\u3060\u3063\u305f\u3051\u3069\u3001\u3053\u3053\u306edata\u3063\u3066style\u306a\u306e\u304b?\u3069\u3046\u306a\u306e?Javascript\u306f\u305d\u3046\u3044\u3046\u3082\u3093\u306a\u306e\u304b\u3082\u3057\u308c\u306a\u3044\u3002\u3053\u3053\u307e\u3067\u7d42\u308f\u3063\u3066popup.html\u306f<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten03.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-939\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten03-297x300.png\" alt=\"\" width=\"297\" height=\"300\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten03-297x300.png 297w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten03-100x100.png 100w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten03.png 626w\" sizes=\"auto, (max-width: 297px) 100vw, 297px\" \/><\/a><\/p>\n<p>\u305f\u3093\u3068\u8ffd\u52a0\u3055\u308c\u3066\u305f\u3002\u3046\u30fc\u3093\u3068\u3001\u3053\u308cstyle\u3068value\u3063\u3066\u4f55\u304c\u9055\u3046\u3093\u3060\u308d\u3002\u8a66\u3057\u306bstyle\u306e\u65b9\u3092\u6d88\u3057\u3066\u307f\u305f\u3089\u8272\u304c\u4ed8\u304b\u306a\u304b\u3063\u305f\u3002\u3067\u3082value\u6d88\u3057\u3066\u3082\u8272\u4ed8\u3044\u305f\u307e\u307e\u3060\u3063\u305f\u3002\u306a\u3093\u3060\u308d\u306a\u3002<\/p>\n<h2>LayerLogic<\/h2>\n<p>\u3088\u3046\u3084\u304f\u80cc\u666f\u8272\u5909\u3048\u308b\u3068\u3053\u308d\u3060\u3002popup.js\u306b<\/p>\n<pre><code class=\"js\">changeColor.onclick = function(element) {\r\n  let color = element.target.value;\r\n  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {\r\n    chrome.tabs.executeScript(\r\n        tabs[0].id,\r\n        {code: 'document.body.style.backgroundColor = \"' + color + '\";'});\r\n  });\r\n};<\/code><\/pre>\n<p>\u3092\u8ffd\u52a0\u3002button\u3067\u3042\u308bchangeColor\u306e.onclick\u3067\u3001\u30af\u30ea\u30c3\u30af\u3057\u305f\u969b\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u8d70\u308b\u3002\u3060\u3051\u3069\u3001\u3053\u3053\u306eelement\u304c\u5206\u304b\u3089\u306a\u3044\u3002\u898b\u305f\u611f\u3058changeColor\u304c\u5165\u3063\u3066\u308b\u3063\u307d\u3044\u3093\u3060\u3051\u3069\u3001\u305d\u3046\u3044\u3046\u3082\u3093\u3068\u3057\u3066\u6349\u3048\u308b\u3057\u304b\u306a\u3044\u3093\u3060\u308d\u3046\u304b\u3002\u4ee3\u5165\u3059\u308b\u6587\u304c\u898b\u5f53\u305f\u3089\u306a\u3044\u304b\u3089\u306a\u3093\u3067\u3053\u306e\u307e\u307e\u4f7f\u3048\u3066\u308b\u306e\u304b\u56f0\u60d1\u3057\u3066\u3057\u307e\u3046\u2026\u3002<a href=\"http:\/\/www.ifelse.jp\/blog\/javascript-02\">\u3053\u3061\u3089<\/a>\u306e<\/p>\n<blockquote>\n<ul>\n<li>\u30a4\u30d9\u30f3\u30c8\u767a\u751f\u6642\u306e\u60c5\u5831\u3092\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u5f15\u6570\u306b\u53d7\u3051\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u3053\u308c\u306fJavaScript\u5074\u3067\u81ea\u52d5\u7684\u306b\u3001\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u306b\u6307\u5b9a\u3057\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3078\u6e21\u3057\u3066\u304f\u308c\u308b\u3082\u306e\u3002<\/li>\n<li>\u30a4\u30d9\u30f3\u30c8\u30cf\u30f3\u30c9\u30e9\u3068\u3057\u3066\u8a2d\u5b9a\u3057\u305f\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306e\u5f15\u6570\u306b\u30a4\u30d9\u30f3\u30c8\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u300ce\u300d\u3092\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002\u300ce\u300d\u306f\u30a4\u30d9\u30f3\u30c8\u767a\u751f\u6642\u306e\u60c5\u5831\u3092\u6301\u3064\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306a\u306e\u3067\u300ce.target.id\u300d\u306e\u3088\u3046\u306b\u60c5\u5831\u3092\u5f97\u308b\u3053\u3068\u304c\u53ef\u80fd\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u3053\u306e\u3042\u305f\u308a\u306a\u306e\u304b\u306a\u3002\u3060\u304b\u3089<a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/Web\/API\/Event\/target\">element.target.value<\/a>\u3067\u3055\u3063\u304d\u5c5e\u6027\u3092\u8ffd\u52a0\u3057\u305fvalue\u306e\u5024\u3092\u5f15\u3063\u5f35\u308c\u308b\u3093\u3060\u3068\u601d\u3046\u3002\u3093\u3067\u3001<a href=\"https:\/\/developer.chrome.com\/extensions\/tabs#method-query\">chrome.tabs.query<\/a>\u3067queryInfo\u3092active(\u30bf\u30d6\u304c\u30a6\u30a3\u30f3\u30c9\u30a6\u3067\u30a2\u30af\u30c6\u30a3\u30d6\u304b\u3069\u3046\u304b\u3002)\u304b\u3064\u3001currentWindow(\u30bf\u30d6\u304c\u73fe\u5728\u306e\u30a6\u30a3\u30f3\u30c9\u30a6\u306b\u3042\u308b\u304b\u3069\u3046\u304b\u3002)\u306a\u3084\u3064\u3092\u6307\u5b9a\u3057\u3066\u3001\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u306b(\u3053\u3053\u306etabs\u3082\u5206\u304b\u3089\u3093)\u3001<a href=\"https:\/\/developer.chrome.com\/extensions\/tabs#method-executeScript\">chrome.tabs.executeScript<\/a>\u3092\u8a2d\u5b9a\u3059\u308b\u3002<a href=\"https:\/\/developer.chrome.com\/extensions\/content_scripts#functionality\">\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u3088\u3063\u3066Content Scripts\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u633f\u5165\u3059\u308b<\/a>\u307f\u305f\u3044\u3067\u3059\u3002\u5f15\u6570\u306f\u3001tabs[0].id\u3067\u73fe\u5728\u306e\u30bf\u30d6\u3092\u6307\u5b9a\u3057\u3066\u3001code\u3092\u633f\u5165\u3057\u3066\u308b\u3002\u300cdocument.body.style.backgroundColor =\u00a0 color;\u300d\u3063\u3066\u3044\u3046\u30b3\u30fc\u30c9\u3092\u4f5c\u3063\u3066\u633f\u5165\u3057\u3066\u5b9f\u884c\u3057\u3066\u308b\u3002\u75b2\u308c\u305f\u3002\u3042\u3001manifest\u306b<\/p>\n<pre><code class=\"json\">\"activeTab\"<\/code><\/pre>\n<p>\u3092\u8ffd\u8a18\u3002background.js\u306e\u30b5\u30a4\u30c8\u3092dalomo.net\u306b\u5909\u66f4\u3057\u3066\u5b9f\u884c\u3057\u3066\u307f\u308b\u3068<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten04.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-940\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten04-300x162.png\" alt=\"\" width=\"300\" height=\"162\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten04-300x162.png 300w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten04-768x415.png 768w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten04.png 1000w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u306a\u3063\u305f!<\/p>\n<h2>options.html<\/h2>\n<p>\u7dd1\u3058\u3083\u306a\u3044\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u305f\u3081\u306e\u30da\u30fc\u30b8\u307f\u305f\u3044\u306a\u306e\u3092\u4f5c\u308b\u3002\u3068\u308a\u3042\u3048\u305a\u30d5\u30a1\u30a4\u30eb\u3092\u843d\u3068\u3057\u3066\u304d\u307e\u3057\u3066\u540c\u3058\u30d5\u30a9\u30eb\u30c0\u306b\u7f6e\u3044\u3066\u3001manifest\u306b<\/p>\n<pre><code class=\"json\">\"options_page\": \"options.html\",<\/code><\/pre>\n<p>\u3068\u8ffd\u8a18\u3002\u305d\u3093\u3067options.js\u3092\u4f5c\u308b\u3002<\/p>\n<pre><code class=\"js\">let page = document.getElementById('buttonDiv');\r\nconst kButtonColors = ['#3aa757', '#e8453c', '#f9bb2d', '#4688f1'];\r\nfunction constructOptions(kButtonColors) {\r\n  for (let item of kButtonColors) {\r\n    let button = document.createElement('button');\r\n    button.style.backgroundColor = item;\r\n    button.addEventListener('click', function () {\r\n      chrome.storage.sync.set({ color: item }, function () {\r\n        console.log('color is ' + item);\r\n      })\r\n    });\r\n    page.appendChild(button);\r\n  }\r\n}\r\nconstructOptions(kButtonColors);<\/code><\/pre>\n<p>getElementById(&#8216;buttonDiv&#8217;)\u3067div\u30bf\u30b0\u306e\u3068\u3053\u3092\u53d6\u5f97\u3002const\u306f\u5b9a\u6570\u3067\u8272\u30b3\u30fc\u30c9\u306e\u914d\u5217\u3092\u4f5c\u308b\u3002constructOptions\u95a2\u6570\u306f\u3001\u305d\u306e\u5b9a\u6570\u306e\u5f15\u6570\u3068\u3057\u3066\u3001\u5b9a\u6570\u306e\u914d\u5217\u306e\u30a2\u30a4\u30c6\u30e0\u6570\u5206createElement\u3067\u30dc\u30bf\u30f3\u3092\u4f5c\u3063\u3066\u304f\u3002style.backgroundColor\u3067\u30dc\u30bf\u30f3\u306e\u8272\u306b\u5b9a\u6570\u306e\u8272\u3092\u8a2d\u5b9a\u3057\u3001addEventListener\u3067\u30dc\u30bf\u30f3\u30af\u30ea\u30c3\u30af\u6642\u306e\u51e6\u7406\u3092\u66f8\u3044\u3066\u304f\u3002storage.sync.set\u3067color\u30ad\u30fc\u306b\u5b9a\u6570\u306e\u8272\u3092\u30bb\u30c3\u30c8\u3059\u308b\u304b\u3089\u3001\u8272\u304c\u5909\u308f\u308b\u306e\u304b\u3002page.appendChild\u3067buttonDiv\u306e\u76f4\u4e0b\u672b\u5c3e\u306bbutton\u3092\u8ffd\u8a18\u3057\u3066\u5b8c\u4e86\u3002<\/p>\n<p><a href=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten05.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-941\" src=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten05-261x300.png\" alt=\"\" width=\"261\" height=\"300\" srcset=\"https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten05-261x300.png 261w, https:\/\/dalomo.net\/blog\/wp-content\/uploads\/2020\/03\/exten05.png 553w\" sizes=\"auto, (max-width: 261px) 100vw, 261px\" \/><\/a><\/p>\n<p>\u7d50\u679c\u3053\u3046\u3044\u3046\u30bd\u30fc\u30b9\u306b\u306a\u308b\u3002\u306a\u308b\u307b\u3069\u306b\u3083\u3093\u3002<\/p>\n<h2>\u611a\u75f4<\/h2>\n<p>\u65e5\u672c\u8a9e\u306e\u60c5\u5831\u3068\u304b\u898b\u308b\u3068\u3055\u30fc\u3001manifest\u3068content.js\u3060\u3051\u3067\u3067\u304d\u308b\u3068\u304b\u66f8\u3044\u3066\u3042\u308b\u304b\u3089\u3055\u30fc\u3001\u305d\u308c\u3067\u3044\u3044\u306e\u304b\u3068\u601d\u3063\u3066\u305f\u3089\u3001\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3060\u3068\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u308b\u306e\u306d\u3002\u3069\u3063\u3061\u304c\u3044\u3044\u306e\u304b\u306f\u4ffa\u306b\u306f\u308f\u304b\u3093\u306a\u3044\u3051\u3069\u3001\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306f\u4ffa\u306b\u306f\u6577\u5c45\u304c\u9ad8\u3044\u306a\u2026\u3002\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3042\u308b\u3093\u3060\u3051\u3069\u3001\u3067\u304d\u308b\u6c17\u304c\u3057\u306a\u304f\u306a\u3063\u3066\u304d\u305f\u306a\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/qiita.com\/k7a\/items\/26d7a22233ecdf48fed8\">https:\/\/qiita.com\/k7a\/items\/26d7a22233ecdf48fed8<\/a><\/p>\n<p><a href=\"https:\/\/qiita.com\/miri4ech\/items\/ffcebaf593f5baa1c112\">https:\/\/qiita.com\/miri4ech\/items\/ffcebaf593f5baa1c112<\/a><\/p>\n<p><a href=\"https:\/\/qiita.com\/y-temp4\/items\/289686fbdde896d22b5e\">https:\/\/qiita.com\/y-temp4\/items\/289686fbdde896d22b5e<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chrome\u306e\u62e1\u5f35 \u3084\u3063\u3066\u307f\u305f\u3044\u3053\u3068\u304c\u3042\u308b\u306e\u3067\u3001Chrome\u306e\u62e1\u5f35\u306e\u4f5c\u308a\u65b9\u3092\u8abf\u3079\u308b\u3002 \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3084\u308b https:\/\/developer.chrome.com\/extensions\/getstarted \u30c1\u30e5\u30fc\u30c8\u30ea\u30a2 &hellip; <a href=\"https:\/\/dalomo.net\/blog\/2020\/03\/07\/936\/\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":937,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[93,92,94,91],"class_list":["post-936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-8","tag-chrome","tag-googlechrome","tag-html","tag-javascript"],"_links":{"self":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/comments?post=936"}],"version-history":[{"count":2,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"predecessor-version":[{"id":965,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/posts\/936\/revisions\/965"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media\/937"}],"wp:attachment":[{"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dalomo.net\/blog\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}