MCPcopy Create free account
hub / github.com/KangLin/RabbitRemoteControl / InjectScriptQWebChannel

Method InjectScriptQWebChannel

Plugins/WebBrowser/FrmWebView.cpp:692–721  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

690}
691
692void CFrmWebView::InjectScriptQWebChannel()
693{
694 Q_ASSERT(page());
695
696 // 从资源载入 qwebchannel.js 和 autofill.js
697 // 你需要在 resources.qrc 中把 js/qwebchannel.js 和 js/autofill.js 注册为资源
698 auto loadResource = [](const QString &rcPath)->QString{
699 QFile f(rcPath);
700 if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
701 qCritical(log) << "Cannot open resource" << rcPath;
702 return QString();
703 }
704 return QString::fromUtf8(f.readAll());
705 };
706
707 QString qwebchannelJs = loadResource(":/js/QWebChannel.js");
708 if (qwebchannelJs.isEmpty()) {
709 qCritical(log) << "QWebChannel.js not found in resources!";
710 return;
711 }
712
713 // 先注入 QWebChannel.js,再注入 AutoFill 脚本。将两者合并到一个 QWebEngineScript 也可以。
714 QWebEngineScript channelScript;
715 channelScript.setName("qwebchannel.js");
716 channelScript.setInjectionPoint(QWebEngineScript::DocumentCreation);
717 channelScript.setRunsOnSubFrames(true);
718 channelScript.setWorldId(QWebEngineScript::MainWorld);
719 channelScript.setSourceCode(qwebchannelJs);
720 page()->scripts().insert(channelScript);
721}
722
723void CFrmWebView::InjectScriptAutoFill()
724{

Callers

nothing calls this directly

Calls 3

setNameMethod · 0.80
openMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected