! \brief Create a Web Engine Profile object Exposing C++ APIs in this way makes it impossible for JavaScript code to predict when APIs \a objects will be created. Therefore we prefer to let the JavaScript code create the APIs objects itself. This function is only used to run some of the WizNote web services. */
| 63 | used to run some of the WizNote web services. |
| 64 | */ |
| 65 | QWebEngineProfile* createWebEngineProfile(const WizWebEngineInjectObjectCollection& objects, QObject* parent) |
| 66 | { |
| 67 | if (objects.empty()) |
| 68 | return nullptr; |
| 69 | |
| 70 | // Create a new profile |
| 71 | QWebEngineProfile *profile = new QWebEngineProfile("WizNoteWebEngineProfile", parent); |
| 72 | |
| 73 | // Load qtwebchannel library |
| 74 | QString jsWebChannelFileName = Utils::WizPathResolve::resourcesPath() + "files/webengine/wizwebchannel.js"; |
| 75 | QString jsWebChannel; |
| 76 | WizLoadUnicodeTextFromFile(jsWebChannelFileName, jsWebChannel); |
| 77 | |
| 78 | // Load javascript initialization script |
| 79 | QString initFileName = Utils::WizPathResolve::resourcesPath() + "files/webengine/wizwebengineviewinit.js"; |
| 80 | QString jsInit; |
| 81 | WizLoadUnicodeTextFromFile(initFileName, jsInit); |
| 82 | |
| 83 | // Get all names of published objects |
| 84 | CWizStdStringArray names; |
| 85 | WizWebEngineInjectObjectCollection::const_iterator inject = objects.constBegin(); |
| 86 | while (inject != objects.constEnd()) { |
| 87 | names.push_back("\"" + inject.key() + "\""); |
| 88 | ++inject; |
| 89 | } |
| 90 | |
| 91 | // Generate js scripts for these objects |
| 92 | CString objectNames; |
| 93 | WizStringArrayToText(names, objectNames, ", "); |
| 94 | jsInit.replace("__objectNames__", objectNames); |
| 95 | |
| 96 | // Combine scripts |
| 97 | QString jsAll = jsWebChannel + "\n" + jsInit; |
| 98 | |
| 99 | { |
| 100 | QWebEngineScript script; |
| 101 | script.setSourceCode(jsAll); |
| 102 | script.setName("webchannel.js"); |
| 103 | script.setWorldId(QWebEngineScript::MainWorld); |
| 104 | script.setInjectionPoint(QWebEngineScript::DocumentCreation); |
| 105 | script.setRunsOnSubFrames(false); // if set True, it will cause some error in javascript. |
| 106 | profile->scripts()->insert(script); |
| 107 | } |
| 108 | |
| 109 | insertScrollbarStyleSheet(profile); |
| 110 | |
| 111 | QObject::connect(profile, &QWebEngineProfile::downloadRequested, |
| 112 | &DownloadManagerWidget::instance(), &DownloadManagerWidget::downloadRequested); |
| 113 | |
| 114 | return profile; |
| 115 | } |
| 116 | |
| 117 | void insertStyleSheet(QWebEngineProfile *profile, const QString &name, const QString &source) |
| 118 | { |
no test coverage detected