logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://anongit.hacktivis.me/git/badwolf.git/

userscripts.c (2187B)


  1. // BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
  2. // SPDX-FileCopyrightText: 2019-2023 Badwolf Authors <https://hacktivis.me/projects/badwolf>
  3. // SPDX-License-Identifier: BSD-3-Clause
  4. #include "userscripts.h"
  5. #include "badwolf.h"
  6. #include "config.h"
  7. #include <assert.h>
  8. #include <glib/gi18n.h> /* _() and other internationalization/localization helpers */
  9. #include <glob.h>
  10. void
  11. load_userscripts(WebKitUserContentManager *content_manager)
  12. {
  13. glob_t scripts_path_glob;
  14. gchar *scripts_path = g_build_filename(g_get_user_data_dir(), "badwolf", "scripts", "*.js", NULL);
  15. unsigned int loaded = 0, failed = 0;
  16. fprintf(stderr, _("badwolf: Checking for userscripts matching %s\n"), scripts_path);
  17. switch(glob(scripts_path, GLOB_NOSORT, NULL, &scripts_path_glob))
  18. {
  19. case 0:
  20. fprintf(stderr, _("badwolf: Notice: Found %zd userscripts\n"), scripts_path_glob.gl_pathc);
  21. break;
  22. case GLOB_NOMATCH:
  23. fprintf(stderr, _("badwolf: Notice: No userscripts found\n"));
  24. goto clean;
  25. break;
  26. case GLOB_NOSPACE:
  27. fprintf(stderr, _("badwolf: Failed to list userscripts: Out of Memory\n"));
  28. goto clean;
  29. break;
  30. case GLOB_ABORTED:
  31. fprintf(stderr, _("badwolf: Failed to list userscripts: Read Error\n"));
  32. goto clean;
  33. break;
  34. }
  35. for(size_t i = 0; i < scripts_path_glob.gl_pathc; i++)
  36. {
  37. char *filename = scripts_path_glob.gl_pathv[i];
  38. gchar *contents;
  39. GError *err = NULL;
  40. if(g_file_get_contents(filename, &contents, NULL, &err) && err == NULL)
  41. {
  42. WebKitUserScript *userscript =
  43. webkit_user_script_new(contents,
  44. WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
  45. WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
  46. NULL,
  47. NULL);
  48. webkit_user_content_manager_add_script(content_manager, userscript);
  49. loaded++;
  50. }
  51. else
  52. {
  53. fprintf(stderr, _("badwolf: Error reading userscript: %s\n"), err->message);
  54. g_error_free(err);
  55. failed++;
  56. }
  57. }
  58. fprintf(stderr,
  59. _("badwolf: Notice: Userscript loading: %d loaded, %d failed to load\n"),
  60. loaded,
  61. failed);
  62. clean:
  63. g_free(scripts_path);
  64. globfree(&scripts_path_glob);
  65. }