Can I Make Greasmonkey Scripts Run On Text Files?

余生长醉 提交于 2021-01-27 21:16:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!