Skip to content

Commit 8af267c

Browse files
authored
Merge pull request #2 from clear-code/Suppress_ERR_CACHE_MISS
Add feature to automatically close ERR_CACHE_MISS page
2 parents 50eac74 + fcc7696 commit 8af267c

File tree

7 files changed

+204
-26
lines changed

7 files changed

+204
-26
lines changed

doc/Sources/user-guide.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ toc-own-page: true
3535
WEBサイトをリロードした際に「フォームを再送信しますか?」ダイアログが表示されたとき、自動でキャンセルしリロードを中止するソリューションです。
3636
誤操作によるフォーム再送信の防止を目的としています。 
3737

38+
また、「フォームを再送信しますか?」というエラーページ(ERR_CACHE_MISS)が表示された場合に、自動でそのエラーページを閉じます。
39+
3840
\newpage
3941
# システム要件
4042

@@ -381,7 +383,7 @@ https://example.com/*
381383
382384
#### @WARNING_WHEN_CLOSE_DIALOG
383385
384-
フォームを再送信しますか?」ダイアログをキャンセルしたとき、以下のような追加の警告ダイアログを表示します。
386+
フォームを再送信しますか?」ダイアログをキャンセルしたとき、以下のような追加の警告ダイアログを表示します。
385387
386388
![](user-guide/media/image31.png)
387389
@@ -392,6 +394,8 @@ https://example.com/*
392394
@WARNING_WHEN_CLOSE_DIALOG
393395
```
394396
397+
「フォームを再送信しますか?」のエラーページ(ERR_CACHE_MISS)が表示され、自動でそのエラーページを閉じるケースでは、この警告ダイアログは表示されません。
398+
395399
### 対象URL一覧
396400
397401
対象URL一覧については、`[TARGETS]`セクションに記載します。

doc/verify/sources/PreReleaseTests.md

Lines changed: 104 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,31 @@
3333

3434
#### 検証
3535

36-
* https://www.google.com/ を開く
37-
* すべてのURLを対象としている場合も、http/httpsのサイトを開いていないとアドインが動作しないため
36+
##### ダイアログを自動でキャンセルするケース
37+
38+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
39+
* `http://localhost:8080`で簡易Webサーバーが起動する
3840
* `doc\verify\sources\TestTools\form.html` を開く
3941
* フォームに「test」と入力して、「送信」ボタンを押す
40-
* 「form.html」 をリロードする
42+
* 遷移先の http://localhost:8080 をリロードする
4143
* [ ] 「フォームを再送信しますか?」ダイアログ一瞬表示され、キャンセルされること
4244
* この状態で、2分程待機する(時間経過で拡張機能が停止しないことの確認)
43-
* 「form.html」 をリロードする
45+
* http://localhost:8080 をリロードする
4446
* [ ] 「フォームを再送信しますか?」ダイアログ一瞬表示され、キャンセルされること
4547

46-
### すべてのURLを対象にした時の動作確認(警告ダイアログ無)
48+
##### ERR_CACHE_MISSページを自動で閉じるケース
49+
50+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
51+
* `http://localhost:8080`で簡易Webサーバーが起動する
52+
* `doc\verify\sources\TestTools\form.html` を開く
53+
* 「フォームをダイアログで開くボタン」を押す
54+
* ポップアップダイアログが開く
55+
* フォームに「test」と入力して、「送信」ボタンを押す
56+
* 遷移先の http://localhost:8080 で右クリックし、「戻る」を押す
57+
* 遷移先の 「form.html」 で右クリックし、「進む」を押す
58+
* [ ] ポップアップダイアログが閉じること
59+
60+
### すべてのURLを対象にした時の動作確認(警告ダイアログ有)
4761

4862
#### 準備
4963

@@ -55,8 +69,10 @@
5569

5670
#### 検証
5771

58-
* https://www.google.com/ を開く
59-
* すべてのURLを対象としている場合も、http/httpsのサイトを開いていないとアドインが動作しないため
72+
##### ダイアログを自動でキャンセルするケース
73+
74+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
75+
* `http://localhost:8080`で簡易Webサーバーが起動する
6076
* `doc\verify\sources\TestTools\form.html` を開く
6177
* フォームに「test」と入力して、「送信」ボタンを押す
6278
* 「form.html」 をリロードする
@@ -72,11 +88,25 @@
7288
* [ ] 警告ダイアログが前面に表示されること(Edgeの後ろに隠れないこと)
7389
* すべての警告ダイアログをOKで閉じる
7490

