about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMarco Peereboom <marco@conformal.com>2011-07-25 16:19:45 +0000
committerMarco Peereboom <marco@conformal.com>2011-07-25 16:19:45 +0000
commit6b5a8503e3a8b2f432dde15f398a28319bd2c59a (patch)
tree953dd88522c7b7702961a60f4e224b014b09eda2
parent88b52fe5333154979d27a49668f3b462c7a417a8 (diff)
downloadxombrero-6b5a8503e3a8b2f432dde15f398a28319bd2c59a.tar.gz
add a method for running scripts on a page. the example uses
get_flash_videos to download and play youtube stuff.

From: Michal Mazurek <akfaew@jasminek.net>
-rw-r--r--playflash.sh28
-rw-r--r--xxxterm.17
-rw-r--r--xxxterm.c78
-rw-r--r--xxxterm.conf7
4 files changed, 119 insertions, 1 deletions
diff --git a/playflash.sh b/playflash.sh
new file mode 100644
index 0000000..66081a4
--- /dev/null
+++ b/playflash.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+# $xxxterm$
+#
+# This is an example script for playing flash videos. It requires the
+# get_flash_video package to be installed on the system. You can copy this
+# file to ~/.xxxterm/, and set
+#   default_script = ~/.xxxterm/playflash.sh
+# in ~/.xxxterm.conf. Remember to make this file executable.
+#
+# You may wish to add the following line to ~/.get_flash_videosrc
+# to avoid accumulating files:
+#   player = mplayer -loop 0 %s 2>/dev/null; rm -f %s
+
+cd /var/tmp && get_flash_videos -p "$1"
+#!/bin/sh
+# $xxxterm$
+#
+# This is an example script for playing flash videos. It requires the
+# get_flash_video package to be installed on the system. You can copy this
+# file to ~/.xxxterm/, and set
+#   default_script = ~/.xxxterm/playflash.sh
+# in ~/.xxxterm.conf. Remember to make this file executable.
+#
+# You may wish to add the following line to ~/.get_flash_videosrc
+# to avoid accumulating files:
+#   player = mplayer -loop 0 %s 2>/dev/null; rm -f %s
+
+cd /var/tmp && get_flash_videos -p "$1"
diff --git a/xxxterm.1 b/xxxterm.1
index ecb56cb..3222676 100644
--- a/xxxterm.1
+++ b/xxxterm.1
@@ -477,6 +477,10 @@ if it is the last tab.
 Restart
 .Nm
 and reload all current tabs.
+.It Cm run_script [path_to_script]
+Runs the script path_to_script with the current uri as the argument.
+If path_to_script is not provided, the value of default_script is used
+instead.
 .It Cm session , Cm session show
 Display the current session name.
 By default the session name is main_session.
@@ -734,6 +738,9 @@ Enable cookies.
 Give focus in newly created tab instead of opening it in the background.
 .It Cm default_font_size
 Set the default browsing font size.
+.It Cm default_script
+Path to the script used as the default value for the run_script
+command.
 .It Cm default_zoom_level
 Set the default browsing zoom level.
 .It Cm download_dir
diff --git a/xxxterm.c b/xxxterm.c
index cec3c96..11357bd 100644
--- a/xxxterm.c
+++ b/xxxterm.c
@@ -510,6 +510,7 @@ int		enable_scripts = 1;
 int		enable_plugins = 0;
 int		default_font_size = 12;
 gfloat		default_zoom_level = 1.0;
+char		default_script[PATH_MAX];
 int		window_height = 768;
 int		window_width = 1024;
 int		icon_size = 2; /* 1 = smallest, 2+ = bigger */
@@ -555,6 +556,7 @@ struct key_binding;
 int		set_browser_mode(struct settings *, char *);
 int		set_cookie_policy(struct settings *, char *);
 int		set_download_dir(struct settings *, char *);
+int		set_default_script(struct settings *, char *);
 int		set_runtime_dir(struct settings *, char *);
 int		set_tab_style(struct settings *, char *);
 int		set_work_dir(struct settings *, char *);
@@ -569,6 +571,7 @@ GtkWidget *	create_button(char *, char *, int);
 char		*get_browser_mode(struct settings *);
 char		*get_cookie_policy(struct settings *);
 char		*get_download_dir(struct settings *);
+char		*get_default_script(struct settings *);
 char		*get_runtime_dir(struct settings *);
 char		*get_tab_style(struct settings *);
 char		*get_work_dir(struct settings *);
@@ -632,6 +635,12 @@ struct special		s_cookie_wl = {
 	walk_cookie_wl
 };
 
