From 0fc98fe3c55558a71b25dd2517c7a0ee440aeed3 Mon Sep 17 00:00:00 2001
From: Daniel Carl <danielcarl@gmx.de>
Date: Thu, 22 Jun 2017 00:43:19 +0200
Subject: [PATCH] Enable JavaScript as long the hinting is done #421.

As another benefit the scroll and resize observer works so that the
hints are redrawn when the window is resized or the top window is
scrolled.
---
 src/hints.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/hints.c b/src/hints.c
index 13ca0af..70c9a12 100644
--- a/src/hints.c
+++ b/src/hints.c
@@ -38,6 +38,7 @@ static struct {
     /* holds the setting if JavaScript can open windows automatically that we
      * have to change to open windows via hinting */
     gboolean       allow_open_win;
+    gboolean       allow_javascript;
     guint          timeout_id;
 } hints;
 
@@ -90,10 +91,13 @@ void hints_clear(Client *c)
         call_hints_function(c, "clear", "true");
 
         /* if open window was not allowed for JavaScript, restore this */
+        WebKitSettings *setting = webkit_web_view_get_settings(c->webview);
         if (!hints.allow_open_win) {
-            WebKitSettings *setting = webkit_web_view_get_settings(c->webview);
             g_object_set(G_OBJECT(setting), "javascript-can-open-windows-automatically", hints.allow_open_win, NULL);
         }
+        if (!hints.allow_javascript) {
+            g_object_set(G_OBJECT(setting), "enable-javascript", hints.allow_javascript, NULL);
+        }
     }
 }
 
@@ -117,12 +121,22 @@ void hints_create(Client *c, const char *input)
 
         /* before we enable JavaScript to open new windows, we save the actual
          * value to be able restore it after hints where fired */
-        g_object_get(G_OBJECT(setting), "javascript-can-open-windows-automatically", &(hints.allow_open_win), NULL);
+        g_object_get(G_OBJECT(setting),
+                "javascript-can-open-windows-automatically", &(hints.allow_open_win),
+                "enable-javascript", &(hints.allow_javascript),
+                NULL);
 
         /* if window open is already allowed there's no need to allow it again */
         if (!hints.allow_open_win) {
             g_object_set(G_OBJECT(setting), "javascript-can-open-windows-automatically", true, NULL);
         }
+        /* TODO This might be a security issue to toggle JavaScript
+         * temporarily on. */
+        /* This is a hack to allow window.setTimeout() and scroll observers in
+         * hinting script which does not work when JavaScript is disabled. */
+        if (!hints.allow_javascript) {
+            g_object_set(G_OBJECT(setting), "enable-javascript", true, NULL);
+        }
 
         hints.promptlen = hints.gmode ? 3 : 2;
 
-- 
2.20.1