75-
### 特定のURLを対象にした時の動作確認(警告ダイアログ無)
91+
##### ERR_CACHE_MISSページを自動で閉じるケース
92+
93+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
94+
* `http://localhost:8080`で簡易Webサーバーが起動する
95+
* `doc\verify\sources\TestTools\form.html` を開く
96+
* 「フォームをダイアログで開くボタン」を押す
97+
* ポップアップダイアログが開く
98+
* フォームに「test」と入力して、「送信」ボタンを押す
99+
* 遷移先の http://localhost:8080 で右クリックし、「戻る」を押す
100+
* 遷移先の 「form.html」 で右クリックし、「進む」を押す
101+
* [ ] 「フォームの再送信が発生するため、このサイトでのリロードは禁止されています。\n\nリロードはキャンセルされました。」という警告ダイアログが表示**されない**こと
102+
* このケースは非サポート。警告を出しているネイティブアプリ側ではなく、拡張機能側が閉じていることと、そもそもキャンセルされているので。
103+
* [ ] ポップアップダイアログが閉じること
104+
105+
### ダイアログを閉じる機能で特定のURLを対象にした時の動作確認(警告ダイアログ無)
76106

77107
#### 補足
78108

79-
本アドオンは、現在のタブが指定したURLを開いていなくても、いずれかのタブで指定したURLが開いている場合に動作する。
109+
本アドオンのダイアログを自動でキャンセルする機能は、現在のタブが指定したURLを開いていなくても、いずれかのタブで指定したURLが開いている場合に動作する。
80110
これは、ネイティブアプリ側で現在開いているタブを判定するのが難しいためである。
81111

82112
#### 準備
@@ -95,6 +125,10 @@
95125

96126
#### 検証
97127

128+
##### ダイアログを自動でキャンセルするケース
129+
130+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
131+
* `http://localhost:8080`で簡易Webサーバーが起動する
98132
* `https://example.com/jp/exclude` を開く
99133
* `doc\verify\sources\TestTools\form.html` を開く
100134
* フォームに「test」と入力して、「送信」ボタンを押す
@@ -123,4 +157,64 @@
123157
* `doc\verify\sources\TestTools\form.html` を開く
124158
* フォームに「test」と入力して、「送信」ボタンを押す
125159
* 「form.html」 をリロードする
126-
* [ ] 「フォームを再送信しますか?」ダイアログ一瞬表示され、キャンセルされること
160+
* [ ] 「フォームを再送信しますか?」ダイアログ一瞬表示され、キャンセルされること
161+
162+
### ERR_CACHE_MISSページを閉じる機能で特定のURLを対象にした時の動作確認
163+
164+
#### 補足
165+
166+
ERR_CACHE_MISSページを閉じる機能は指定したURLでのみ動作する。
167+
168+
#### 対象のページではない場合
169+
170+
##### 準備
171+
172+
以下の通り設定して検証を行う。
173+
174+
* [doc\verify\sources\TestTools/Scenarios/scenario3.ini](../TestTools/Scenarios/scenario3.ini)`C:\Program Files\RepostConfirmationCanceler\RepostConfirmationCanceler.ini` に配置する。
175+
* 設定の内容は以下の通り
176+
* 以下のサイトを対象とする
177+
* `*://example.com/jp*`
178+
* `*://example.com/us/??/`
179+
* `https://www.clear-code.com/`
180+
* 以下のサイトを除外する
181+
* `*://example.com/jp/exclude*`
182+
* 念のためEdgeを再起動する
183+
184+
##### 検証
185+
186+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
187+
* `http://localhost:8080`で簡易Webサーバーが起動する
188+
* `doc\verify\sources\TestTools\form.html` を開く
189+
* 「フォームをダイアログで開くボタン」を押す
190+
* ポップアップダイアログが開く
191+
* フォームに「test」と入力して、「送信」ボタンを押す
192+
* 遷移先の http://localhost:8080 で右クリックし、「戻る」を押す
193+
* 遷移先の 「form.html」 で右クリックし、「進む」を押す
194+
* [ ] ERR_CACHE_MISS(フォームの再送信しますか?)エラーページが表示されること
195+
196+
#### 対象のページの場合
197+
198+
##### 準備
199+
200+
以下の通り設定して検証を行う。
201+
202+
* [doc\verify\sources\TestTools/Scenarios/scenario4.ini](../TestTools/Scenarios/scenario4.ini)`C:\Program Files\RepostConfirmationCanceler\RepostConfirmationCanceler.ini` に配置する。
203+
* 設定の内容は以下の通り
204+
* 以下のサイトを対象とする
205+
* `*://localhost:8080*`
206+
* 念のためEdgeを再起動する
207+
208+
補足: マッチング処理の実装はダイアログキャンセルと同様であるため、ここでは詳しいマッチング処理の判定まではテストしない。
209+
210+
##### 検証
211+
212+
* `powershell doc\verify\sources\TestTools\http_server.ps1`でローカルのWebサーバーを起動する
213+
* `http://localhost:8080`で簡易Webサーバーが起動する
214+
* `doc\verify\sources\TestTools\form.html` を開く
215+
* 「フォームをダイアログで開くボタン」を押す
216+
* ポップアップダイアログが開く
217+
* フォームに「test」と入力して、「送信」ボタンを押す
218+
* 遷移先の http://localhost:8080 で右クリックし、「戻る」を押す
219+
* 遷移先の 「form.html」 で右クリックし、「進む」を押す
220+
* [ ] ダイアログが閉じること
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[TARGETS]
2+
*://localhost:8080*