+struct special		s_default_script = {
+	set_default_script,
+	get_default_script,
+	NULL
+};
+
 struct special		s_download_dir = {
 	set_download_dir,
 	get_download_dir,
@@ -673,6 +682,7 @@ struct settings {
 	{ "ctrl_click_focus",		XT_S_INT, 0,		&ctrl_click_focus, NULL, NULL },
 	{ "default_font_size",		XT_S_INT, 0,		&default_font_size, NULL, NULL },
 	{ "default_zoom_level",		XT_S_FLOAT, 0,		NULL, NULL, NULL, &default_zoom_level },
+	{ "default_script",		XT_S_STR, 0, NULL, NULL,&s_default_script },
 	{ "download_dir",		XT_S_STR, 0, NULL, NULL,&s_download_dir },
 	{ "enable_cookie_whitelist",	XT_S_INT, 0,		&enable_cookie_whitelist, NULL, NULL },
 	{ "enable_js_whitelist",	XT_S_INT, 0,		&enable_js_whitelist, NULL, NULL },
@@ -1243,6 +1253,26 @@ get_cookie_policy(struct settings *s)
 }
 
 char *
+get_default_script(struct settings *s)
+{
+	if (default_script[0] == '\0')
+		return (0);
+	return (g_strdup(default_script));
+}
+
+int
+set_default_script(struct settings *s, char *val)
+{
+	if (val[0] == '~')
+		snprintf(default_script, sizeof default_script, "%s/%s",
+		    pwd->pw_dir, &val[1]);
+	else
+		strlcpy(default_script, val, sizeof default_script);
+
+	return (0);
+}
+
+char *
 get_download_dir(struct settings *s)
 {
 	if (download_dir[0] == '\0')
@@ -2402,6 +2432,50 @@ save_tabs_and_quit(struct tab *t, struct karg *args)
 }
 
 int
+run_page_script(struct tab *t, struct karg *args)
+{
+	const gchar		*uri;
+	char			*tmp, script[PATH_MAX];
+
+	tmp = args->s != NULL && strlen(args->s) > 0 ? args->s : default_script;
+	if (tmp[0] == '\0') {
+		show_oops(t, "no script specified");
+		return (1);
+	}
+
+	if ((uri = get_uri(t)) == NULL) {
+		show_oops(t, "tab is empty, not running script");
+		return (1);
+	}
+
+	if (tmp[0] == '~')
+		snprintf(script, sizeof script, "%s/%s",
+		    pwd->pw_dir, &tmp[1]);
+	else
+		strlcpy(script, tmp, sizeof script);
+
+	switch (fork()) {
+	case -1:
+		show_oops(t, "can't fork to run script");
+		return (1);
+		/* NOTREACHED */
+	case 0:
+		break;
+	default:
+		return (0);
+	}
+
+	/* child */
+	execlp(script, script, uri, (void *)NULL);
+
+	_exit(0);
+
+	/* NOTREACHED */
+
+	return (0);
+}
+
+int
 yank_uri(struct tab *t, struct karg *args)
 {
 	const gchar		*uri;
@@ -4835,6 +4909,7 @@ struct key_binding {
 	{ "pasteurinew",	0,	0,	GDK_P		},
 	{ "toplevel toggle",	0,	0,	GDK_F4		},
 	{ "help",		0,	0,	GDK_F1		},
+	{ "run_script",		MOD1,	0,	GDK_r		},
 
 	/* search */
 	{ "searchnext",		0,	0,	GDK_n		},
@@ -5147,7 +5222,6 @@ struct cmd {
 	{ "scrollfarright",	0,	move,			XT_MOVE_FARRIGHT,	0 },
 	{ "scrollfarleft",	0,	move,			XT_MOVE_FARLEFT,	0 },
 
-
 	{ "favorites",		0,	xtp_page_fl,		0,			0 },
 	{ "fav",		0,	xtp_page_fl,		0,			0 },
 	{ "favadd",		0,	add_favorite,		0,			0 },
@@ -5209,6 +5283,7 @@ struct cmd {
 	{ "urlhide",		0,	urlaction,		XT_URL_HIDE,		0 },
 	{ "urlshow",		0,	urlaction,		XT_URL_SHOW,		0 },
 	{ "statustoggle",	0,	statustoggle,		0,			0 },
+	{ "run_script",		0,	run_page_script,	0,			XT_USERARG },
 
 	{ "print",		0,	print_page,		0,			0 },
 
@@ -6300,6 +6375,7 @@ run_mimehandler(struct tab *t, char *mime_type, WebKitNetworkRequest *request)
 	switch (fork()) {
 	case -1:
 		show_oops(t, "can't fork mime handler");
+		return (1);
 		/* NOTREACHED */
 	case 0:
 		break;
diff --git a/xxxterm.conf b/xxxterm.conf
index da611d9..5d52a1b 100644
--- a/xxxterm.conf
+++ b/xxxterm.conf
@@ -31,6 +31,12 @@
 # enable_spell_checking	= 1
 # spell_check_languages = en_US
 
+#
+# default_script points to a script executed by the run_script command.
+# the only argument passed to this script is the current uri
+#
+# default_script	= ~/.xxxterm/playflash.sh
+
 # NOTE: webkit 1.4.x overwrites these values!
 # max_host_connections	= 5
 # max_connections	= 25
@@ -158,6 +164,7 @@
 # keybinding	= print,C-p
 # keybinding	= quitall,C-q
 # keybinding	= restart,M1-q
+# keybinding	= run_script,M1-r
 # keybinding	= js toggle,C-j
 # keybinding	= cookie toggle,M1-c
 # keybinding	= togglesrc,C-s