日課のポイ活でページを開いてすぐ閉じるのが少しでも楽になるように作った。
×ボタンまでマウスカーソルを移動させたり、Ctrl+Wを押すのも地味に面倒だった。
本当は他ボタンマウスを買うのがベストだけど代用としてとりあえず作ってみた。
↓manifest.json
{ "name": "closeTab(rightDoubleClick)", "description": "ダブル右クリックでタブを閉じる", "version": "0.0.1", "manifest_version": 3, "permissions": [ "activeTab", "scripting", "tabs" ], "host_permissions": [ "http://*/*", "https://*/*" ], "content_scripts": [ { "matches": ["<all_urls>"], "js": ["content.js"] } ], "background": { "service_worker": "background.js" } }
↓content.js
console.log("スタート!!"); document.addEventListener('mouseup', function(e) { console.log("mouseupが発生しました! button:" + e.button + " detail:" + e.detail); /* e.button 0:左クリック 1:中央ボタン 2:右クリック */ /* e.detail 直近のmouseup回数 */ if(e.button == 2 && e.detail == 2) { /* ↓backgroundにメッセージを送信(contentではtabsを使えないので。。。) */ chrome.runtime.sendMessage({message: "content→background"}, function() { console.log("content→backgroundにメッセージ送信"); }); } }, false);
↓background.js
/* ↓contentからメッセージを受信 */ chrome.runtime.onMessage.addListener((request, sender, sendResponse) =>{ console.log(request.message); getCurrentTab().then((tab) => { chrome.tabs.remove(tab.id); }); }); /* 現在のタブを取得する 公式のコピペ→https://developer.chrome.com/docs/extensions/reference/api/tabs?hl=ja#get_the_current_tab */ async function getCurrentTab() { let queryOptions = { active: true, lastFocusedWindow: true }; let [tab] = await chrome.tabs.query(queryOptions); return tab; }
//以上