Inter Extension Communication
Extensions同士での連携のために,
Extension A から Bへのメッセージの送信
var B_id = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
var MyName = 'A';
var port = chrome.extension.connect(B_id, {name: MyName});
port.postMessage({message: 'Bさん、こんにちは'});
port.onMessage.addListener(function(msg) {
console.log(msg);
port.disconnect();
});
まず,
Extension Bでの,
var A_name = 'A';
chrome.extension.onConnectExternal.addListener(
function(port) {
port.onMessage.addListener(function(msg, info) {
if (info.name === A_name) {
port.postMessage({message: 'Aさん、こんにちは'});
}
});
});
続いて,
Tabs/ Windows APIの操作
最後に,
Tabs/
//新しいタブを現在のタブの隣に開く
function open_tab(tab){
chrome.tabs.create({
index:tab.index+1,
url:'chrome://newtab/',
selected:true
});
}
//新しいタブをバックグラウンドで現在のタブの隣に開く
function open_tab_background(tab){
chrome.tabs.create({
index:tab.index+1,
url:'chrome://newtab/',
selected:false
});
}
//新しいタブを右端に開く
function open_tab_last(){
chrome.tabs.create({
url:'chrome://newtab/',
selected:true
});
}
//新しいタブをバックグラウンドで右端に開く
function open_tab_last_background(){
chrome.tabs.create({
url:'chrome://newtab/',
selected:false
});
}
//空白のタブを現在のタブの隣に開く
function open_blank_tab(tab){
chrome.tabs.create({
index:tab.index+1,
url:'about:blank',
selected:true
});
}
//空白のタブを右端に開く
function open_blank_tab_last(){
chrome.tabs.create({
url:'about:blank',
selected:true
});
}
//空白のタブをバックグランドで現在のタブの隣に開く
function open_blank_tab_background(tab){
chrome.tabs.create({
index:tab.index+1,
url:'about:blank',
selected:false
});
}
//空白のタブをバックグランドで右端に開く
function open_blank_tab_background(){
chrome.tabs.create({
url:'about:blank',
selected:false
});
}
//現在のタブを閉じる
function close_tab(tab){
chrome.tabs.remove(tab.id);
}
//新しいウィンドウを開く
function open_window(){
chrome.windows.create({url:'chrome://newtab/'});
}
//新しいウィンドウを空白ページで開く
function open_blank_window(){
chrome.windows.create({url:'about:blank'});
}
//現在のウィンドウを閉じる
function close_window(tab){
chrome.windows.remove(tab.windowId);
}
//現在のタブの右隣のタブを選択
function right_tab(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
tabs.forEach(function(_t,i){
if (_t.id === tab.id){
var newtab = tabs[i+1] || tabs[0];
if (newtab){
chrome.tabs.update(newtab.id, {selected:true});
}
}
});
});
}
//現在のタブの左隣のタブを選択
function left_tab(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
tabs.forEach(function(_t,i){
if (_t.id === tab.id){
var newtab = tabs[i-1] || tabs[tabs.length-1];
if (newtab){
chrome.tabs.update(newtab.id, {selected:true});
}
}
});
});
}
//右端のタブを選択
function last_tab(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
var newtab = tabs[tabs.length-1];
if (newtab){
chrome.tabs.update(newtab.id, {selected:true});
}
});
}
//左端のタブを選択
function first_tab(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
var newtab = tabs[0];
if (newtab){
chrome.tabs.update(newtab.id, {selected:true});
}
});
}
//現在のタブ以外を閉じる
function close_other_tabs(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
tabs.forEach(function(_t,i){
if (_t.id !== tab.id) {
chrome.tabs.remove(_t.id);
}
});
});
}
//現在のタブより右のタブを閉じる
function close_right_tabs(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
tabs.reverse().some(function(_t,i){
if (_t.id !== tab.id) {
chrome.tabs.remove(_t.id);
} else {
return true;
}
});
});
}
//現在のタブより左のタブを閉じる
function close_left_tabs(tab){
chrome.tabs.getAllInWindow(tab.windowId, function(tabs){
tabs.some(function(_t,i){
if (_t.id !== tab.id) {
chrome.tabs.remove(_t.id);
} else {
return true;
}
});
});
}
//現在のタブを複製する(ただし、履歴は引き継げない)
function clone_tab(tab){
chrome.tabs.create({index:tab.index+1,url:tab.url});
}
このようにどの操作もかなりシンプルに記述できます。タブの選択を切り替えたい場合はchrome.
そのほか,
まとめ
今回は前回の特集記事以降に実装されたBrowser Actions APIを中心に,