doc/verify/sources/TestTools/form.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@
77
<body>
88
<h1>フォーム再送信サンプル</h1>
99
<p>送信ボタンを押下後、リロードすると「フォームを再送信しますか?」ダイアログが表示されます。</p>
10-
<form method="POST">
10+
<p>送信ボタンを押下後、ブラウザの「戻る」「進む」を実行すると、「フォームを再送信しますか?」(ERR_CACHE_MISS)エラーページが表示されます。</p>
11+
<form method="POST" action="http://localhost:8080">
1112
<label>名前: <input type="text" name="name"></label>
1213
<button type="submit">送信</button>
1314
</form>
14-
<p>本フォームは自分自身にデータ送信するため、外部にはデータ送信はしません。</p>
15+
<p>本フォームは外部にはデータ送信しません。</p>
16+
<button id="openFormBtn">フォームをダイアログで開く</button>
17+
18+
<script>
19+
document.getElementById('openFormBtn').addEventListener('click', function() {
20+
window.open('form.html',
21+
'formWindow',
22+
'width=500,height=400,resizable=yes,scrollbars=yes'
23+
);});
24+
</script>
1525
</body>
1626
</html>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Add-Type -AssemblyName System.Net
2+
3+
$listener = New-Object System.Net.HttpListener
4+
$listener.Prefixes.Add("http://localhost:8080/")
5+
$listener.Start()
6+
Write-Host "HTTP server running: http://localhost:8080/"
7+
8+
while ($listener.IsListening) {
9+
try {
10+
$context = $listener.GetContext()
11+
$response = $context.Response
12+
$request = $context.Request
13+
14+
# Disable cache in order to cause ERR_CACHE_MISS
15+
$response.Headers.Add("Cache-Control", "no-store, no-cache, must-revalidate")
16+
$response.Headers.Add("Pragma", "no-cache")
17+
$response.ContentType = "text/html"
18+
19+
$html = @"
20+
<!DOCTYPE html>
21+
<html>
22+
<head><title>PowerShell HTTP Server</title></head>
23+
<body>
24+
<h1>No cached page</h1>
25+
<p>This page is not cached</p>
26+
</body>
27+
</html>
28+
"@
29+
30+
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
31+
$response.ContentLength64 = $buffer.Length
32+
$response.OutputStream.Write($buffer, 0, $buffer.Length)
33+
$response.OutputStream.Close()
34+
} catch {
35+
Write-Warning "Error: $_"
36+
}
37+
}

