JXBrowser加载本地HTML后无法访问JavaScript库问题咨询
Hey there! Let's work through your Cytoscape + JxBrowser issue step by step.
First, let's fix the executeJavaScript parameter confusion
When you call event.getBrowser().executeJavaScript("cytoscape.js"), you're passing a filename instead of valid JavaScript code — that's a common mix-up! The executeJavaScript method expects actual executable JS logic, not the name of the library file. For example, if you want to initialize Cytoscape, you'd pass code like:
event.getBrowser().executeJavaScript("var cy = cytoscape({ container: '#cy' });");
The library itself should already be loaded in your HTML via a <script> tag, so you don't need to reference the filename in this call.
Why you're seeing 'cytoscape is not defined'
Let's break down the likely causes:
Incorrect library path in your HTML
Double-check that your<script>tag for Cytoscape points to the right file. If it's a local file, use a relative path that resolves correctly from your HTML's location. For example:<script src="./cytoscape.min.js"></script>If the path is wrong, the library never loads, hence the undefined error.
Loading timing issues
WhileinvokeAndWaitFinishLoadingMainFramewaits for the main frame to load, it doesn't guarantee that all async scripts (like those withasync/deferattributes) have finished loading. Instead, listen for theFinishLoadingEventon the main frame, or even better, wrap your Cytoscape logic in the page'swindow.onloadevent to ensure all resources are ready before executing code.Local file security restrictions
JxBrowser is built on Chromium, which has strict security rules forfile://URLs. Sometimes loading scripts from local files can hit permission blocks. Try serving your HTML and Cytoscape files via a simple local HTTP server (likepython -m http.serverif you have Python installed) and load the page usinghttp://localhost:8000/index.htmlinstead of afile://URL. This often resolves local file loading issues.
Does JxBrowser support Cytoscape?
Absolutely! Since JxBrowser uses the Chromium engine, any JavaScript library that works in Google Chrome will work in JxBrowser. The problem here isn't lack of support — it's almost certainly related to how you're loading the library or timing your JS execution.
Quick example of a working setup
Here's a simplified, correct implementation:
Your index.html
<!DOCTYPE html> <html> <head> <title>Cytoscape Test</title> <script src="./cytoscape.min.js"></script> <style> #cy { width: 100%; height: 400px; border: 1px solid #ccc; } </style> </head> <body> <div id="cy"></div> </body> </html>
Your JxBrowser code
Browser browser = new Browser(); BrowserView view = new BrowserView(browser); // Load the local HTML file correctly File htmlFile = new File("/path/to/your/index.html"); browser.loadURL(htmlFile.toURI().toString()); // Wait for the main frame to fully load all resources browser.addLoadListener(new LoadListener() { @Override public void onFinishLoadingFrame(FinishLoadingEvent event) { if (event.isMainFrame()) { // Execute valid Cytoscape initialization code event.getBrowser().executeJavaScript("var cy = cytoscape({ " + "container: '#cy', " + "elements: [" + "{ data: { id: 'node1' } }, " + "{ data: { id: 'node2' } }, " + "{ data: { id: 'edge1', source: 'node1', target: 'node2' } }" + "] " + "});"); } } // Implement other required methods (can leave empty) @Override public void onStartLoadingFrame(StartLoadingEvent event) {} @Override public void onFailLoadingFrame(FailLoadingEvent event) {} @Override public void onDocumentLoadedInFrame(DocumentLoadedInFrameEvent event) {} });
Give these fixes a try — I bet one of them will resolve your undefined error!
内容的提问来源于stack exchange,提问作者Jack Onslow

