Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

Improve screenshot & print protection coverage

Marc Seitzf4d00e0a7b85
diff --git a/components/view/ScreenProtection.tsx b/components/view/ScreenProtection.tsx
index de8b054d..c0c1fb38 100644
--- a/components/view/ScreenProtection.tsx
+++ b/components/view/ScreenProtection.tsx
@@ -15,31 +15,33 @@ export const ScreenProtector = () => {
event.stopPropagation();
};
- // Windows screenshot shortcuts
+ // PrintScreen-based shortcuts (Windows + Linux).
+ // The PrintScreen key only emits a `keyup` event in browsers (no `keydown`),
+ // so these must be registered with `keyup` enabled to be detected at all.
useHotkeys(
[
"printscreen", // PrintScreen key
"alt+printscreen", // Alt + PrintScreen (active window)
"meta+printscreen", // Win + PrintScreen (save to file)
- "meta+shift+s", // Win + Shift + S (Snipping Tool)
- "meta+g", // Win + G (Game Bar)
+ "ctrl+printscreen", // Ctrl + PrintScreen
+ "shift+printscreen", // Shift + PrintScreen (selection in some Linux distros)
+ "ctrl+alt+printscreen", // Ctrl + Alt + PrintScreen (some Linux distros)
],
handleScreenshotAttempt,
{
preventDefault: true,
enableOnFormTags: true,
enableOnContentEditable: true,
+ keyup: true,
+ keydown: true,
},
);
- // macOS screenshot shortcuts
+ // Windows snipping/recording shortcuts (these emit `keydown`)
useHotkeys(
[
- "meta+shift",
- "meta+shift+3", // Cmd + Shift + 3 (full screen)
- "meta+shift+4", // Cmd + Shift + 4 (selection)
- "meta+shift+5", // Cmd + Shift + 5 (screenshot utility)
- "meta+shift+4+space", // Cmd + Shift + 4 + Space (window)
+ "meta+shift+s", // Win + Shift + S (Snipping Tool)
+ "meta+g", // Win + G (Game Bar)
],
handleScreenshotAttempt,
{
@@ -49,11 +51,13 @@ export const ScreenProtector = () => {
},
);
- // Linux screenshot shortcuts
+ // macOS screenshot shortcuts
useHotkeys(
[
- "shift+printscreen", // Shift + PrintScreen (selection in some Linux distros)
- "ctrl+alt+printscreen", // Ctrl + Alt + PrintScreen (some Linux distros)
+ "meta+shift+3", // Cmd + Shift + 3 (full screen)
+ "meta+shift+4", // Cmd + Shift + 4 (selection)
+ "meta+shift+5", // Cmd + Shift + 5 (screenshot utility)
+ "meta+shift+4+space", // Cmd + Shift + 4 + Space (window)
],
handleScreenshotAttempt,
{
diff --git a/components/view/view-data.tsx b/components/view/view-data.tsx
index fc28e144..4b0360a9 100644
--- a/components/view/view-data.tsx
+++ b/components/view/view-data.tsx
@@ -205,6 +205,7 @@ export default function ViewData({
<AdvancedExcelViewer
file={viewData.file!}
versionNumber={document.versions[0].versionNumber}
+ screenshotProtectionEnabled={link.enableScreenshotProtection!}
navData={navData}
/>
) : viewData.fileType === "image" ? (
diff --git a/components/view/viewer/advanced-excel-viewer.tsx b/components/view/viewer/advanced-excel-viewer.tsx
index 1f9e3122..be55839f 100644
--- a/components/view/viewer/advanced-excel-viewer.tsx
+++ b/components/view/viewer/advanced-excel-viewer.tsx
@@ -1,23 +1,46 @@
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
import { useSafePageViewTracker } from "@/lib/tracking/safe-page-view-tracker";
import { getTrackingOptions } from "@/lib/tracking/tracking-config";
+import { cn } from "@/lib/utils";
+import { ScreenProtector } from "../ScreenProtection";
import Nav, { TNavData } from "../nav";
import { AwayPoster } from "./away-poster";
export default function AdvancedExcelViewer({
file,
versionNumber,
+ screenshotProtectionEnabled,
navData,
}: {
file: string;
versionNumber: number;
+ screenshotProtectionEnabled: boolean;
navData: TNavData;
}) {
const { linkId, documentId, viewId, isPreview, dataroomId, brand } = navData;
const pageNumber = 1;
+ const [isWindowFocused, setIsWindowFocused] = useState<boolean>(true);
+
+ // Blur the content whenever the window loses focus (e.g. when the OS
+ // screenshot/snipping UI takes over). Only active when protection is enabled.
+ useEffect(() => {
+ if (!screenshotProtectionEnabled) return;
+
+ const handleFocus = () => setIsWindowFocused(true);
+ const handleBlur = () => setIsWindowFocused(false);
+
+ window.addEventListener("focus", handleFocus);
+ window.addEventListener("blur", handleBlur);
+
+ return () => {
+ window.removeEventListener("focus", handleFocus);
+ window.removeEventListener("blur", handleBlur);
+ };
+ }, [screenshotProtectionEnabled]);
+
const startTimeRef = useRef(Date.now());
const visibilityRef = useRef<boolean>(true);
@@ -150,7 +173,12 @@ export default function AdvancedExcelViewer({
className="relative mx-2 flex h-screen flex-col sm:mx-6 lg:mx-8"
>
<iframe
- className="h-full w-full"
+ className={cn(
+ "h-full w-full",
+ !isWindowFocused &&
+ screenshotProtectionEnabled &&
+ "blur-xl transition-all duration-300",
+ )}
src={`https://view.officeapps.live.com/op/embed.aspx?src=${file}&wdPrint=0&action=embedview&wdAllowInteractivity=False`}
></iframe>
<div
@@ -159,6 +187,7 @@ export default function AdvancedExcelViewer({
background: brand?.accentColor || "rgb(3, 7, 18)",
}}
/>
+ {screenshotProtectionEnabled ? <ScreenProtector /> : null}
</div>
<AwayPoster
isVisible={isInactive}
diff --git a/lib/hooks/use-disable-print.ts b/lib/hooks/use-disable-print.ts
index ba8f4967..602edbbe 100644
--- a/lib/hooks/use-disable-print.ts
+++ b/lib/hooks/use-disable-print.ts
@@ -1,53 +1,31 @@
import { useEffect } from "react";
interface UseDisablePrintOptions {
- className?: string;
styleId?: string;
}
export function useDisablePrint({
- className = "printing-disabled",
styleId = "printing-disabled-style",
}: UseDisablePrintOptions = {}) {
useEffect(() => {
+ // Hide all content unconditionally inside the print media query. This is
+ // pure CSS, so it blocks printing / "Save as PDF" even on browsers that
+ // do not fire `beforeprint`/`afterprint` or `matchMedia('print')` change
+ // events (e.g. Samsung Internet, Opera Mini), without relying on JS to
+ // toggle a class at print time.
const style = document.createElement("style");
style.id = styleId;
style.textContent = `
@media print {
- body.${className} * {
+ body {
display: none !important;
}
}
`;
document.head.appendChild(style);
- const handleBeforePrint = () => {
- document.body.classList.add(className);
- };
-
- const handleAfterPrint = () => {
- document.body.classList.remove(className);
- };
-
- window.addEventListener("beforeprint", handleBeforePrint);
- window.addEventListener("afterprint", handleAfterPrint);
-
- const mediaQueryList = window.matchMedia?.("print");
- const mediaQueryHandler = (e: MediaQueryListEvent) => {
- if (e.matches) {
- handleBeforePrint();
- } else {
- handleAfterPrint();
- }
- };
-
- mediaQueryList?.addEventListener?.("change", mediaQueryHandler);
-
return () => {
document.getElementById(styleId)?.remove();
- window.removeEventListener("beforeprint", handleBeforePrint);
- window.removeEventListener("afterprint", handleAfterPrint);
- mediaQueryList?.removeEventListener?.("change", mediaQueryHandler);
};
- }, [className, styleId]);
+ }, [styleId]);
}