问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/26387485/can-i-make-greasmonkey-scripts-run-on-text-files