Skip to content

Instantly share code, notes, and snippets.

@sarnobat
Last active June 6, 2024 03:58
Show Gist options
  • Save sarnobat/ba47fe3bf74c0afc48849d59e4364348 to your computer and use it in GitHub Desktop.
Save sarnobat/ba47fe3bf74c0afc48849d59e4364348 to your computer and use it in GitHub Desktop.

https://github.com/sarnobat/chrome_extension

manifest.json

{
  "name": "Close Tab",
  "version": "1.0",
  "description": "Closes tabs that have been stashed (looks for the word 'Success')",
  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["content.js"]
    }
  ],
  "background": {
    "service_worker": "service_worker.js",
    "type": "module"
  },
  "host_permissions": [
	"http://*/*",
	"https://*/*"
	],
  "permissions": [
	"tabs",
	"alarms"
  ],
  "manifest_version": 3
}

content.js

chrome.runtime.onMessage.addListener(
	function(request, sender, sendResponse) {
		console.debug('request = ' + request);
		console.debug('sender = ' + sender);
		console.debug('sendResponse = ' + sendResponse);
		var success =  document.getElementsByTagName("body")[0].innerHTML.startsWith("Success");
		if (success) {
			// respond to background script telling it to close the tab
			sendResponse({
				tab: request.tab,
				counter: request.counter+1,
				message : "found success"
			});
		} else {
			sendResponse({
				tab: request.tab,
				counter: request.counter+1,
				message : "did not yet succeed"
			});
		}
	}
);

service_worker.js

// 0.1 takes too much processing. Every 30-60 secs would be better.
chrome.alarms.create("Close unprivileged stashed windows", {
	delayInMinutes: 0.1,
	periodInMinutes: 0.1
});

chrome.alarms.onAlarm.addListener(function( alarm ) {

	chrome.tabs.query({}, function(tabs) {

//	chrome.tabs.getAllInWindow(null, function(tabs){
		for (var i = 0; i < tabs.length; i++) {
			//chrome.tabs.sendRequest(tabs[i].id, { action: "xxx" });
			var tab = tabs[i];
			var url = tabs[i].url;
//			console.log("-");
			if (url.includes('netgear.rohidekar.com/yurl/stash') ||
			    url.includes('netgear.rohidekar.com/yurl/httpcat') ||
			    url.includes('http://localhost:4466/')) {
				console.log("(sending message to stashed tab - but may not be successful yet): " + url);
				chrome.tabs.sendMessage(tab.id, {tab: tab, counter: 0, message : "was stashing tab " + tab.id +  " successful?" }, null, function handler(response) {
					// doesn't work
					//debugger;
					console.debug("chrome.runtime.onMessage = " + chrome.runtime.onMessage);
					console.debug(response);
					console.debug(response.message);
					if (response.message == 'found success') {
//						alert('closing:  ' + tab.url);
						chrome.tabs.remove(response.tab.id, function (){});
					} else {
						console.debug('Cannot close yet, will try again later');
					}
				});
			}
		}
	});
});

//chrome.alarms.create("Close unprivileged stashed windows", object alarmInfo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment