Skip to content

Commit f613520

Browse files
committed
Move script blocks into separate JavaScript files
1 parent f980aa1 commit f613520

4 files changed

Lines changed: 73 additions & 54 deletions

File tree

library/Pdfexport/Backend/Chromedriver.php

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Icinga\Module\Pdfexport\Backend;
77

88
use Exception;
9+
use Icinga\Application\Icinga;
910
use Icinga\Module\Pdfexport\ChromeDevTools\ChromeDevTools;
1011
use Icinga\Module\Pdfexport\ChromeDevTools\Command as DevToolsCommand;
1112
use Icinga\Module\Pdfexport\WebDriver\Command;
@@ -17,38 +18,6 @@ class Chromedriver extends WebdriverBackend
1718
{
1819
protected ?ChromeDevTools $dcp = null;
1920

20-
public const ACTIVATE_SCRIPTS = <<<JS
21-
function activateScripts(node) {
22-
if (isScript(node) === true) {
23-
node.parentNode.replaceChild(cloneScript(node) , node);
24-
} else {
25-
var i = -1, children = node.childNodes;
26-
while (++i < children.length) {
27-
activateScripts(children[i]);
28-
}
29-
}
30-
31-
return node;
32-
}
33-
34-
function cloneScript(node) {
35-
var script = document.createElement("script");
36-
script.text = node.innerHTML;
37-
38-
var i = -1, attrs = node.attributes, attr;
39-
while (++i < attrs.length) {
40-
script.setAttribute((attr = attrs[i]).name, attr.value);
41-
}
42-
return script;
43-
}
44-
45-
function isScript(node) {
46-
return node.tagName === 'SCRIPT';
47-
}
48-
49-
activateScripts(document.documentElement);
50-
JS;
51-
5221
public function __construct(string $url)
5322
{
5423
parent::__construct($url, Capabilities::chrome());
@@ -58,8 +27,17 @@ protected function setContent(PrintableHtmlDocument $document): void
5827
{
5928
parent::setContent($document);
6029

30+
$module = Icinga::app()->getModuleManager()->getModule('pdfexport');
31+
if (! method_exists($module, 'getJsDir')) {
32+
$jsPath = join(DIRECTORY_SEPARATOR, [$module->getBaseDir(), 'public', 'js']);
33+
} else {
34+
$jsPath = $module->getJsDir();
35+
}
36+
37+
$activeScripts = file_get_contents($jsPath . '/activate-scripts.js');
38+
6139
$this->driver->execute(
62-
Command::executeScript(self::ACTIVATE_SCRIPTS),
40+
Command::executeScript($activeScripts),
6341
);
6442
$this->driver->execute(
6543
Command::executeScript('new Layout().apply();'),

library/Pdfexport/Backend/HeadlessChromeBackend.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Exception;
99
use GuzzleHttp\Client as HttpClient;
1010
use GuzzleHttp\Exception\ServerException;
11+
use Icinga\Application\Icinga;
1112
use Icinga\Application\Logger;
1213
use Icinga\Application\Platform;
1314
use Icinga\File\Storage\StorageInterface;
@@ -47,26 +48,6 @@ class HeadlessChromeBackend implements PfdPrintBackend
4748
/** @var string */
4849
public const WAIT_FOR_NETWORK = 'wait-for-network';
4950

50-
/** @var string Javascript Promise to wait for layout initialization */
51-
public const WAIT_FOR_LAYOUT = <<<JS
52-
new Promise((fulfill, reject) => {
53-
let timeoutId = setTimeout(() => reject('fail'), 10000);
54-
55-
if (document.documentElement.dataset.layoutReady === 'yes') {
56-
clearTimeout(timeoutId);
57-
fulfill(null);
58-
return;
59-
}
60-
61-
document.addEventListener('layout-ready', e => {
62-
clearTimeout(timeoutId);
63-
fulfill(e.detail);
64-
}, {
65-
once: true
66-
});
67-
})
68-
JS;
69-
7051
protected ?StorageInterface $fileStorage = null;
7152

7253
protected bool $useFilesystemTransfer = false;
@@ -469,11 +450,20 @@ protected function setContent(PrintableHtmlDocument $document): void
469450
'expression' => 'setTimeout(() => new Layout().apply(), 0)',
470451
]);
471452

453+
$module = Icinga::app()->getModuleManager()->getModule('pdfexport');
454+
if (! method_exists($module, 'getJsDir')) {
455+
$jsPath = join(DIRECTORY_SEPARATOR, [$module->getBaseDir(), 'public', 'js']);
456+
} else {
457+
$jsPath = $module->getJsDir();
458+
}
459+
460+
$waitForLayout = file_get_contents($jsPath . '/wait-for-layout.js');
461+
472462
$promisedResult = $this->communicate($page, 'Runtime.evaluate', [
473463
'awaitPromise' => true,
474464
'returnByValue' => true,
475465
'timeout' => 1000, // Failsafe: doesn't apply to `await` it seems
476-
'expression' => static::WAIT_FOR_LAYOUT,
466+
'expression' => $waitForLayout,
477467
]);
478468
if (isset($promisedResult['exceptionDetails'])) {
479469
if (isset($promisedResult['exceptionDetails']['exception']['description'])) {

public/js/activate-scripts.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
function activateScripts(node) {
5+
if (isScript(node) === true) {
6+
node.parentNode.replaceChild(cloneScript(node) , node);
7+
} else {
8+
var i = -1, children = node.childNodes;
9+
while (++i < children.length) {
10+
activateScripts(children[i]);
11+
}
12+
}
13+
14+
return node;
15+
}
16+
17+
function cloneScript(node) {
18+
var script = document.createElement("script");
19+
script.text = node.innerHTML;
20+
21+
var i = -1, attrs = node.attributes, attr;
22+
while (++i < attrs.length) {
23+
script.setAttribute((attr = attrs[i]).name, attr.value);
24+
}
25+
return script;
26+
}
27+
28+
function isScript(node) {
29+
return node.tagName === 'SCRIPT';
30+
}
31+
32+
activateScripts(document.documentElement);

public/js/wait-for-layout.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: 2026 Icinga GmbH <https://icinga.com>
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
new Promise((fulfill, reject) => {
5+
let timeoutId = setTimeout(() => reject('fail'), 10000);
6+
7+
if (document.documentElement.dataset.layoutReady === 'yes') {
8+
clearTimeout(timeoutId);
9+
fulfill(null);
10+
return;
11+
}
12+
13+
document.addEventListener('layout-ready', e => {
14+
clearTimeout(timeoutId);
15+
fulfill(e.detail);
16+
}, {
17+
once: true
18+
});
19+
})

0 commit comments

Comments
 (0)