คือส่วนขยายที่ติดตั้งลงบนบราวเซอร์ Google Chrome บนคอมพิวเตอร์เดสก์ท็อป เพื่อปรับแต่ง เพิ่มฟีเจอร์ต่างๆใน Chrome ดูตัวอย่างมากมายตามลิ้งค์นี้ครับ https://chrome.google.com/webstore/category/extensions?hl=th
หลักๆจะประกอบด้วย
เราจะสร้าง Extensions สำหรับเปลี่ยนสี background ของเว็บไซต์
หรือดาวน์โหลดตัวอย่าง Extensions จากลิ้งค์นี้ https://developer.chrome.com/extensions/examples/tutorials/get_started_complete.zip
1 2 3 4 5 6 |
{ "name": "Seenual Example", "version": "1.0", "description": "Seenual Build an Extension!", "manifest_version": 2 } |
1 2 3 4 5 6 7 8 9 10 11 |
{ "name": "Seenual Example", "version": "1.0", "description": "Seenual Build an Extension!", "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" }, "manifest_version": 2 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{ "name": "Seenual Example", "version": "1.0", "description": "Seenual Build an Extension!", "icons": { "16": "images/icon16.png", "48": "images/icon48.png", "128": "images/icon128.png" }, "background": { "scripts": ["background.js"], "persistent": false }, "manifest_version": 2 } |
1 2 3 4 5 |
chrome.runtime.onInstalled.addListener(function() { chrome.storage.sync.set({color: '#3aa757'}, function() { console.log("The color is green."); }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <head> <style> button { height: 30px; width: 30px; outline: none; } </style> </head> <body> <button id="changeColor"></button> <script src="popup.js"></script> </body> </html> |
1 2 3 4 5 6 |
{ "name": "Getting Started Example", ... "permissions": ["declarativeContent", "storage"], ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
chrome.runtime.onInstalled.addListener(function() { chrome.storage.sync.set({color: '#3aa757'}, function() { console.log('The color is green.'); }); chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { chrome.declarativeContent.onPageChanged.addRules([{ conditions: [new chrome.declarativeContent.PageStateMatcher({ pageUrl: {hostEquals: 'developer.chrome.com'}, }) ], actions: [new chrome.declarativeContent.ShowPageAction()] }]); }); }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. 'use strict'; let changeColor = document.getElementById('changeColor'); chrome.storage.sync.get('color', function(data) { changeColor.style.backgroundColor = data.color; changeColor.setAttribute('value', data.color); }); changeColor.onclick = function(element) { let color = element.target.value; chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.executeScript( tabs[0].id, {code: 'document.body.style.backgroundColor = "' + color + '";'}); }); }; |
1 2 3 4 5 6 |
{ "name": "Getting Started Example", ... "permissions": ["activeTab", "declarativeContent", "storage"], ... } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <style> button { height: 30px; width: 30px; outline: none; margin: 10px; } </style> </head> <body> <div id="buttonDiv"> </div> <div> <p>Choose a different background color!</p> </div> </body> <script src="options.js"></script> </html> |
กำหนดค่าลงใน manifest.js
1 2 3 4 5 6 7 |
{ "name": "Getting Started Example", ... "options_page": "options.html", ... "manifest_version": 2 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let page = document.getElementById('buttonDiv'); const kButtonColors = ['#3aa757', '#e8453c', '#f9bb2d', '#4688f1']; function constructOptions(kButtonColors) { for (let item of kButtonColors) { let button = document.createElement('button'); button.style.backgroundColor = item; button.addEventListener('click', function() { chrome.storage.sync.set({color: item}, function() { console.log('color is ' + item); }) }); page.appendChild(button); } } constructOptions(kButtonColors); |
เรียนรู้เพิ่มเติมตามลิ้งค์ด้านล่างครับ :
Getting Started : https://developer.chrome.com/extensions/getstarted
Overview : https://developer.chrome.com/extensions/overview
Develop Extensions : https://developer.chrome.com/extensions/devguide
Publish in the Chrome Web Store : https://developer.chrome.com/webstore/publish
หน้าจัดการ Chrome Extensions ของเรา https://chrome.google.com/webstore/devconsole/
ป้ายกำกับ:Chrome Extensions, Chrome web store, การสร้าง Chrome Extensions