Skip to content

Commit 1dfa82f

Browse files
authored
Merge pull request #210 from psiinon/chrome-dl-fix
Fix download on script completion on Chrome
2 parents dc5d562 + 6c0a440 commit 1dfa82f

File tree

7 files changed

+82
-57
lines changed

7 files changed

+82
-57
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ All notable changes to the full browser extension will be documented in this fil
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

6+
## Unreleased
7+
8+
### Fixed
9+
- Download on script completion on Chrome.
10+
- Download file date string.
11+
612
## 0.1.2 - 2025-06-17
713

814
### Added

CHANGELOG.rec.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ All notable changes to the recorder browser extension will be documented in this
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

6+
## Unreleased
7+
8+
### Fixed
9+
- Download on script completion on Chrome.
10+
- Download file date string.
11+
612
## 0.1.2 - 2025-06-17
713

814
### Added

source/Background/index.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {ReportedStorage} from '../types/ReportedModel';
2323
import {ZestScript, ZestScriptMessage} from '../types/zestScript/ZestScript';
2424
import {ZestStatementWindowClose} from '../types/zestScript/ZestStatement';
2525
import {
26-
DOWNLOAD_RECORDING,
2726
GET_ZEST_SCRIPT,
2827
IS_FULL_EXTENSION,
2928
LOCAL_STORAGE,
@@ -173,33 +172,6 @@ function sendZestScriptToZAP(
173172
}
174173
}
175174

