Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions core/src/commonMain/kotlin/dev/kdriver/core/browser/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ class Config(
"--disable-renderer-backgrounding",
"--disable-background-networking",
"--disable-dev-shm-usage",
"--disable-features=IsolateOrigins,DisableLoadExtensionCommandLineSwitch,site-per-process",
"--disable-features=IsolateOrigins,site-per-process",
"--disable-session-crashed-bubble",
"--disable-search-engine-choice-screen"
"--disable-search-engine-choice-screen",
"--enable-unsafe-extension-debugging"
)

val browserArgs: List<String>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dev.kdriver.core.browser

import dev.kdriver.cdp.domain.Target
import dev.kdriver.cdp.domain.browser
import dev.kdriver.cdp.domain.page
import dev.kdriver.cdp.domain.target
import dev.kdriver.cdp.domain.*
import dev.kdriver.core.connection.Connection
import dev.kdriver.core.connection.DefaultConnection
import dev.kdriver.core.connection.OwnedConnection
Expand Down Expand Up @@ -151,11 +148,6 @@
config.port = freePort()
}

// handle extensions if any
config.extensions.takeIf { it.isNotEmpty() }?.let {
config.addArgument("--load-extension=${it.joinToString(",")}")
}

config.lang?.let {
config.addArgument("--lang=$it")
}
Expand Down Expand Up @@ -220,6 +212,20 @@
connection.target.setDiscoverTargets(discover = true)
}

if (config.extensions.isNotEmpty()) {
logger.info("Loading ${config.extensions.size} extension(s) using CDP")
config.extensions.forEach { extensionPath ->
try {
val result = connection.extensions.loadUnpacked(extensionPath.toString())
logger.info("Successfully loaded extension from $extensionPath with ID: ${result.id}")
} catch (e: Exception) {
logger.error("Failed to load extension from $extensionPath: ${e.message}")
e.printStackTrace()
throw e
}
}
}

updateTargets()
return this
}
Expand Down Expand Up @@ -277,7 +283,7 @@
return try {
info = http.get<ContraDict>("version")
true
} catch (e: Exception) {

Check warning on line 286 in core/src/commonMain/kotlin/dev/kdriver/core/browser/DefaultBrowser.kt

View check run for this annotation

codefactor.io / CodeFactor

core/src/commonMain/kotlin/dev/kdriver/core/browser/DefaultBrowser.kt#L286

The caught exception is too generic. Prefer catching specific exceptions to the case that is currently handled. (detekt.TooGenericExceptionCaught)
logger.debug("Could not start: ${e.message}")
false
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/jvmTest/kotlin/dev/kdriver/core/Extensions.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package dev.kdriver.core

import kotlinx.io.files.Path
import java.io.File
import java.net.URI

fun sampleFile(name: String): String {
val resource = checkNotNull(Thread.currentThread().contextClassLoader.getResource(name)) {
"Resource '$name' not found"
}
return resource.toURI().toString()
}

fun samplePath(name: String): Path {
return Path(File(URI(sampleFile(name))).absolutePath)
}
36 changes: 36 additions & 0 deletions core/src/jvmTest/kotlin/dev/kdriver/core/browser/BrowserTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.kdriver.core.browser

import dev.kdriver.core.extensions.addExtension
import dev.kdriver.core.sampleFile
import dev.kdriver.core.samplePath
import dev.kdriver.core.tab.ReadyState
import kotlinx.coroutines.runBlocking
import kotlin.test.Test
Expand Down Expand Up @@ -151,4 +153,38 @@ class BrowserTest {
browser.stop()
}

@Test
fun testExtensionLoading() = runBlocking {
val config = Config(headless = true, sandbox = false)
config.addExtension(samplePath("extension"))

assertTrue(config.extensions.isNotEmpty())
assertEquals(1, config.extensions.size)

val browser = createBrowser(this, config)
assertNotNull(browser)
assertNotNull(browser.connection)

// Navigate to a test page
val tab = browser.get("https://example.com")
tab.waitForReadyState(ReadyState.COMPLETE)

// Wait for extension to inject content
tab.wait(5000)

// Verify the extension marker element exists
val markerElement = tab.select("#kdriver-extension-marker", timeout = 10000)
assertNotNull(
markerElement,
"Extension marker element should exist, indicating the extension loaded successfully"
)
assertEquals("KDriver Extension Active", markerElement.text)

// Verify the data attribute
val dataAttr = markerElement["data-extension-loaded"]
assertEquals("true", dataAttr)

browser.stop()
}

}
20 changes: 20 additions & 0 deletions core/src/jvmTest/resources/extension/content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// KDriver test extension content script
// This script adds a marker element to the page to verify the extension was loaded

(function () {
// Create a marker div that we can detect in tests
const marker = document.createElement('div');
marker.id = 'kdriver-extension-marker';
marker.setAttribute('data-extension-loaded', 'true');
marker.style.display = 'none';
marker.textContent = 'KDriver Extension Active';

// Insert the marker into the document
if (document.body) {
document.body.appendChild(marker);
} else {
document.addEventListener('DOMContentLoaded', function () {
document.body.appendChild(marker);
});
}
})();
22 changes: 22 additions & 0 deletions core/src/jvmTest/resources/extension/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"manifest_version": 3,
"name": "KDriver Test Extension",
"version": "1.0",
"description": "Test extension for verifying extension loading",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
],
"run_at": "document_end",
"match_about_blank": true,
"all_frames": true
}
],
"host_permissions": [
"<all_urls>"
]
}
Loading