Bladeren bron

tried detecting iframe url change

Tobias Simetsreiter 5 jaren geleden
bovenliggende
commit
1b140b4dfc
2 gewijzigde bestanden met toevoegingen van 12 en 5 verwijderingen
  1. 1 1
      index.html
  2. 11 4
      vers.js

+ 1 - 1
index.html

@@ -11,7 +11,7 @@
     <select name="versions" id="Versioner_selector">
     </select>
   </div>
-  <iframe id="Versioner_mainframe" src="">
+  <iframe id="Versioner_mainframe" sandbox="allow-same-origin" src="">
     Your browser doesn't support iframes
   </iframe>
 </body>

+ 11 - 4
vers.js

@@ -3,6 +3,8 @@
 function Versioner(){
 
     this.version_prefix = "v/";
+    this.default_version = "latest";
+    this.default_path = "/index.html"
 
     this.main = ()=>{
         this.main_frame = document.getElementById("Versioner_mainframe");
@@ -12,7 +14,8 @@ function Versioner(){
         this.data_script.addEventListener("load", this.loadData.bind(this));
         document.body.append(this.data_script)
 
-        this.main_frame.addEventListener("load", this.updateMainFrame.bind(this));
+        this.main_frame.addEventListener("load", this.updateMainFrame);
+        window.addEventListener("message", console.log);
 
         this.selector.addEventListener("change", ((e)=>{
             this.version = this.selector.selectedOptions[0].value;
@@ -30,13 +33,17 @@ function Versioner(){
     }
 
 
-    this.updateMainFrame = (e)=>{
-        var subp = e.target.contentWindow.location.pathname;
+    this.updateMainFrame = (loc)=>{
+        // var subp = e.target.contentWindow.location.pathname;
+        var subp = this.main_frame.contentWindow.location.pathname;
+        console.log(subp)
         if(subp.search(location.pathname)==0){
             var sub_relpath = subp.slice(location.pathname.length);
             this.path = sub_relpath.slice(this.version.length);
             this.path = this.path.slice(this.version_prefix.length)
             this.updateUri(false);
+        } else {
+            window.location.href = this.main_frame.contentWindow.location.href;
         }
     }
 
@@ -55,7 +62,7 @@ function Versioner(){
             this.path = path;
         } 
         if (!this.path){
-            this.path = "";
+            this.path = this.default_path;
         }
         var query = "?v="+ this.version +"&p=" + this.path;
         window.history.replaceState("","",query);