Skip to content

Commit

Permalink
Update example on setting a proxy (#1)
Browse files Browse the repository at this point in the history
* Update example on setting a proxy

* send setProxy message in example
  • Loading branch information
alxgrk authored Nov 2, 2020
1 parent 41e9d88 commit 2ab805a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ object ChromeExtensionUtility {

private const val EXTENSION_ID = "nmamelejkbhljllfalieeoedmnfolplf"

// sent message will be received by the extensions index.js
private const val INITIALIZE_EXTENSION = """
var doneCallback = arguments[arguments.length - 1]; // this is injected by chrome runtime
chrome.runtime.sendMessage("$EXTENSION_ID",
{extension: "init"}, // request object
{}, // sender object
function(a) { doneCallback(a); } // sendResponse callback
);"""

internal fun Array<out String>.copyTo(targetDir: Path) =
map { file ->
ChromeExtensionUtility::class.java.classLoader.getResourceAsStream(file)!!
Expand All @@ -79,8 +70,21 @@ object ChromeExtensionUtility {
val alreadyEnabled = isExtensionInitialized[this]
if (alreadyEnabled == null || !alreadyEnabled) {
isExtensionInitialized[this] = true
return this.executeAsyncScript(INITIALIZE_EXTENSION)
return sendAsyncMessage("{extension: \"init\"}")
}
return null
}
}

/**
* Sends a message to the extension asynchronously.
*/
fun JavascriptExecutor.sendAsyncMessage(requestObject: String) = executeAsyncScript(
"""
var doneCallback = arguments[arguments.length - 1]; // this is injected by chrome runtime
chrome.runtime.sendMessage("$EXTENSION_ID",
$requestObject,
{},
function(a) { doneCallback(a); } // sendResponse callback
);
""")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package de.alxgrk

import de.alxgrk.spring_selenium_pool.ChromeExtensionUtility.initializeExtension
import de.alxgrk.spring_selenium_pool.ChromeExtensionUtility.sendAsyncMessage
import de.alxgrk.spring_selenium_pool.ChromeProfile
import de.alxgrk.spring_selenium_pool.WebDriverPool
import org.openqa.selenium.By
Expand Down Expand Up @@ -45,6 +46,19 @@ class ExampleApplication {
😎😎😎
""".trimIndent())

// set proxy
val proxyIp = "127.0.0.1"
val proxyPort = 12345
(this as JavascriptExecutor).sendAsyncMessage("""
{
extension: "setProxy",
proxyJson: {
ip: "$proxyIp",
port: $proxyPort
}
}
""")
}
}
}
}
13 changes: 12 additions & 1 deletion spring-selenium-pool-example/src/main/resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@ chrome.runtime.onMessageExternal.addListener(
// do whatever you want to
sendResponse("extension responded")
}
if (request && request.extension === "setProxy") {
var proxy = request.proxyJson
var config =
{
mode: "fixed_servers",
rules: {singleProxy: {port: proxy.port, scheme: "http", host: proxy.ip}, bypassList: ["localhost"]}
};
chrome.proxy.settings.set({value: config, scope: 'regular'}, function () {
sendResponse()
});
}
}
);
);

0 comments on commit 2ab805a

Please sign in to comment.