- Revision
- 289627
- Author
- [email protected]
- Date
- 2022-02-11 09:30:40 -0800 (Fri, 11 Feb 2022)
Log Message
Inject custom styles into PDF.js to make it look like PDFKit
https://bugs.webkit.org/show_bug.cgi?id=236510
Reviewed by Tim Horton.
This introduces a pdfjs-extras directory that is copied in the pdfjs/extras resource subdirectory at build-time.
In that directory contains a content-script.js file that is executed when the PDFJS iframe loads. Currently, that
content script only injects the PDFKit styling, but it may be used for other types of interaction in the future
(e.g. loading a blob, hooking with find-in-page messages, etc.).
In PDFDocument, we add m_iframe, a ref to the PDFDocument iframe, that we can re-use when the iframe loads,
to inject the content script.
PDFDocumentEventListener handles the iframe load event listener.
m_viewerRendered which was set once the <iframe> was added to the document, is now no longer needed, since we can
check for m_iframe directly.
* Modules/pdfjs-extras/[email protected]: Added.
* Modules/pdfjs-extras/content-script.js:
(const.PDFJSContentScript.injectStyle):
(const.PDFJSContentScript.init):
* Modules/pdfjs-extras/[email protected]: Added.
* Modules/pdfjs-extras/[email protected]: Added.
* Modules/pdfjs-extras/style-cocoa.css: Added.
(body):
(@keyframes fade-out):
(to):
(#loadingBar):
(#toolbarContainer):
(#toolbarViewer):
(#toolbarViewer:hover):
(#toolbarViewerMiddle,):
(#toolbarViewerLeft,):
(#toolbarViewer .toolbarButton):
(#toolbarViewer .toolbarButton:active):
(#toolbarViewer .toolbarButton::before):
(#zoomOut):
(#zoomIn):
(#zoomOut + .splitToolbarButtonSeparator):
(#download):
(#viewerContainer):
(#findbar):
(#findbar:not(.hidden) ~ #viewerContainer):
(#findbar::before,):
(#findbarOptionsTwoContainer):
(#findResultsCount,):
(#findbar .splitToolbarButton > .toolbarButton::before):
(#findInput:focus):
(#findInput[data-status="notFound"]):
* WebCore.xcodeproj/project.pbxproj:
* dom/EventListener.h:
* html/PDFDocument.cpp:
(WebCore::PDFDocumentEventListener::handleEvent):
(WebCore::PDFDocumentEventListener::operator== const):
(WebCore::PDFDocument::PDFDocument):
(WebCore::PDFDocument::createDocumentStructure):
(WebCore::PDFDocument::updateDuringParsing):
(WebCore::PDFDocument::finishedParsing):
(WebCore::PDFDocument::injectContentScript):
(WebCore::m_viewerRendered): Deleted.
* html/PDFDocument.h:
Modified Paths
Added Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (289626 => 289627)
--- trunk/Source/WebCore/ChangeLog 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/ChangeLog 2022-02-11 17:30:40 UTC (rev 289627)
@@ -1,3 +1,69 @@
+2022-02-11 Tim Nguyen <[email protected]>
+
+ Inject custom styles into PDF.js to make it look like PDFKit
+ https://bugs.webkit.org/show_bug.cgi?id=236510
+
+ Reviewed by Tim Horton.
+
+ This introduces a pdfjs-extras directory that is copied in the pdfjs/extras resource subdirectory at build-time.
+
+ In that directory contains a content-script.js file that is executed when the PDFJS iframe loads. Currently, that
+ content script only injects the PDFKit styling, but it may be used for other types of interaction in the future
+ (e.g. loading a blob, hooking with find-in-page messages, etc.).
+
+ In PDFDocument, we add m_iframe, a ref to the PDFDocument iframe, that we can re-use when the iframe loads,
+ to inject the content script.
+
+ PDFDocumentEventListener handles the iframe load event listener.
+
+ m_viewerRendered which was set once the <iframe> was added to the document, is now no longer needed, since we can
+ check for m_iframe directly.
+
+ * Modules/pdfjs-extras/[email protected]: Added.
+ * Modules/pdfjs-extras/content-script.js:
+ (const.PDFJSContentScript.injectStyle):
+ (const.PDFJSContentScript.init):
+ * Modules/pdfjs-extras/[email protected]: Added.
+ * Modules/pdfjs-extras/[email protected]: Added.
+ * Modules/pdfjs-extras/style-cocoa.css: Added.
+ (body):
+ (@keyframes fade-out):
+ (to):
+ (#loadingBar):
+ (#toolbarContainer):
+ (#toolbarViewer):
+ (#toolbarViewer:hover):
+ (#toolbarViewerMiddle,):
+ (#toolbarViewerLeft,):
+ (#toolbarViewer .toolbarButton):
+ (#toolbarViewer .toolbarButton:active):
+ (#toolbarViewer .toolbarButton::before):
+ (#zoomOut):
+ (#zoomIn):
+ (#zoomOut + .splitToolbarButtonSeparator):
+ (#download):
+ (#viewerContainer):
+ (#findbar):
+ (#findbar:not(.hidden) ~ #viewerContainer):
+ (#findbar::before,):
+ (#findbarOptionsTwoContainer):
+ (#findResultsCount,):
+ (#findbar .splitToolbarButton > .toolbarButton::before):
+ (#findInput:focus):
+ (#findInput[data-status="notFound"]):
+ * WebCore.xcodeproj/project.pbxproj:
+ * dom/EventListener.h:
+ * html/PDFDocument.cpp:
+ (WebCore::PDFDocumentEventListener::handleEvent):
+ (WebCore::PDFDocumentEventListener::operator== const):
+ (WebCore::PDFDocument::PDFDocument):
+ (WebCore::PDFDocument::createDocumentStructure):
+ (WebCore::PDFDocument::updateDuringParsing):
+ (WebCore::PDFDocument::finishedParsing):
+ (WebCore::PDFDocument::injectContentScript):
+ (WebCore::m_viewerRendered): Deleted.
+ * html/PDFDocument.h:
+
2022-02-11 Wenson Hsieh <[email protected]>
[iOS] Add support for a "markup image" item in the callout bar when selecting a single image
Added: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
(Binary files differ)
Index: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
===================================================================
--- trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:30:40 UTC (rev 289627)
Property changes on: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
___________________________________________________________________
Added: svn:mime-type
+image/png
\ No newline at end of property
Copied: trunk/Source/WebCore/Modules/pdfjs-extras/content-script.js (from rev 289625, trunk/Source/WebCore/html/PDFDocument.h) (0 => 289627)
--- trunk/Source/WebCore/Modules/pdfjs-extras/content-script.js (rev 0)
+++ trunk/Source/WebCore/Modules/pdfjs-extras/content-script.js 2022-02-11 17:30:40 UTC (rev 289627)
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+const PDFJSContentScript = {
+ injectStyle() {
+ const stylesheet = document.createElement("link");
+ stylesheet.rel = "stylesheet";
+ stylesheet.href = ""
+ document.head.appendChild(stylesheet);
+ },
+ init() {
+ this.injectStyle();
+ }
+};
+
+PDFJSContentScript.init();
Added: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
(Binary files differ)
Index: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
===================================================================
--- trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:30:40 UTC (rev 289627)
Property changes on: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
___________________________________________________________________
Added: svn:mime-type
+image/png
\ No newline at end of property
Added: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
(Binary files differ)
Index: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
===================================================================
--- trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/Modules/pdfjs-extras/[email protected] 2022-02-11 17:30:40 UTC (rev 289627)
Property changes on: trunk/Source/WebCore/Modules/pdfjs-extras/[email protected]
___________________________________________________________________
Added: svn:mime-type
+image/png
\ No newline at end of property
Added: trunk/Source/WebCore/Modules/pdfjs-extras/style-cocoa.css (0 => 289627)
--- trunk/Source/WebCore/Modules/pdfjs-extras/style-cocoa.css (rev 0)
+++ trunk/Source/WebCore/Modules/pdfjs-extras/style-cocoa.css 2022-02-11 17:30:40 UTC (rev 289627)
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+body {
+ /* PDF.js variables */
+ --body-bg-color: rgb(128, 128, 128);
+ --scrollbar-color: auto;
+ --scrollbar-bg-color: auto;
+
+ /* Variables from WKPDFHUDView.mm */
+ --layerVerticalOffset: 40px;
+ --layerCornerRadius: 12px;
+ --layerGrayComponent: 0%;
+ --layerAlpha: 0.75;
+ --layerImageScale: 1.5;
+ --layerSeperatorControllerSize: 1.5px;
+ --layerControllerHorizontalMargin: calc(10px * var(--layerImageScale));
+ --layerImageVerticalMargin: 12px;
+ --controlLayerNormalAlpha: 0.75;
+ --controlLayerDownAlpha: 0.45;
+
+ --layerFadeInTimeInterval: 0.25s;
+ --layerFadeOutTimeInterval: 0.5s;
+ --initialHideTimeInterval: 3s;
+}
+
+@keyframes fade-out {
+ from {
+ opacity: 1;
+ }
+ to {
+ opacity: 0;
+ }
+}
+
+#loadingBar {
+ display: none !important;
+}
+
+#toolbarContainer {
+ display: contents;
+}
+
+#toolbarViewer {
+ position: fixed;
+ display: inline-flex;
+ left: 0;
+ right: 0;
+ width: fit-content;
+ height: auto;
+ margin: auto;
+
+ bottom: var(--layerVerticalOffset);
+ padding: var(--layerImageVerticalMargin) var(--layerControllerHorizontalMargin);
+ gap: var(--layerControllerHorizontalMargin);
+
+ background-color: hsla(0, 0%, var(--layerGrayComponent), var(--layerAlpha));
+ transition: var(--layerFadeInTimeInterval) opacity;
+ opacity: 0;
+ border-radius: var(--layerCornerRadius);
+ animation-name: fade-out;
+ animation-delay: var(--initialHideTimeInterval);
+ animation-duration: var(--layerFadeOutTimeInterval);
+ animation-fill-mode: backwards;
+}
+
+#toolbarViewer:hover {
+ transition: var(--layerFadeOutTimeInterval) opacity;
+ opacity: 1;
+}
+
+#toolbarViewerMiddle,
+#toolbarViewerRight,
+#toolbarViewerMiddle > .splitToolbarButton {
+ display: contents;
+}
+
+#toolbarViewerLeft,
+#toolbarViewerMiddle > *:not(.splitToolbarButton),
+#toolbarViewerRight > *:not(#download) {
+ display: none !important;
+}
+
+#toolbarViewer .toolbarButton {
+ width: calc(20px * var(--layerImageScale));
+ height: calc(20px * var(--layerImageScale));
+ padding: 0;
+ background-color: transparent;
+ background-size: contain;
+ background-position: center;
+ background-repeat: no-repeat;
+ opacity: var(--controlLayerNormalAlpha);
+}
+
+#toolbarViewer .toolbarButton:active {
+ opacity: var(--controlLayerDownAlpha);
+}
+
+#toolbarViewer .toolbarButton::before {
+ display: none !important;
+}
+
+#zoomOut {
+ order: 1;
+ background-image: url("./[email protected]");
+}
+
+#zoomIn {
+ order: 2;
+ background-image: url("./[email protected]");
+}
+
+#zoomOut + .splitToolbarButtonSeparator {
+ order: 3;
+ background-color: white;
+ opacity: 0.45;
+ width: var(--layerSeperatorControllerSize);
+ margin-top: -1px;
+ margin-bottom: -1px;
+}
+
+#download {
+ order: 4;
+ background-image: url("./[email protected]");
+ display: block !important;
+}
+
+#viewerContainer {
+ top: 0;
+}
+
+/* Restyle the findbar */
+
+#findbar {
+ position: fixed;
+ top: 0;
+ left: 0;
+ right: 0;
+ margin: 0;
+ display: flex;
+ flex-direction: row-reverse;
+ background: var(--body-bg-color);
+ border-radius: 0;
+ box-shadow: none;
+
+ --main-color: rgba(249, 249, 250, 1);
+
+ --field-color: inherit;
+ --field-bg-color: rgba(0, 0, 0, 0.1);
+ --field-border-color: rgba(255,255,255,0.45);
+ --findbar-nextprevious-btn-bg-color: transparent;
+
+ --toggled-btn-color: rgba(255, 255, 255, 1);
+ --toggled-btn-bg-color: rgba(0, 0, 0, 0.3);
+ --button-hover-color: rgba(102, 102, 103, 1);
+ --toggled-hover-active-btn-color: rgba(0, 0, 0, 0.4);
+
+ --toolbar-icon-bg-color: rgba(255, 255, 255, 1);
+ --toolbar-icon-hover-bg-color: rgba(255, 255, 255, 1);
+}
+
+#findbar:not(.hidden) ~ #viewerContainer {
+ top: 32px;
+}
+
+#findbar::before,
+#findbar::after {
+ display: none;
+}
+
+#findbarOptionsTwoContainer {
+ display: contents;
+}
+
+#findResultsCount,
+#findMsg {
+ background-color: transparent !important;
+ color: inherit !important;
+}
+
+#findbar .splitToolbarButton > .toolbarButton::before {
+ rotate: -90deg;
+ top: 4px;
+}
+
+#findInput:focus {
+ border-color: rgba(0, 103, 244, 0.48);
+ outline: 2px solid rgba(0, 103, 244, 0.48);
+}
+
+#findInput[data-status="notFound"] {
+ background: var(--field-bg-color);
+ color: var(--field-color);
+}
Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (289626 => 289627)
--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2022-02-11 17:30:40 UTC (rev 289627)
@@ -38230,6 +38230,7 @@
5DF50887116F3077005202AB /* Check For Inappropriate Files In Framework */,
71D6AA381DA4E69400B23969 /* Copy modern media controls code and assets */,
49AA6B1727B6755F000B0F64 /* Copy PDF.js Resources */,
+ 49D6AE1B27B6984B0000EB06 /* Copy PDF.js Extras */,
5379C7AA21E5287100E4A8F6 /* Check .xcfilelists */,
6577FFC7276AC9AD0011AEC8 /* Create Symlink to Alt Root Path */,
);
@@ -38410,6 +38411,24 @@
shellPath = /bin/sh;
shellScript = "SRC_DIR=\"$SRCROOT/../ThirdParty/pdfjs\"\nDST_DIR=\"$BUILT_PRODUCTS_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/pdfjs\"\n\nmkdir -p \"$DST_DIR\"\n\nrsync -aq --exclude \".svn\" --exclude \".DS_Store\" \"$SRC_DIR/\" \"$DST_DIR/\"\n\ncd \"$SRC_DIR\"\n";
};
+ 49D6AE1B27B6984B0000EB06 /* Copy PDF.js Extras */ = {
+ isa = PBXShellScriptBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ );
+ inputFileListPaths = (
+ );
+ inputPaths = (
+ );
+ name = "Copy PDF.js Extras";
+ outputFileListPaths = (
+ );
+ outputPaths = (
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ shellPath = /bin/sh;
+ shellScript = "SRC_DIR=\"$SRCROOT/Modules/pdfjs-extras\"\nDST_DIR=\"$BUILT_PRODUCTS_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/pdfjs/extras\"\n\nmkdir -p \"$DST_DIR\"\n\nrsync -aq --exclude \".svn\" --exclude \".DS_Store\" \"$SRC_DIR/\" \"$DST_DIR/\"\n\ncd \"$SRC_DIR\"\n";
+ };
5325BDCC21DFF33E00A0DEE1 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Modified: trunk/Source/WebCore/dom/EventListener.h (289626 => 289627)
--- trunk/Source/WebCore/dom/EventListener.h 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/dom/EventListener.h 2022-02-11 17:30:40 UTC (rev 289627)
@@ -38,8 +38,8 @@
class EventListener : public RefCounted<EventListener>, public CanMakeWeakPtr<EventListener> {
public:
enum Type {
- JSEventListenerType,
- ImageEventListenerType,
+ JSEventListenerType,
+ ImageEventListenerType,
ObjCEventListenerType,
CPPEventListenerType,
ConditionEventListenerType,
@@ -46,6 +46,7 @@
GObjectEventListenerType,
NativeEventListenerType,
SVGTRefTargetEventListenerType,
+ PDFDocumentEventListenerType,
};
virtual ~EventListener() = default;
Modified: trunk/Source/WebCore/html/PDFDocument.cpp (289626 => 289627)
--- trunk/Source/WebCore/html/PDFDocument.cpp 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/html/PDFDocument.cpp 2022-02-11 17:30:40 UTC (rev 289627)
@@ -25,8 +25,11 @@
#include "config.h"
#include "PDFDocument.h"
+#include "AddEventListenerOptions.h"
#include "DOMWindow.h"
#include "DocumentLoader.h"
+#include "EventListener.h"
+#include "EventNames.h"
#include "Frame.h"
#include "HTMLAnchorElement.h"
#include "HTMLBodyElement.h"
@@ -33,6 +36,7 @@
#include "HTMLHtmlElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
+#include "HTMLScriptElement.h"
#include "RawDataDocumentParser.h"
#include "Settings.h"
#include <wtf/IsoMallocInlines.h>
@@ -44,13 +48,8 @@
using namespace HTMLNames;
-PDFDocument::PDFDocument(Frame& frame, const URL& url)
- : HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::PDF })
- , m_viewerRendered(false)
-{
+/* PDFDocumentParser: this receives the PDF bytes */
-}
-
class PDFDocumentParser final : public RawDataDocumentParser {
public:
static Ref<PDFDocumentParser> create(PDFDocument& document)
@@ -87,6 +86,44 @@
document().finishedParsing();
}
+/* PDFDocumentEventListener: event listener for the PDFDocument iframe */
+
+class PDFDocumentEventListener final : public EventListener {
+public:
+ static Ref<PDFDocumentEventListener> create(PDFDocument& document) { return adoptRef(*new PDFDocumentEventListener(document)); }
+
+private:
+ explicit PDFDocumentEventListener(PDFDocument& document)
+ : EventListener(PDFDocumentEventListenerType)
+ , m_document(document)
+ {
+ }
+
+ bool operator==(const EventListener&) const override;
+ void handleEvent(ScriptExecutionContext&, Event&) override;
+
+ WeakPtr<PDFDocument> m_document;
+};
+
+void PDFDocumentEventListener::handleEvent(ScriptExecutionContext&, Event& event)
+{
+ if (event.type() == eventNames().loadEvent)
+ m_document->injectContentScript();
+}
+
+bool PDFDocumentEventListener::operator==(const EventListener& other) const
+{
+ // All PDFDocumentEventListenerType objects compare as equal; OK since there is only one per document.
+ return other.type() == PDFDocumentEventListenerType;
+}
+
+/* PDFDocument */
+
+PDFDocument::PDFDocument(Frame& frame, const URL& url)
+ : HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::PDF })
+{
+}
+
Ref<DocumentParser> PDFDocument::createParser()
{
return PDFDocumentParser::create(*this);
@@ -110,18 +147,32 @@
iframe->setAttribute(styleAttr, AtomString("width: 100%; height: 100%; border: 0; display: block;", AtomString::ConstructFromLiteral));
body->appendChild(iframe);
- m_viewerRendered = true;
+ auto listener = PDFDocumentEventListener::create(*this);
+ iframe->addEventListener("load", listener.copyRef(), false);
+
+ m_iFrame = iframe.ptr();
}
void PDFDocument::updateDuringParsing()
{
- if (!m_viewerRendered)
+ if (!m_iFrame)
createDocumentStructure();
}
void PDFDocument::finishedParsing()
{
- ASSERT(m_viewerRendered);
+ ASSERT(m_iFrame);
}
+void PDFDocument::injectContentScript()
+{
+ ASSERT(m_iFrame);
+ auto script = HTMLScriptElement::create(scriptTag, *this, false, false);
+ script->setAttribute(srcAttr, "webkit-pdfjs-viewer://pdfjs/extras/content-script.js");
+
+ auto* document = m_iFrame->contentDocument();
+ ASSERT(document && document->body());
+ document->body()->appendChild(script);
}
+
+}
Modified: trunk/Source/WebCore/html/PDFDocument.h (289626 => 289627)
--- trunk/Source/WebCore/html/PDFDocument.h 2022-02-11 17:24:47 UTC (rev 289626)
+++ trunk/Source/WebCore/html/PDFDocument.h 2022-02-11 17:30:40 UTC (rev 289627)
@@ -28,6 +28,8 @@
namespace WebCore {
+class HTMLIFrameElement;
+
class PDFDocument final : public HTMLDocument {
WTF_MAKE_ISO_ALLOCATED(PDFDocument);
public:
@@ -38,6 +40,7 @@
void updateDuringParsing();
void finishedParsing();
+ void injectContentScript();
private:
PDFDocument(Frame&, const URL&);
@@ -45,7 +48,7 @@
Ref<DocumentParser> createParser() override;
void createDocumentStructure();
- bool m_viewerRendered = false;
+ RefPtr<HTMLIFrameElement> m_iFrame;
};
} // namespace WebCore