Skip to content

Commit

Permalink
feat: add built-in scheme handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tishion committed Oct 9, 2024
1 parent 44761e7 commit 8d49d2d
Show file tree
Hide file tree
Showing 40 changed files with 329 additions and 257 deletions.
2 changes: 1 addition & 1 deletion cmake/CefViewCoreConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include(FetchContent)
FetchContent_Declare(
CefViewCore
GIT_REPOSITORY https://github.com/CefView/CefViewCore.git
GIT_TAG 6bb08e71d610671f2b2c0080fb61bcf534e4dcb2
GIT_TAG 1cc9a2a1085520bc31609c623ae338e699498bb7
)

# set CEF version to be used
Expand Down
12 changes: 12 additions & 0 deletions example/QCefViewTest/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ MainWindow::createLeftCefView()
connect(m_pLeftCefViewWidget, &QCefView::invokeMethod, this, &MainWindow::onInvokeMethod);

// connect the cefQueryRequest to the slot
connect(m_pLeftCefViewWidget, &QCefView::cefUrlRequest, this, &MainWindow::onQCefUrlRequest);
connect(m_pLeftCefViewWidget, &QCefView::cefQueryRequest, this, &MainWindow::onQCefQueryRequest);
connect(m_pLeftCefViewWidget, &QCefView::reportJavascriptResult, this, &MainWindow::onJavascriptResult);
connect(m_pLeftCefViewWidget, &QCefView::loadStart, this, &MainWindow::onLoadStart);
Expand Down Expand Up @@ -162,6 +163,17 @@ MainWindow::onInvokeMethod(const QCefBrowserId& browserId,
}
}

void
MainWindow::onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url)
{
QString title("QCef URL Request");
QString text = QString("Current Thread: QT_UI\r\n"
"URL: %1")
.arg(url);

QMessageBox::information(this->window(), title, text);
}

void
MainWindow::onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query)
{
Expand Down
2 changes: 2 additions & 0 deletions example/QCefViewTest/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ protected slots:
const QString& method,
const QVariantList& arguments);

void onQCefUrlRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QString& url);

void onQCefQueryRequest(const QCefBrowserId& browserId, const QCefFrameId& frameId, const QCefQuery& query);

