Skip to content
49 changes: 49 additions & 0 deletions src/main/zapHomeFiles/hud/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NoClientIdError extends Error {}

/* exported utils */

// Here utils is using an IIFE so that it is executed as soon as the file is loaded.
const utils = (function () {
/*
* Utility Functions
Expand Down Expand Up @@ -120,6 +121,17 @@ const utils = (function () {
hostname = hostname.split('?')[0];
hostname = hostname.split('#')[0];

// Remove port if present
hostname = hostname.split(':')[0];

// Split the hostname into parts
const parts = hostname.split('.');

// If the hostname has more than two parts, return the last two parts as the domain
if (parts.length > 2) {
return parts.slice(-2).join('.');
}

return hostname;
}

Expand Down Expand Up @@ -734,6 +746,43 @@ const utils = (function () {
return dateObject.toISOString().slice(11, 23);
}

if (typeof module === 'object') { // This if check is for unit tests as they work in Node environment only because module.exports is not supported in browser
module.exports = {
parseRequestHeader,
parseResponseHeader,
isFromTrustedOrigin,
parseDomainFromUrl,
getParameter,
isHUDInitialized,
initializeHUD,
loadFrame,
saveFrame,
registerTool,
registerTools,
loadTool,
writeTool,
loadPanelTools,
loadAllTools,
addToolToPanel,
removeToolFromPanel,
messageFrame,
messageAllTabs,
getAllClients,
getWindowVisibilityState,
messageWindow,
sortToolsByPosition,
configureButtonHtml,
getUpgradedDomain,
getUpgradedUrl,
errorHandler,
getZapFilePath,
getZapImagePath,
zapApiErrorDialog,
log,
timestampToTimeString
};
}

return {
parseRequestHeader,
parseResponseHeader,
Expand Down
12 changes: 6 additions & 6 deletions src/test/js/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import test from 'ava';
const test = require('ava');
const utils = require('../../../src/main/zapHomeFiles/hud/utils.js');

// TODO: use clean build system
// Dynamically load utils functions from utils.js
// <dirty-hack>
const fs = require('fs');
// const fs = require('fs');

const CODE_PATH = './src/main/zapHomeFiles/hud/utils.js';
const jsCode = fs.readFileSync(CODE_PATH, 'utf-8');
global.eval(jsCode);
// const CODE_PATH = './src/main/zapHomeFiles/hud/utils.js';
// const jsCode = fs.readFileSync(CODE_PATH, 'utf-8');
// global.eval(jsCode);
// </dirty-hack>

test('sortToolsByPosition result are in descending order', t => {
const tools = [{
position: 3,
Expand Down
2 changes: 1 addition & 1 deletion src/webpack/loaders/zap-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = function zapLoader(source) {
source = source.replace('<<ZAP_HUD_URL>>', 'https://targetdomain.com');
source = source.replace('<<ZAP_HUD_API>>', 'https://zap/api');
source = source.replace('<<ZAP_HUD_WS>>', 'https://zap/websocket');
source = source.replace('<<ZAP_HUD_TOOLS>>', '\'http://zap/to/some/tool\'');
source = source.replace('<<ZAP_HUD_TOOLS>>', 'http://zap/to/some/tool');
source = source.replace('<<ZAP_SHARED_SECRET>>', 'sometestsecret');

return source;
Expand Down
Loading