patch-surf.c - pkgsrc-localpatches - leot's pkgsrc LOCALPATCHES
 (HTM) hg clone https://bitbucket.org/iamleot/pkgsrc-localpatches
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       patch-surf.c
       ---
            1 $NetBSD$
            2 
            3 Add support for UserAgent, UserScript and clickplumb() and try to
            4 address scrollv() and scrollh() negative values.
            5 
            6 --- surf.c.orig 2019-06-12 11:50:12.000000000 +0000
            7 +++ surf.c
            8 @@ -80,6 +80,8 @@ typedef enum {
            9         SpellLanguages,
           10         StrictTLS,
           11         Style,
           12 +       UserAgent,
           13 +       UserScript,
           14         WebGL,
           15         ZoomLevel,
           16         ParameterLast
           17 @@ -164,7 +166,9 @@ static void seturiparameters(Client *c, 
           18  static void setparameter(Client *c, int refresh, ParamName p, const Arg *a);
           19  static const char *getcert(const char *uri);
           20  static void setcert(Client *c, const char *file);
           21 +static const char *getscript(const char *uri);
           22  static const char *getstyle(const char *uri);
           23 +static void setscript(Client *c, const char *file);
           24  static void setstyle(Client *c, const char *file);
           25  static void runscript(Client *c);
           26  static void evalscript(Client *c, const char *jsstr, ...);
           27 @@ -236,6 +240,7 @@ static void find(Client *c, const Arg *a
           28  static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
           29  static void clicknewwindow(Client *c, const Arg *a, WebKitHitTestResult *h);
           30  static void clickexternplayer(Client *c, const Arg *a, WebKitHitTestResult *h);
           31 +static void clickplumb(Client *c, const Arg *a, WebKitHitTestResult *h);
           32  
           33  static char winid[64];
           34  static char togglestats[12];
           35 @@ -265,6 +270,8 @@ static ParamName loadtransient[] = {
           36         PreferredLanguages,
           37         ShowIndicators,
           38         StrictTLS,
           39 +       UserAgent,
           40 +       UserScript,
           41         ParameterLast
           42  };
           43  
           44 @@ -340,6 +347,7 @@ setup(void)
           45         scriptfile = buildfile(scriptfile);
           46         cachedir   = buildpath(cachedir);
           47         certdir    = buildpath(certdir);
           48 +       scriptdir  = buildpath(scriptdir);
           49  
           50         gdkkb = gdk_seat_get_keyboard(gdk_display_get_default_seat(gdpy));
           51  
           52 @@ -364,6 +372,17 @@ setup(void)
           53                 }
           54         }
           55  
           56 +       for (i = 0; i < LENGTH(scripts); ++i) {
           57 +               if (!regcomp(&(scripts[i].re), scripts[i].regex, REG_EXTENDED)) {
           58 +                       scripts[i].file = g_strconcat(scriptdir, "/", scripts[i].file,
           59 +                                                   NULL);
           60 +               } else {
           61 +                       fprintf(stderr, "Could not compile regex: %s\n",
           62 +                               scripts[i].regex);
           63 +                       scripts[i].regex = NULL;
           64 +               }
           65 +       }
           66 +
           67         if (!stylefile) {
           68                 styledir = buildpath(styledir);
           69                 for (i = 0; i < LENGTH(styles); ++i) {
           70 @@ -722,6 +741,7 @@ seturiparameters(Client *c, const char *
           71                 case Certificate:
           72                 case CookiePolicies:
           73                 case Style:
           74 +               case UserScript:
           75                         setparameter(c, 0, p, &curconfig[p].val);
           76                 }
           77         }
           78 @@ -846,6 +866,17 @@ setparameter(Client *c, int refresh, Par
           79                         setstyle(c, getstyle(geturi(c)));
           80                 refresh = 0;
           81                 break;
           82 +       case UserAgent:
           83 +               if (a->v && g_strcmp0(a->v, ""))
           84 +                       webkit_settings_set_user_agent(s, a->v);
           85 +               break;
           86 +       case UserScript:
           87 +               webkit_user_content_manager_remove_all_scripts(
           88 +                   webkit_web_view_get_user_content_manager(c->view));
           89 +               if (a->i)
           90 +                       setscript(c, getscript(geturi(c)));
           91 +               refresh = 0;
           92 +               break;
           93         case WebGL:
           94                 webkit_settings_set_enable_webgl(s, a->i);
           95                 break;
           96 @@ -903,6 +934,20 @@ setcert(Client *c, const char *uri)
           97  }
           98  
           99  const char *
          100 +getscript(const char *uri)
          101 +{
          102 +       int i;
          103 +
          104 +       for (i = 0; i < LENGTH(scripts); ++i) {
          105 +               if (scripts[i].regex &&
          106 +                   !regexec(&(scripts[i].re), uri, 0, NULL, 0))
          107 +                       return scripts[i].file;
          108 +       }
          109 +
          110 +       return "";
          111 +}
          112 +
          113 +const char *
          114  getstyle(const char *uri)
          115  {
          116         int i;
          117 @@ -920,6 +965,26 @@ getstyle(const char *uri)
          118  }
          119  
          120  void
          121 +setscript(Client *c, const char *file)
          122 +{
          123 +       gchar *script;
          124 +
          125 +       if (!g_file_get_contents(file, &script, NULL, NULL)) {
          126 +               fprintf(stderr, "Could not read script file: %s\n", file);
          127 +               return;
          128 +       }
          129 +
          130 +       webkit_user_content_manager_add_script(
          131 +           webkit_web_view_get_user_content_manager(c->view),
          132 +           webkit_user_script_new(script,
          133 +           WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
          134 +           WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END,
          135 +           NULL, NULL));
          136 +
          137 +       g_free(script);
          138 +}
          139 +
          140 +void
          141  setstyle(Client *c, const char *file)
          142  {
          143         gchar *style;
          144 @@ -1016,7 +1081,10 @@ newwindow(Client *c, const Arg *a, int n
          145         }
          146         cmd[i++] = curconfig[JavaScript].val.i ? "-S" : "-s";
          147         cmd[i++] = curconfig[StrictTLS].val.i ? "-T" : "-t";
          148 -       if (fulluseragent && g_strcmp0(fulluseragent, "")) {
          149 +       if (curconfig[UserAgent].val.v && g_strcmp0(curconfig[UserAgent].val.v, "")) {
          150 +               cmd[i++] = "-u";
          151 +               cmd[i++] = curconfig[UserAgent].val.v;
          152 +       } else if (fulluseragent && g_strcmp0(fulluseragent, "")) {
          153                 cmd[i++] = "-u";
          154                 cmd[i++] = fulluseragent;
          155         }
          156 @@ -1119,7 +1187,9 @@ newview(Client *c, WebKitWebView *rv)
          157  /* For more interesting settings, have a look at
          158   * http://webkitgtk.org/reference/webkit2gtk/stable/WebKitSettings.html */
          159  
          160 -               if (strcmp(fulluseragent, "")) {
          161 +               if (strcmp(curconfig[UserAgent].val.v, "")) {
          162 +                       webkit_settings_set_user_agent(settings, curconfig[UserAgent].val.v);
          163 +               } else if (strcmp(fulluseragent, "")) {
          164                         webkit_settings_set_user_agent(settings, fulluseragent);
          165                 } else if (surfuseragent) {
          166                         webkit_settings_set_user_agent_with_application_details(
          167 @@ -1971,6 +2041,15 @@ clickexternplayer(Client *c, const Arg *
          168         spawn(c, &arg);
          169  }
          170  
          171 +void
          172 +clickplumb(Client *c, const Arg *a, WebKitHitTestResult *h)
          173 +{
          174 +       Arg arg;
          175 +       
          176 +       arg = (Arg)PLUMB(c->targeturi ? c->targeturi : geturi(c));
          177 +       spawn(c, &arg);
          178 +}
          179 +
          180  int
          181  main(int argc, char *argv[])
          182  {