Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Export report in an HTML file #545

Merged
merged 28 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
326c2e7
feat: add summary export components
ayushnirwal Feb 29, 2024
614338d
Merge branch 'develop' into feat/export-html-report
ayushnirwal Mar 4, 2024
d20bac4
ref:report generation
ayushnirwal Mar 6, 2024
39c6d74
style-fix: redesign download report button
ayushnirwal Mar 6, 2024
21794cb
fix:no of frames showing up
ayushnirwal Mar 8, 2024
9f52668
Merge main and resolve conflicts
mohdsayed Apr 1, 2024
a55b3dd
Minor refactoring
mohdsayed Apr 1, 2024
a8c0f04
Minor improvements
mohdsayed Apr 1, 2024
195f778
ref: remove devServer config for report
ayushnirwal Apr 2, 2024
d5ad438
ref: add types
ayushnirwal Apr 2, 2024
41ab3e3
ref: refactor report app assembly
ayushnirwal Apr 3, 2024
05a2dab
feat: add library detection section in report
ayushnirwal Apr 3, 2024
45923bd
ref: move download button
ayushnirwal Apr 3, 2024
73f3c67
Merge branch 'develop' into feat/export-html-report
ayushnirwal Apr 3, 2024
e95c39b
feat:disable download button until library detection is done
ayushnirwal Apr 3, 2024
80ef23e
feat: add disable reason in download report button
ayushnirwal Apr 3, 2024
d733f30
Merge branch 'develop' into feat/export-html-report
ayushnirwal Apr 5, 2024
efa2114
style-fix: update font-sized
ayushnirwal Apr 5, 2024
b1b1fec
feat: name report file based on top level url
ayushnirwal Apr 5, 2024
dd87aae
ref: move download button to menubar
ayushnirwal Apr 7, 2024
3fcf6c0
style-fix: scale down download item
ayushnirwal Apr 8, 2024
b73957d
Fix infinite loading issue when a new tab is opened
mohdsayed Apr 8, 2024
d69e644
ref:refactor report view
ayushnirwal Apr 8, 2024
109e197
ref: add unit test for report view
ayushnirwal Apr 9, 2024
2ad94d6
ref: add unit test for report object generation
ayushnirwal Apr 9, 2024
1d1fb14
chore: merge develop and resolve merge conflicts
ayushnirwal Apr 9, 2024
6f61a0b
ref: move dependencies and return null with invalid data
ayushnirwal Apr 9, 2024
7b511b9
ref: move dependencies and return null with invalid data
ayushnirwal Apr 9, 2024
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
50 changes: 49 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"cli:prebuild": "node ./scripts/delete-build-artifacts.cjs",
"cli:dev": "tsc-watch --build",
"cli:build": "npm run cli:prebuild && tsc --build",
"cli-dashboard:start": "npm run dev --workspace=@ps-analysis-tool/cli-dashboard",
"cli-dashboard:dev": "npm run dev --workspace=@ps-analysis-tool/cli-dashboard",
"cli-dashboard:build": "npm run build --workspace=@ps-analysis-tool/cli-dashboard",
"cli": "node dist/cli/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const LandingHeader = ({ dataMapping = [] }: LandingHeaderProps) => {
<div className="lg:max-w-[729px] flex gap-9 px-4">
{dataMapping.map((circleData, index) => {
return (
<div key={index} className="text-center w-16">
<div key={index} className="text-center w-16 h-fit">
<CirclePieChart
title={circleData.title}
centerCount={circleData.count}
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
},
"devDependencies": {
"@types/react-copy-to-clipboard": "^5.0.4",
"devtools-protocol": "^0.0.1236148"
"devtools-protocol": "^0.0.1236148",
"html-inline-script-webpack-plugin": "^3.2.1"
}
}
2 changes: 1 addition & 1 deletion packages/extension/src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"web_accessible_resources": [
{
"resources": ["assets/data/*.json", "data/*json"],
"resources": ["assets/data/*.json", "data/*json", "report/index.html"],
"matches": ["*://*/*"]
}
]
Expand Down
88 changes: 88 additions & 0 deletions packages/extension/src/utils/downloadReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies.
*/
import {
prepareCookieStatsComponents,
prepareCookiesCount,
prepareFrameStatsComponent,
type DataMapping,
} from '@ps-analysis-tool/design-system';
import { saveAs } from 'file-saver';

/**
* Utility function to download report.
* @param tabCookies Tab cookies.
* @param tabFrames Tab frames.
*/
export default async function downloadReport(tabCookies: any, tabFrames: any) {
const htmlText = await (await fetch('../report/index.html')).text();
const parser = new DOMParser();
const reportDom = parser.parseFromString(htmlText, 'text/html');

const cookieStats = prepareCookiesCount(tabCookies);
const cookiesStatsComponents = prepareCookieStatsComponents(cookieStats);
const frameStateCreator = prepareFrameStatsComponent(tabFrames, tabCookies);

const cookieClassificationDataMapping: DataMapping[] = [
{
title: 'Total cookies',
count: cookieStats.total,
data: cookiesStatsComponents.legend,
},
{
title: '1st party cookies',
count: cookieStats.firstParty.total,
data: cookiesStatsComponents.firstParty,
},
{
title: '3rd party cookies',
count: cookieStats.thirdParty.total,
data: cookiesStatsComponents.thirdParty,
},
];

const blockedCookieDataMapping: DataMapping[] = [
{
title: 'Blocked cookies',
count: cookieStats.blockedCookies.total,
data: cookiesStatsComponents.blocked,
},
];

// Injections
const script = reportDom.createElement('script');
const code = `window.PSAT_DATA = ${JSON.stringify({
cookieClassificationDataMapping,
tabCookies,
cookiesStatsComponents,
tabFrames,
showInfoIcon: true,
showHorizontalMatrix: false,
blockedCookieDataMapping,
showBlockedInfoIcon: true,
frameStateCreator,
})}`;

script.text = code;
reportDom.head.appendChild(script);

const injectedHtmlText = `<head>${reportDom.head.innerHTML}<head><body>${reportDom.body.innerHTML}</body>`;
const html = new Blob([injectedHtmlText]);

saveAs(html, 'report.html');
}
10 changes: 10 additions & 0 deletions packages/extension/src/view/devtools/components/cookies/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { type CookieTableData } from '@ps-analysis-tool/common';
import { useCookie, useSettings } from '../../stateProviders';
import CookiesListing from './cookiesListing';
import AssembledCookiesLanding from './cookieLanding';
import downloadReport from '../../../../utils/downloadReport';

interface CookiesProps {
setFilteredCookies: React.Dispatch<CookieTableData[]>;
Expand All @@ -43,12 +44,16 @@ const Cookies = ({ setFilteredCookies }: CookiesProps) => {
selectedFrame,
changeListeningToThisTab,
tabToRead,
tabCookies,
tabFrames,
} = useCookie(({ state, actions }) => ({
isCurrentTabBeingListenedTo: state.isCurrentTabBeingListenedTo,
loading: state.loading,
selectedFrame: state.selectedFrame,
tabToRead: state.tabToRead,
changeListeningToThisTab: actions.changeListeningToThisTab,
tabCookies: state.tabCookies,
tabFrames: state.tabFrames,
}));

const { allowedNumberOfTabs } = useSettings(({ state }) => ({
Expand Down Expand Up @@ -86,6 +91,11 @@ const Cookies = ({ setFilteredCookies }: CookiesProps) => {
<CookiesListing setFilteredCookies={setFilteredCookies} />
) : (
<CookiesLanding>
<Button
extraClasses="absolute top-3 right-3"
onClick={() => downloadReport(tabCookies, tabFrames)}
text="Download report"
/>
<AssembledCookiesLanding />
</CookiesLanding>
)}
Expand Down
3 changes: 3 additions & 0 deletions packages/extension/src/view/report/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
83 changes: 83 additions & 0 deletions packages/extension/src/view/report/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import React from 'react';
import {
CookiesLandingContainer,
CookiesMatrix,
} from '@ps-analysis-tool/design-system';

/**
* Internal dependencies
*/
import './app.css';

const App = ({ data }: { data: any }) => {
return (
<div className="h-full w-full flex flex-col">
<CookiesLandingContainer
dataMapping={data.cookieClassificationDataMapping}
testId="cookies-insights"
>
<CookiesMatrix
title={data.cookieClassificationTitle}
tabCookies={data.tabCookies}
componentData={data.cookiesStatsComponents.legend}
tabFrames={data.tabFrames}
showInfoIcon={data.showInfoIcon}
showHorizontalMatrix={data.showHorizontalMatrix}
associatedCookiesCount={data.associatedCookiesCount}
/>
</CookiesLandingContainer>
<CookiesLandingContainer
dataMapping={data.blockedCookieDataMapping}
testId="blocked-cookies-insights"
>
{data.cookiesStatsComponents.blockedCookiesLegend.length > 0 && (
<CookiesMatrix
title="Blocked Reasons"
tabCookies={data.tabCookies}
componentData={data.cookiesStatsComponents.blockedCookiesLegend}
tabFrames={data.tabFrames}
showInfoIcon={data.showBlockedInfoIcon}
showHorizontalMatrix={false}
infoIconTitle="Cookies that have been blocked by the browser.(The total count might not be same as cumulative reason count because cookie might be blocked due to more than 1 reason)."
/>
)}
</CookiesLandingContainer>
<CookiesLandingContainer
dataMapping={data.frameStateCreator.dataMapping}
testId="frames-insights"
>
<CookiesMatrix
title="Frames"
componentData={data.frameStateCreator.legend}
showMatrix={true}
tabCookies={data.tabCookies}
tabFrames={data.tabFrames}
showInfoIcon={data.showInfoIcon}
showHorizontalMatrix={false}
infoIconTitle="The details regarding frames and associated cookies in this page."
/>
</CookiesLandingContainer>
</div>
);
};

export default App;
11 changes: 11 additions & 0 deletions packages/extension/src/view/report/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Report</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
36 changes: 36 additions & 0 deletions packages/extension/src/view/report/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import React from 'react';
import { createRoot } from 'react-dom/client';

/**
* Internal dependencies
*/
import App from './app';

document.addEventListener('DOMContentLoaded', () => {
const root = document.getElementById('root');
//@ts-ignore custom data attached to window breaks types
const data = window.PSAT_DATA;

if (root) {
createRoot(root).render(<App data={data} />);
}
});
Loading
Loading