webextensions/edge/background.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ function wildcardToRegexp(source) {
4747
* ]
4848
* }
4949
*/
50-
const RepostConfirmationCancelerTalkClient = {
50+
const RepostConfirmationCanceler = {
5151
cached: null,
5252

5353
init() {
5454
this.cached = null;
5555
this.ensureLoadedAndConfigured();
56-
console.log('Running as RepostConfirmationCancelerTalkClient Talk client');
56+
console.log('Running RepostConfirmationCanceler');
5757
},
5858

5959
async ensureLoadedAndConfigured() {
@@ -114,7 +114,7 @@ const RepostConfirmationCancelerTalkClient = {
114114
return false;
115115
},
116116

117-
handleURL(config, url){
117+
handleURL(config, url, callbackWhenMatch){
118118
if (!url) {
119119
console.log(`* Empty URL found`);
120120
return false;
@@ -135,7 +135,7 @@ const RepostConfirmationCancelerTalkClient = {
135135
console.log(`handleURL: check for section ${section.Name} (${JSON.stringify(section)})`);
136136
if (this.match(section, urlToMatch)) {
137137
console.log(` => unmatched`);
138-
this.startMonitoring();
138+
callbackWhenMatch();
139139
return true;
140140
}
141141
else {
@@ -153,7 +153,7 @@ const RepostConfirmationCancelerTalkClient = {
153153
for (const tab of tabs) {
154154
const url = tab.url ?? tab.pendingUrl;
155155
console.log(`handleAllTabs ${url} (tab=${tab.id})`);
156-
if(this.handleURL(config, url)){
156+
if(this.handleURL(config, url, this.startMonitoring)){
157157
break;
158158
}
159159
};
@@ -169,15 +169,40 @@ const RepostConfirmationCancelerTalkClient = {
169169

170170
const config = this.cached;
171171
const url = tab.pendingUrl || tab.url;
172-
this.handleURL(config, url);
172+
this.handleURL(config, url, this.startMonitoring);
173173
},
174174

175175
onNavigationCommitted(details) {
176176
const url = details.url;
177177
console.log(`onNavigationCommitted: ${url}`);
178178
const config = this.cached;
179-
this.handleURL(config, url);
179+
this.handleURL(config, url, this.startMonitoring);
180180
},
181+
182+
onErrorOccurred(details) {
183+
console.log('onErrorOccurred:', details);
184+
if (details.error === 'net::ERR_CACHE_MISS') {
185+
const url = details.url;
186+
const tabId = details.tabId;
187+
const config = this.cached;
188+
this.handleURL(config, url, () => {
189+
this.closeTab(tabId);
190+
});
191+
}
192+
},
193+
194+
closeTab(tabId) {
195+
if (tabId !== -1) {
196+
console.log("Closing tab:", tabId);
197+
chrome.tabs.remove(tabId, () => {
198+
if (chrome.runtime.lastError) {
199+
console.log("Error while closing tab:", chrome.runtime.lastError.message)
200+
} else {
201+
console.log("Tab closed");
202+
}
203+
});
204+
}
205+
}
181206
};
182207

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

187212
chrome.alarms.onAlarm.addListener((alarm) => {
188213
if (alarm.name === 'poll-config') {
189-
RepostConfirmationCancelerTalkClient.configure();
190-
RepostConfirmationCancelerTalkClient.handleAllTabs();
214+
RepostConfirmationCanceler.configure();
215+
RepostConfirmationCanceler.handleAllTabs();
191216
//handleURL for all url in tabs.
192217
}
193218
});
194219

220+
chrome.webRequest.onErrorOccurred.addListener(
221+
RepostConfirmationCanceler.onErrorOccurred.bind(RepostConfirmationCanceler),
222+
{urls: ["<all_urls>"]}
223+
);
224+
195225
/* Tab book-keeping for intelligent tab handlings */
196-
chrome.tabs.onUpdated.addListener(RepostConfirmationCancelerTalkClient.onTabUpdated.bind(RepostConfirmationCancelerTalkClient));
197-
chrome.webNavigation.onCommitted.addListener(RepostConfirmationCancelerTalkClient.onNavigationCommitted.bind(RepostConfirmationCancelerTalkClient));
226+
chrome.tabs.onUpdated.addListener(RepostConfirmationCanceler.onTabUpdated.bind(RepostConfirmationCanceler));
227+
chrome.webNavigation.onCommitted.addListener(RepostConfirmationCanceler.onNavigationCommitted.bind(RepostConfirmationCanceler));
198228

199-
RepostConfirmationCancelerTalkClient.init();
229+
RepostConfirmationCanceler.init();

webextensions/edge/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"nativeMessaging",
88
"alarms",
99
"tabs",
10-
"webNavigation"
10+
"webNavigation",
11+
"webRequest"
1112
],
1213
"host_permissions": [
1314
"<all_urls>"

0 commit comments

Comments
 (0)