diff options
| author | Georgi Nikolov <ggeorgi60@gmail.com> | 2025-01-25 20:35:03 +0200 |
|---|---|---|
| committer | Georgi Nikolov <ggeorgi60@gmail.com> | 2025-01-25 20:35:03 +0200 |
| commit | f1246e8b009ce3cd3229225bf1d57237cad58ce0 (patch) | |
| tree | 8500b1376a4fd2972024f4f4ff91165a08a8def5 | |
| parent | da0952778ec9790b4409cdbbeda12dc618cfd461 (diff) | |
| download | nowayforward_human-f1246e8b009ce3cd3229225bf1d57237cad58ce0.tar nowayforward_human-f1246e8b009ce3cd3229225bf1d57237cad58ce0.tar.gz nowayforward_human-f1246e8b009ce3cd3229225bf1d57237cad58ce0.zip | |
Added downloading of the js imports that are files
| -rw-r--r-- | controllers/archive_page.php | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/controllers/archive_page.php b/controllers/archive_page.php index 88b2b0c..60ab284 100644 --- a/controllers/archive_page.php +++ b/controllers/archive_page.php @@ -183,6 +183,41 @@ class DownloadPage { } } + function handleJsImports(&$content) : void { + if (preg_match_all("/import .*'(.*)'/", $content, $matches, PREG_PATTERN_ORDER) > 0) { + $urls = $matches[1]; + + foreach ($urls as $url) { + $original_url = $url; + $url = ltrim($url, "./"); + $url = rtrim($url, "./"); + + // Handle relative URLs + if (parse_url($url, PHP_URL_SCHEME) === null) { + $url = $this->page_url . $url; + } + + if ($this->isResourceAccessible($url)) { + // Get the file name and local path + $file_name = basename($url); + $file_path = './' . $file_name; + $folder_path = $this->folder_location . '/' . $this->folder_name; + $urlContents = $this->downloadFile($url); + if ($urlContents) { + // Save the resource locally + $file = fopen($folder_path . '/' . $file_name, "w"); + if ($file){ + fwrite($file, $urlContents); + fclose($file); + } + // Replace the URL in the CSS content + $content = str_replace($original_url, "'" . $file_path . "'", $content); + } + } + } + } + } + function downloadSource(&$dom, $folder_path, $tagName, $attribute, $simular_pages) : void { $links = $dom->getElementsByTagName($tagName); foreach($links as $link) { @@ -197,6 +232,10 @@ class DownloadPage { // The resource is a css resource most likely // Go trough the resource, download the urls and replace them with their local path $this->handleCssUrls($sourceContent); + } elseif ($tagName == "script") { + // The resource is a script resource most likely + // Go trough the resource, download the imports and replace them with their local path + $this->handleJsImports($sourceContent); } if (count($simular_pages) != 0) { // Page is not unique so check if any other already downloaded resource is |
