オプションページがあり、バックグラウンドページと通信したい場合は、chrome.extension.getBackgroundPage()を実行するだけです。
背景ページと通信するオプションページ
options.html
var bkg = chrome.extension.getBackgroundPage()
bkg.startExtension();
bkg.stopExtension();
background.html
function startExtension() {
console.log('Starting Extension');
}
function stopExtension() {
console.log('Stopping Extension');
}
バックグラウンドページと通信するコンテンツスクリプト
「ページに挿入されているコード」という場合、それはどのWebサイトですか。その場合は、メッセージパッシングでコンテンツスクリプトを使用する必要があります。そうするために、あなたはこれをすることができます。
content_script.js
chrome.extension.sendRequest({action:'start'}, function(response) {
console.log('Start action sent');
});
background.html
function onRequest(request, sender, sendResponse) {
if (request.action == 'start')
startExtension()
else if (request.action == 'stop')
stopExtension()
sendResponse({});
};
chrome.extension.onRequest.addListener(onRequest);
いずれにせよ、メッセージパッシングは拡張機能を利用する人にとっては良い読み物です。