176-
function downloadZestScript(zestScriptJSON: string, title: string): void {
177-
const blob = new Blob([zestScriptJSON], {type: 'application/json'});
178-
const url = URL.createObjectURL(blob);
179-
180-
const link = document.createElement('a');
181-
link.href = url;
182-
link.download = title + (title.slice(-4) === '.zst' ? '' : '.zst');
183-
link.style.display = 'none';
184-
185-
document.body.appendChild(link);
186-
link.click();
187-
document.body.removeChild(link);
188-
189-
URL.revokeObjectURL(url);
190-
}
191-
192-
function pad(i: number): string {
193-
return `${i}`.padStart(2, `0`);
194-
}
195-
196-
function getDateString(): string {
197-
const now = new Date();
198-
return `${now.getFullYear()}-${pad(now.getMonth())}-${pad(
199-
now.getDay()
200-
)}-${pad(now.getHours())}-${pad(now.getMinutes())}-${pad(now.getSeconds())}`;
201-
}
202-
203175
async function handleMessage(
204176
request: MessageEvent,
205177
zapurl: string,
@@ -277,17 +249,6 @@ async function handleMessage(
277249
}
278250
break;
279251
}
280-
case DOWNLOAD_RECORDING: {
281-
zestScript.getZestScript().then((items) => {
282-
const msg = items as ZestScriptMessage;
283-
let site = '';
284-
if (request.data) {
285-
site = `${request.data}-`;
286-
}
287-
downloadZestScript(msg.script, `zap-rec-${site}${getDateString()}.zst`);
288-
});
289-
break;
290-
}
291252
case SET_SAVE_SCRIPT_ENABLE:
292253
Browser.storage.sync.set({
293254
zapenablesavescript: zestScript.getZestStatementCount() > 0,

source/ContentScript/recorder.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ import {
2929
ZestStatementLaunchBrowser,
3030
ZestStatementSwitchToFrame,
3131
} from '../types/zestScript/ZestStatement';
32+
import {ZestScriptMessage} from '../types/zestScript/ZestScript';
3233
import {getPath} from './util';
34+
import {downloadJson} from '../utils/util';
3335
import {
34-
DOWNLOAD_RECORDING,
36+
GET_ZEST_SCRIPT,
3537
STOP_RECORDING,
3638
ZAP_FLOATING_DIV,
3739
ZAP_FLOATING_DIV_ELEMENTS,
@@ -413,6 +415,19 @@ class Recorder {
413415
}
414416
}
415417

418+
pad(i: number): string {
419+
return `${i}`.padStart(2, `0`);
420+
}
421+
422+
getDateString(): string {
423+
const now = new Date();
424+
return `${now.getFullYear()}-${this.pad(now.getMonth() + 1)}-${this.pad(
425+
now.getDate()
426+
)}-${this.pad(now.getHours())}-${this.pad(now.getMinutes())}-${this.pad(
427+
now.getSeconds()
428+
)}`;
429+
}
430+
416431
insertFloatingPopup(): void {
417432
if (this.floatingWindowInserted) {
418433
const floatingDiv = document.getElementById(ZAP_FLOATING_DIV);
@@ -496,10 +511,18 @@ class Recorder {
496511
})
497512
.then((items) => {
498513
if (items.downloadScript) {
499-
Browser.runtime.sendMessage({
500-
type: DOWNLOAD_RECORDING,
501-
data: window.location.hostname,
502-
});
514+
Browser.runtime
515+
.sendMessage({type: GET_ZEST_SCRIPT})
516+
.then((items2) => {
517+
const msg = items2 as ZestScriptMessage;
518+
downloadJson(
519+
msg.script,
520+
`zap-rec-${
521+
window.location.hostname
522+
}${this.getDateString()}.zst`
523+
);
524+
});
525+
503526
Browser.storage.sync.set({downloadScript: false});
504527
}
505528
});

source/Popup/index.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ZAP_START_RECORDING,
3232
ZAP_STOP_RECORDING,
3333
} from '../utils/constants';
34+
import {downloadJson} from '../utils/util';
3435
import {ZestScriptMessage} from '../types/zestScript/ZestScript';
3536

3637
const STOP = i18n.t('stop');
@@ -193,19 +194,11 @@ function downloadZestScript(zestScriptJSON: string, title: string): void {
193194
scriptNameInput?.focus();
194195
return;
195196
}
196-
const blob = new Blob([zestScriptJSON], {type: 'application/json'});
197-
const url = URL.createObjectURL(blob);
197+
downloadJson(
198+
zestScriptJSON,
199+
title + (title.slice(-4) === '.zst' ? '' : '.zst')
200+
);
198201

199-
const link = document.createElement('a');
200-
link.href = url;
201-
link.download = title + (title.slice(-4) === '.zst' ? '' : '.zst');
202-
link.style.display = 'none';
203-
204-
document.body.appendChild(link);
205-
link.click();
206-
document.body.removeChild(link);
207-
208-
URL.revokeObjectURL(url);
209202
Browser.runtime.sendMessage({type: RESET_ZEST_SCRIPT});
210203
Browser.storage.sync.set({
211204
zaprecordingactive: false,

source/utils/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const ZAP_START_RECORDING = 'zapStartRecording';
4141
export const SET_SAVE_SCRIPT_ENABLE = 'setSaveScriptEnable';
4242
export const ZEST_SCRIPT = 'zestScript';
4343

44-
export const DOWNLOAD_RECORDING = 'downloadRecording';
4544
export const STOP_RECORDING = 'stopRecording';
4645
export const RESET_ZEST_SCRIPT = 'resetZestScript';
4746
export const GET_ZEST_SCRIPT = 'getZestScript';

source/utils/util.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Zed Attack Proxy (ZAP) and its related source files.
3+
*
4+
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
5+
*
6+
* Copyright 2025 The ZAP Development Team
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
function downloadJson(json: string, title: string): void {
22+
const blob = new Blob([json], {type: 'application/json'});
23+
const url = URL.createObjectURL(blob);
24+
25+
const link = document.createElement('a');
26+
link.href = url;
27+
link.download = title;
28+
link.style.display = 'none';
29+
30+
document.body.appendChild(link);
31+
link.click();
32+
document.body.removeChild(link);
33+
34+
URL.revokeObjectURL(url);
35+
}
36+
37+
export {downloadJson};

0 commit comments

Comments
 (0)