Set hint css vie web content manager #349.
authorDaniel Carl <danielcarl@gmx.de>
Sun, 7 May 2017 12:21:27 +0000 (14:21 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Sun, 7 May 2017 12:32:30 +0000 (14:32 +0200)
Add the hint css direct to webkit instead of manipulating the DOM to add
a style node. By the way the default style was removed from config.def.h
into src/scripts directory so that the css is minified during
compilation.

src/Makefile
src/config.def.h
src/scripts/hints.css [new file with mode: 0644]
src/scripts/hints.js
src/scripts/js2h.sh
src/setting.c

index 50e267d..f60d2ce 100644 (file)
@@ -4,6 +4,7 @@ include $(BASEDIR)/config.mk
 SUBDIRS  = webextension
 OBJ      = $(patsubst %.c, %.o, $(wildcard *.c))
 JSFILES  = $(wildcard scripts/*.js)
+CSSFILES = $(wildcard scripts/*.css)
 
 all: vimb $(SUBDIRS:%=%.subdir-all)
 
@@ -22,10 +23,10 @@ config.h:
        @echo create $@ from config.def.h
        $(Q)cp config.def.h $@
 
-scripts/scripts.h: $(JSFILES)
+scripts/scripts.h: $(JSFILES) $(CSSFILES)
        $(Q)$(RM) $@
-       @echo "create $@ from *.js"
-       $(Q)for file in $(JSFILES); do \
+       @echo "create $@ from *.{css,js}"
+       $(Q)for file in $(JSFILES) $(CSSFILES); do \
                ./scripts/js2h.sh $$file >> $@; \
        done
 
index 0e82137..44f9351 100644 (file)
 #define SETTING_HOME_PAGE                     "about:blank"
 
 #define MAXIMUM_HINTS              500
-
-/* CSS style use on creating hints. This might also be averrules by css out of
- * $XDG_CONFIG_HOME/vimb/style.css file. */
-#define HINT_CSS "#_hintContainer{\
-position:static\
-}\
-._hintLabel{\
--webkit-transform:translate(-4px,-4px);\
-position:absolute;\
-z-index:100000;\
-font:bold .8em monospace;\
-color:#000;\
-background-color:#fff;\
-margin:0;\
-padding:0px 1px;\
-border:1px solid #444;\
-opacity:0.7\
-}\
-._hintElem{\
-background-color:#ff0 !important;\
-color:#000 !important;\
-transition:all 0 !important;\
-transition-delay:all 0 !important\
-}\
-._hintElem._hintFocus{\
-background-color:#8f0 !important\
-}\
-._hintLabel._hintFocus{\
-z-index:100001;\
-opacity:1\
-}"
-
diff --git a/src/scripts/hints.css b/src/scripts/hints.css
new file mode 100644 (file)
index 0000000..c061121
--- /dev/null
@@ -0,0 +1,28 @@
+#_hintContainer{
+    position:static
+}
+._hintLabel{
+    -webkit-transform:translate(-4px,-4px);
+    background-color:#fff;
+    border:1px solid #444;
+    color:#000;
+    font:bold .8em monospace;
+    margin:0;
+    opacity:0.7;
+    padding:0px 1px;
+    position:absolute;
+    z-index:100000
+}
+._hintElem{
+    background-color:#ff0 !important;
+    color:#000 !important;
+    transition-delay:all 0 !important;
+    transition:all 0 !important
+}
+._hintElem._hintFocus{
+    background-color:#8f0 !important
+}
+._hintLabel._hintFocus{
+    opacity:1;
+    z-index:10000
+}
index 80c5df2..eeef5b7 100644 (file)
@@ -197,9 +197,6 @@ var hints = Object.freeze((function(){
             if (doc.body) {
                 doc.body.appendChild(hDiv);
             }
-            /* create the default style sheet */
-            createStyle(doc);
-
             docs.push({
                 doc:   doc,
                 start: start,
@@ -318,18 +315,6 @@ var hints = Object.freeze((function(){
         return [doc.defaultView.scrollX, doc.defaultView.scrollY];
     }
 
-    function createStyle(doc) {
-        if (doc.hasStyle) {
-            return;
-        }
-        var e = doc.createElement("style");
-        /* HINT_CSS is replaces by the contents of the HINT_CSS constant from config.h */
-        e.innerHTML = "HINT_CSS";
-        doc.head.appendChild(e);
-        /* prevent us from adding the style multiple times */
-        doc.hasStyle = true;
-    }
-
     function focus(back) {
         var idx = validHints.indexOf(activeHint);
         /* previous active hint not found */
index 4a50c12..ce38d9d 100755 (executable)
@@ -13,13 +13,13 @@ if [ ! -r "$FILE" ]; then
     exit 1
 fi
 
-# Remove the path and .js file extension and turn all chars to upper case to
-# get the constant name.
-CONSTANT=$(echo "$FILE" | sed 's:.*/::g' | sed 's:^\(.*\).\(js\):\2_\1:g' | tr a-z A-Z)
+# Put file extension and _ before file name, turn all to upper case to get the
+# constant name.
+CONSTANT=$(echo "$FILE" | sed 's:.*/::g' | sed 's:^\(.*\).\(css\|js\):\2_\1:g' | tr a-z A-Z)
 
 # minify the script
 cat $FILE | \
-# removove single line comments
+# remove single line comments
 sed -e 's|^//.*$||g' | \
 # remove linebreaks
 tr '\n\r' ' ' | \
@@ -32,8 +32,7 @@ sed -e 's:/\*[^*]*\*/::g' \
 # ecaspe
 sed -e 's|\\x20| |g' \
     -e 's|\\|\\\\|g' \
-    -e 's|"|\\"|g' \
-    -e 's|HINT_CSS|" HINT_CSS "|' | \
+    -e 's|"|\\"|g' | \
 # write opener with the starting and ending quote char
 sed -e "1s/^/#define $CONSTANT \"/" \
     -e '$s/$/"\n/'
index 0df1881..54e9b18 100644 (file)
@@ -657,6 +657,14 @@ static int user_style(Client *c, const char *name, DataType type, void *value, v
         webkit_user_content_manager_remove_all_style_sheets(ucm);
     }
 
+    /* Inject the global styles with author level to allow restyling by user
+     * style sheets. */
+    style = webkit_user_style_sheet_new(CSS_HINTS,
+            WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
+            WEBKIT_USER_STYLE_LEVEL_AUTHOR, NULL, NULL);
+    webkit_user_content_manager_add_style_sheet(ucm, style);
+    webkit_user_style_sheet_unref(style);
+
     return CMD_SUCCESS;
 }