From 3571f5849ddeb2b87a7906e87ff5c4510f3dacb8 Mon Sep 17 00:00:00 2001
From: adbenitez <asieldbenitez@gmail.com>
Date: Fri, 31 Dec 2021 04:26:06 -0500
Subject: [PATCH] set unique selfAddr per device/window and use that data in
 the example index.html

---
 deltachat.js | 28 +++++++++++++++++++---------
 index.html   |  6 +++---
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/deltachat.js b/deltachat.js
index c5564c7..9f32022 100644
--- a/deltachat.js
+++ b/deltachat.js
@@ -5,7 +5,7 @@ window.deltachat = (() => {
     var updateListener = () => {};
 
     return {
-        selfAddr: () => "foo@bar.dex",
+        selfAddr: () => window.xdcSelfAddr || "device0@local.host",
         setUpdateListener: (cb) => (window.xdcUpdateListener = cb),
         getAllUpdates: () => {
             return JSON.parse(
@@ -47,16 +47,26 @@ function addPeer() {
     var xdcChild = window.open(window.location);
     var xdcRoot = getXdcRoot();
     xdcChild.xdcRoot = xdcRoot;
+    xdcChild.xdcSelfAddr = "device" + xdcRoot.allXdcWindows.length + "@local.host";
     xdcRoot.allXdcWindows.push(xdcChild);
 }
 
-function alterBody() {
-    var div = document.createElement('div');
-    div.innerHTML =
-        '<div style="' + styleControlPanel + '">' +
-        '<a href="javascript:addPeer();" style="' + styleMenuLink + '">Add Peer</a>' +
-        '<div>';
-    document.getElementsByTagName('body')[0].append(div.firstChild);
+function alterApp() {
+    var title = document.getElementsByTagName('title')[0];
+    if (typeof title == 'undefined') {
+	title = document.createElement('title');
+	document.getElementsByTagName('head')[0].append(title);
+    }
+    title.innerText = window.deltachat.selfAddr();
+
+    if (getXdcRoot() == window) {
+	var div = document.createElement('div');
+	div.innerHTML =
+            '<div style="' + styleControlPanel + '">' +
+            '<a href="javascript:addPeer();" style="' + styleMenuLink + '">Add Peer</a>' +
+            '<div>';
+	document.getElementsByTagName('body')[0].append(div.firstChild);
+    }
 }
 
-window.addEventListener("load", alterBody);
+window.addEventListener("load", alterApp);
diff --git a/index.html b/index.html
index cb78510..d6e1b81 100644
--- a/index.html
+++ b/index.html
@@ -12,15 +12,15 @@
 
     function sendMsg() {
         msg = document.getElementById("input").value;
-        window.deltachat.sendUpdate('someone typed "'+msg+'"', msg);
+        window.deltachat.sendUpdate('someone typed "'+msg+'"', {"addr": window.deltachat.selfAddr(), "msg": msg});
     }
 
     function receiveUpdate(update) {
-        document.getElementById('output').innerHTML += update.payload + "<br>";
+        document.getElementById('output').innerHTML += "<strong>&lt;" + update.payload.addr + "&gt;</strong> " + update.payload.msg + "<br>";
     }
 
     window.deltachat.setUpdateListener(receiveUpdate);
 
 </script>
 </body>
-</html>
\ No newline at end of file
+</html>