void onJavascriptResult(const QCefBrowserId& browserId,
Expand Down
244 changes: 125 additions & 119 deletions example/QCefViewTest/index.in.html
Original file line number Diff line number Diff line change
@@ -1,129 +1,135 @@
<html>

<head>
<script>
console.log("page script starts.....");

window.onwheel = e => console.log(`wheel event: altKey=${e.altKey} ctrlKey=${e.ctrlKey}`);

function onLoad() {
if (typeof CallBridge == "undefined") {
alert("Not in CefView context");
return;
}
}

function onChangeColorEvent(color) {
console.log("request change background color to: ", color);
// change the background color
document.getElementById("main").style.backgroundColor = color;
}

function onAddEventListener() {
// Add a event listener to handle the event named 'colorChange'
CallBridge.addEventListener(
// event name
"colorChange",
// event handler
onChangeColorEvent
);
}

function onRemoveEventListener() {
// Add a event listener to handle the event named 'colorChange'
CallBridge.removeEventListener(
// event name
"colorChange",
// event handler
onChangeColorEvent
);
}

function onInvokeMethodClicked(name, ...arg) {
CallBridge.invokeMethod(name, ...arg);
}

function onCallBridgeQueryClicked() {
var query = {
request: document.getElementById("message").value,
onSuccess: function (response) {
alert(response);
},
onFailure: function (error_code, error_message) {
alert(error_message);
},
};
window.CefViewQuery(query);
}

function testInvokeMethod() {
let d = {
d1: true,
d2: 5678,
d3: "test object",
d4: [1, "2", false],
d5: {
d1: true,
d2: 5678,
d3: "nested object",
d4: [1, "2", true],
},
};
onInvokeMethodClicked("TestMethod", 1, false, "arg3", d);
}
</script>
<style>
.noselect {
user-select: none;
/* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
}
</style>
<script>
console.log("page script starts.....");

window.onwheel = e => console.log(`wheel event: altKey=${e.altKey} ctrlKey=${e.ctrlKey}`);

function onLoad() {
if (typeof CallBridge == "undefined") {
alert("Not in CefView context");
return;
}
}

function onChangeColorEvent(color) {
console.log("request change background color to: ", color);
// change the background color
document.getElementById("main").style.backgroundColor = color;
}

function onAddEventListener() {
// Add a event listener to handle the event named 'colorChange'
CallBridge.addEventListener(
// event name
"colorChange",
// event handler
onChangeColorEvent
);
}

function onRemoveEventListener() {
// Add a event listener to handle the event named 'colorChange'
CallBridge.removeEventListener(
// event name
"colorChange",
// event handler
onChangeColorEvent
);
}

function onInvokeMethodClicked(name, ...arg) {
CallBridge.invokeMethod(name, ...arg);
}

function onCallBridgeQueryClicked() {
var query = {
request: document.getElementById("message").value,
onSuccess: function (response) {
alert(response);
},
onFailure: function (error_code, error_message) {
alert(error_message);
},
};
window.CefViewQuery(query);
}

function testInvokeMethod() {
let d = {
d1: true,
d2: 5678,
d3: "test object",
d4: [1, "2", false],
d5: {
d1: true,
d2: 5678,
d3: "nested object",
d4: [1, "2", true],
},
};
onInvokeMethodClicked("TestMethod", 1, false, "arg3", d);
}
</script>
<style>
.noselect {
user-select: none;
/* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
}
</style>
</head>

<body onload="onLoad()" id="main" class="noselect">
<h1 align="center" style="font-size: 12pt">Web Area</h1>
<div align="center">
<div style="background-color: green; -webkit-app-region: drag">
<h1>Dragging area</h1>
<h1 align="center" style="font-size: 12pt">Web Area</h1>
<div align="center">
<div style="background-color: green; -webkit-app-region: drag">
<h1>Dragging area</h1>
</div>
<br />

<label> Test Case for Built-in Scheme Handler </label>
<br />
<a href="cefview://custom.scheme.handler/api/example?arg1=1&args=hello">Built-in Scheme Handler (CefView://)</a>
<br />
<br />

<label> Test Case for EventListener</label>
<br />
<input type="button" value="AddEventListener" onclick="onAddEventListener()" />
<input type="button" value="RemoveEventListener" onclick="onRemoveEventListener()" />
<br />
<br />

<label> Test Case for InvokeMethod </label>
<br />
<input type="button" value="Invoke Method" onclick="testInvokeMethod()" />
<br />
<br />

<label> Test Case for QCefQuery </label>
<br />
<textarea id="message" style="width: 320px; height: 120px">this message will be processed by native code.</textarea>
<br />
<input type="button" value="Query" onclick="onCallBridgeQueryClicked()" />
<br />
<br />

<label> Test Case for Popup Browser </label>
<br />
<a href="#" target="_blank">Popup Browser By HTML</a>
<br />
<a href="#" onClick="window.open('#','QCefView Popup','width=800, height=600'); return false;">
Popup Browser By Script
</a>

<br />
<a href="#" onClick="window.close();">Close Current Window</a>


<p>An iframe with default borders:</p>
<iframe src="/tutorial.html" width="100%" height="300"> </iframe>
</div>
<br />

<label> Test Case for EventListener</label>
<br />
<input type="button" value="AddEventListener" onclick="onAddEventListener()" />
<input type="button" value="RemoveEventListener" onclick="onRemoveEventListener()" />
<br />
<br />

<label> Test Case for InvokeMethod </label>
<br />
<input type="button" value="Invoke Method" onclick="testInvokeMethod()" />
<br />
<br />

<label> Test Case for QCefQuery </label>
<br />
<textarea id="message" style="width: 320px; height: 120px">
this message will be processed by native code.</textarea>
<br />
<input type="button" value="Query" onclick="onCallBridgeQueryClicked()" />
<br />
<br />

<label> Test Case for Popup Browser </label>
<br />
<a href="#" target="_blank">Popup Browser By HTML</a>
<br />
<a href="#" onClick="window.open('#','QCefView Popup','width=800, height=600'); return false;">Popup Browser By
Script</a>

<br />
<a href="#" onClick="window.close();">Close Current Window</a>


<p>An iframe with default borders:</p>
<iframe src="/tutorial.html" width="100%" height="300"> </iframe>
</div>
</body>

</html>
4 changes: 3 additions & 1 deletion example/QCefViewTest/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ main(int argc, char* argv[])
config.setUserAgent("QCefViewTest");
// set log level
config.setLogLevel(QCefConfig::LOGSEVERITY_DEFAULT);
// set JSBridge object name (default value is QCefViewClient)
// set JSBridge object name (default value is CefViewClient)
config.setBridgeObjectName("CallBridge");
// set Built-in scheme name (default value is CefView)
config.setBuiltinSchemeName("CefView");
// port for remote debugging (default is 0 and means to disable remote debugging)
config.setRemoteDebuggingPort(9000);
// set background color for all browsers
Expand Down
Loading

0 comments on commit 8d49d2d

Please sign in to comment.