mirror of
https://github.com/Qortal/qortal.git
synced 2025-04-02 10:15:52 +00:00
Send URL updates to the UI when pages are loaded.
This commit is contained in:
parent
3077810ea8
commit
5b7e9666dc
@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
|
|||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
|
import org.qortal.arbitrary.misc.Service;
|
||||||
|
|
||||||
public class HTMLParser {
|
public class HTMLParser {
|
||||||
|
|
||||||
@ -13,12 +14,21 @@ public class HTMLParser {
|
|||||||
private String linkPrefix;
|
private String linkPrefix;
|
||||||
private byte[] data;
|
private byte[] data;
|
||||||
private String qdnContext;
|
private String qdnContext;
|
||||||
|
private String resourceId;
|
||||||
|
private Service service;
|
||||||
|
private String identifier;
|
||||||
|
private String path;
|
||||||
|
|
||||||
public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data, String qdnContext) {
|
public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data,
|
||||||
|
String qdnContext, Service service, String identifier) {
|
||||||
String inPathWithoutFilename = inPath.substring(0, inPath.lastIndexOf('/'));
|
String inPathWithoutFilename = inPath.substring(0, inPath.lastIndexOf('/'));
|
||||||
this.linkPrefix = usePrefix ? String.format("%s/%s%s", prefix, resourceId, inPathWithoutFilename) : "";
|
this.linkPrefix = usePrefix ? String.format("%s/%s%s", prefix, resourceId, inPathWithoutFilename) : "";
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.qdnContext = qdnContext;
|
this.qdnContext = qdnContext;
|
||||||
|
this.resourceId = resourceId;
|
||||||
|
this.service = service;
|
||||||
|
this.identifier = identifier;
|
||||||
|
this.path = inPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addAdditionalHeaderTags() {
|
public void addAdditionalHeaderTags() {
|
||||||
@ -31,8 +41,8 @@ public class HTMLParser {
|
|||||||
String qAppsScriptElement = String.format("<script src=\"/apps/q-apps.js?time=%d\">", System.currentTimeMillis());
|
String qAppsScriptElement = String.format("<script src=\"/apps/q-apps.js?time=%d\">", System.currentTimeMillis());
|
||||||
head.get(0).prepend(qAppsScriptElement);
|
head.get(0).prepend(qAppsScriptElement);
|
||||||
|
|
||||||
// Add QDN context var
|
// Add vars
|
||||||
String qdnContextVar = String.format("<script>var qdnContext=\"%s\";</script>", this.qdnContext);
|
String qdnContextVar = String.format("<script>var qdnContext=\"%s\"; var qdnService=\"%s\"; var qdnName=\"%s\"; var qdnIdentifier=\"%s\"; var qdnPath=\"%s\";</script>", this.qdnContext, this.service.toString(), this.resourceId, this.identifier, this.path);
|
||||||
head.get(0).prepend(qdnContextVar);
|
head.get(0).prepend(qdnContextVar);
|
||||||
|
|
||||||
// Add base href tag
|
// Add base href tag
|
||||||
|
@ -129,7 +129,7 @@ public class ArbitraryDataRenderer {
|
|||||||
if (HTMLParser.isHtmlFile(filename)) {
|
if (HTMLParser.isHtmlFile(filename)) {
|
||||||
// HTML file - needs to be parsed
|
// HTML file - needs to be parsed
|
||||||
byte[] data = Files.readAllBytes(Paths.get(filePath)); // TODO: limit file size that can be read into memory
|
byte[] data = Files.readAllBytes(Paths.get(filePath)); // TODO: limit file size that can be read into memory
|
||||||
HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data, qdnContext);
|
HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data, qdnContext, service, identifier);
|
||||||
htmlParser.addAdditionalHeaderTags();
|
htmlParser.addAdditionalHeaderTags();
|
||||||
response.addHeader("Content-Security-Policy", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src 'self' blob:; img-src 'self' data: blob:;");
|
response.addHeader("Content-Security-Policy", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src 'self' blob:; img-src 'self' data: blob:;");
|
||||||
response.setContentType(context.getMimeType(filename));
|
response.setContentType(context.getMimeType(filename));
|
||||||
|
@ -10,7 +10,7 @@ function handleResponse(event, response) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle emmpty or missing responses
|
// Handle empty or missing responses
|
||||||
if (response == null || response.length == 0) {
|
if (response == null || response.length == 0) {
|
||||||
response = "{\"error\": \"Empty response\"}"
|
response = "{\"error\": \"Empty response\"}"
|
||||||
}
|
}
|
||||||
@ -151,7 +151,6 @@ window.addEventListener("message", (event) => {
|
|||||||
case "LINK_TO_QDN_RESOURCE":
|
case "LINK_TO_QDN_RESOURCE":
|
||||||
if (data.service == null) data.service = "WEBSITE"; // Default to WEBSITE
|
if (data.service == null) data.service = "WEBSITE"; // Default to WEBSITE
|
||||||
window.location = buildResourceUrl(data.service, data.name, data.identifier, data.path);
|
window.location = buildResourceUrl(data.service, data.name, data.identifier, data.path);
|
||||||
response = true;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "SEARCH_QDN_RESOURCES":
|
case "SEARCH_QDN_RESOURCES":
|
||||||
@ -279,6 +278,13 @@ window.addEventListener("message", (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (response == null) {
|
||||||
|
// Pass to parent (UI), in case they can fulfil this request
|
||||||
|
event.data.requestedHandler = "UI";
|
||||||
|
parent.postMessage(event.data, '*', [event.ports[0]]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
handleResponse(event, response);
|
handleResponse(event, response);
|
||||||
|
|
||||||
}, false);
|
}, false);
|
||||||
@ -317,6 +323,19 @@ else if (document.attachEvent) {
|
|||||||
document.attachEvent('onclick', interceptClickEvent);
|
document.attachEvent('onclick', interceptClickEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send current page details to UI
|
||||||
|
*/
|
||||||
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
qortalRequest({
|
||||||
|
action: "QDN_RESOURCE_DISPLAYED",
|
||||||
|
service: qdnService,
|
||||||
|
name: qdnName,
|
||||||
|
identifier: qdnIdentifier,
|
||||||
|
path: qdnPath
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Intercept image loads from the DOM
|
* Intercept image loads from the DOM
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user