2

My web-server serves up the logs as plain text.
Is it possible to use Greasemonkey to do some formatting of the logs or is it only possible to use it on HTML content?

Could I force the text into HTML on load then process it after?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
opticyclic
  • 7,412
  • 12
  • 81
  • 155

1 Answers1

2

Yes, Greasemonkey works on text files.

Note that when a browser such as Firefox or Chrome displays a plain text file, the browser wraps it in a dynamic <pre> element, like so:

<html><head>...</head>
<body>
    <pre>
        <!-- Actual content of text file is here. -->
    </pre>
</body></html>

For best results, take that into account when scripting.

For example, for this public text file (U of I, Open Source License), install this script using Greasemonkey, Tampermonkey, Scriptish, etc.:

// ==UserScript==
// @name     _Manip text file
// @include  http://llvm.org/releases/2.8/LICENSE.TXT
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// ==/UserScript==

var pageTextNd  = $("body > pre");
var newPageTxt  = pageTextNd.text ().replace (/\bLLVM\b/gi, "Ernst Blofeld");
//-- Rewrite the page
pageTextNd.text (newPageTxt);

And see the results.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Thats cool. Can you explain what $("body > pre"); is and why you need /\bLLVM\b/gi in the search and not just LLVM. Thanks. – opticyclic Oct 16 '14 at 15:32
  • `$("body > pre")` is the jQuery way of getting the dynamic `
    ` node that the text file is wrapped in. `body > pre` is also a standard/valid CSS selector as many jQuery selectors are.  The `\b`s may not be strictly necessary in this case (I didn't check), but that's just to make sure that no derivative/similar words got diced up awkwardly. (EG: replacing "hi" with "bye"; don't want to affect "highlights", so include the word breaks (`\b`).)
    – Brock Adams Oct 16 '14 at 16:27
  • Very useful to me. Thank you. In my opinion $("pre") is enough as we are pretty sure that the browser will create a single pre object and end of line characters will probably be "\n", at least in Firefox. – Hatoru Hansou May 06 '15 at 13:20