Perplexity Force Writing Mode

Force "writing mode" on perplexity

  1. // ==UserScript==
  2. // @name Perplexity Force Writing Mode
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Force "writing mode" on perplexity
  6. // @author Lugia19
  7. // @match https://www.perplexity.ai/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Store the original fetch function
  16. const originalFetch = window.fetch;
  17.  
  18. // Override the fetch function
  19. window.fetch = async function (...args) {
  20. const url = args[0];
  21. let urlString = '';
  22.  
  23. // Handle different types of URL arguments
  24. if (typeof url === 'string') {
  25. urlString = url;
  26. } else if (url instanceof URL) {
  27. urlString = url.href;
  28. } else if (url instanceof Request) {
  29. urlString = url.url;
  30. }
  31.  
  32. // Check if this is a request to the target endpoint
  33. if (urlString.includes('https://www.perplexity.ai/rest/sse/perplexity_ask')) {
  34. try {
  35. // Clone the request to avoid modifying the original
  36. let request = args[1] || {};
  37.  
  38. // If there's a body, parse and modify it
  39. if (request.body) {
  40. const body = JSON.parse(request.body);
  41.  
  42. // Force the search_focus to be "writing"
  43. if (body.params) {
  44. body.params.search_focus = "writing";
  45. body.params.local_search_enabled = false;
  46. console.log("Forced search_focus to 'writing'");
  47. }
  48.  
  49. // Create a new request with the modified body
  50. const newRequest = {
  51. ...request,
  52. body: JSON.stringify(body)
  53. };
  54.  
  55. // Replace the original request with our modified one
  56. args[1] = newRequest;
  57. }
  58. } catch (error) {
  59. console.error("Error in fetch monkeypatch:", error);
  60. }
  61. }
  62.  
  63. // Call the original fetch with possibly modified arguments
  64. return originalFetch.apply(this, args);
  65. };
  66.  
  67. console.log("Perplexity.ai Force Writing Focus userscript loaded");
  68. })();