Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions webextensions/edge/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ function wildcardToRegexp(source) {
* ]
* }
*/
const RepostConfirmationCancelerTalkClient = {
const RepostConfirmationCanceler = {
Copy link
Contributor Author

@HashidaTKS HashidaTKS Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このオブジェクトの中にERR_CACHE_MISSのページだったらタブを閉じる、という処理を追加した。
そのため単なるTalkClientではなくなったのでリネームした。

cached: null,

init() {
this.cached = null;
this.ensureLoadedAndConfigured();
console.log('Running as RepostConfirmationCancelerTalkClient Talk client');
console.log('Running RepostConfirmationCanceler');
},

async ensureLoadedAndConfigured() {
Expand Down Expand Up @@ -114,7 +114,7 @@ const RepostConfirmationCancelerTalkClient = {
return false;
},

handleURL(config, url){
handleURL(config, url, callbackWhenMatch){
if (!url) {
console.log(`* Empty URL found`);
return false;
Expand All @@ -135,7 +135,7 @@ const RepostConfirmationCancelerTalkClient = {
console.log(`handleURL: check for section ${section.Name} (${JSON.stringify(section)})`);
if (this.match(section, urlToMatch)) {
console.log(` => unmatched`);
this.startMonitoring();
callbackWhenMatch();
Copy link
Contributor Author

@HashidaTKS HashidaTKS Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今回の機能追加で、このメソッドを流用したい。ただ、今回追加した部分と従来の部分でマッチした場合にさせたい動作が異なる。

  • ERR_CACHE_MISSページのURLが合致していた場合はタブを閉じたい。
  • それ以外の場合は、RepostConfirmationCanceler.exeを起動したい。

マッチ時にさせたい動作が異なるので、コールバック化して外部から渡せるようにした。

return true;
}
else {
Expand All @@ -153,7 +153,7 @@ const RepostConfirmationCancelerTalkClient = {
for (const tab of tabs) {
const url = tab.url ?? tab.pendingUrl;
console.log(`handleAllTabs ${url} (tab=${tab.id})`);
if(this.handleURL(config, url)){
if(this.handleURL(config, url, this.startMonitoring)){
break;
}
};
Expand All @@ -169,15 +169,40 @@ const RepostConfirmationCancelerTalkClient = {

const config = this.cached;
const url = tab.pendingUrl || tab.url;
this.handleURL(config, url);
this.handleURL(config, url, this.startMonitoring);
},

onNavigationCommitted(details) {
const url = details.url;
console.log(`onNavigationCommitted: ${url}`);
const config = this.cached;
this.handleURL(config, url);
this.handleURL(config, url, this.startMonitoring);
},

onErrorOccurred(details) {
console.log('onErrorOccurred:', details);
if (details.error === 'net::ERR_CACHE_MISS') {
const url = details.url;
const tabId = details.tabId;
const config = this.cached;
this.handleURL(config, url, () => {
this.closeTab(tabId);
});
}
},

closeTab(tabId) {
if (tabId !== -1) {
console.log("Closing tab:", tabId);
chrome.tabs.remove(tabId, () => {
if (chrome.runtime.lastError) {
console.log("Error while closing tab:", chrome.runtime.lastError.message)
} else {
console.log("Tab closed");
}
});
}
}
};

/* Refresh config for every N minute */
Expand All @@ -186,14 +211,19 @@ chrome.alarms.create('poll-config', {'periodInMinutes': ALARM_MINUTES});

chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'poll-config') {
RepostConfirmationCancelerTalkClient.configure();
RepostConfirmationCancelerTalkClient.handleAllTabs();
RepostConfirmationCanceler.configure();
RepostConfirmationCanceler.handleAllTabs();
//handleURL for all url in tabs.
}
});

chrome.webRequest.onErrorOccurred.addListener(
RepostConfirmationCanceler.onErrorOccurred.bind(RepostConfirmationCanceler),
{urls: ["<all_urls>"]}
);

/* Tab book-keeping for intelligent tab handlings */
chrome.tabs.onUpdated.addListener(RepostConfirmationCancelerTalkClient.onTabUpdated.bind(RepostConfirmationCancelerTalkClient));
chrome.webNavigation.onCommitted.addListener(RepostConfirmationCancelerTalkClient.onNavigationCommitted.bind(RepostConfirmationCancelerTalkClient));
chrome.tabs.onUpdated.addListener(RepostConfirmationCanceler.onTabUpdated.bind(RepostConfirmationCanceler));
chrome.webNavigation.onCommitted.addListener(RepostConfirmationCanceler.onNavigationCommitted.bind(RepostConfirmationCanceler));

RepostConfirmationCancelerTalkClient.init();
RepostConfirmationCanceler.init();
3 changes: 2 additions & 1 deletion webextensions/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"nativeMessaging",
"alarms",
"tabs",
"webNavigation"
"webNavigation",
"webRequest"
],
"host_permissions": [
"<all_urls>"
Expand Down
Loading