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?
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.
Related
On certain actions, I want to open a modal/popup on the website user is visiting.
I am doing it right now as:
//contentScript.js
function on_certain_action(message) {
const injectElement = document.createElement("div");
injectElement.className = 'app_modal'
document.body.appendChild(injectElement);
}
But, it is getting complex and difficult to understand. Is there some way I can write it in HTML? OR how do you handle it, if you are injecting complex script in content?
Our Firefox addon issues queries to Google at the backend (main.js), then extracts some content through xpath. For this purpose, we use innerHTML to create a document instance for xpath parsing. But when we submit this addon to Mozilla, we got rejected because:
This add-on is creating DOM nodes from HTML strings containing potentially unsanitized data, by assigning to innerHTML, jQuery.html, or through similar means. Aside from being inefficient, this is a major security risk. For more information, see https://developer.mozilla.org/en/XUL_School/DOM_Building_and_HTML_Insertion
Following the link provided, we tried to replace innerHTML with nsIParserUtils.parseFragment(). However, the example code:
let { Cc, Ci } = require("chrome");
function parseHTML(doc, html, allowStyle, baseURI, isXML) {
let PARSER_UTILS = "#mozilla.org/parserutils;1";
...
The Cc, Ci utilities can only be used on main.js, while the function requires a document (doc) as the argument. But we could not find any examples about creating a document inside main.js, where we could not use document.implementation.createHTMLDocument("");. Because main.js is a background script, which does not have reference to the global built-in document.
I googled a lot, but still could not find any solutions. Could anybody kindly help?
You probably want to use nsIDOMParser instead, which is the same as the standard DOMParser accessible in window globals except that you can use it from privileged contexts without a window object.
Although that gives you a whole document with synthesized <html> and <body> elements if you don't provide your own. If you absolutely need a fragment you can use the html5 template element to extract a fragment via domparser:
let partialHTML = "foo <b>baz</b> bar"
let frag = parser.parseFromString(`<template>${partialHTML}</template>`, 'text/html').querySelector("template").content
I would like to change a page using a Greasemonkey script, but the page is the result of a CGI POST call and thus the URL looks like this:
http://www.example.com/cgi-bin/foo.pl
So obviously I cannot use the #include in the Greasemonkey script, because that URL fits a lot of pages the app can generate.
I could just look for some elements on the page within the script before doing stuff:
if (document.getElementById('someIdentifier')) {
// do changes in here
}
But I was wondering if there is a better, maybe built-in way to do this.
Greasemonkey (and most userscript engines) has no built-in way to target a page by its elements. Your script needs to poll or use mechanisms like Mutation Observers to check for the desired elements.
A handy way to do this is to use the waitForKeyElements() utility, and you still want to use the #include, #exclude, and/or #match directives to narrow down the page firings as much as possible.
Here is a complete Greasemonkey/Tampermonkey script illustrating the process:
// ==UserScript==
// #name _Do something after select posts
// #include http://www.example.com/cgi-bin/foo.pl*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// ==/UserScript==
/*- The #grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
function processTargetElement (jNode) {
//***** YOUR CODE HERE *****
jNode.css ("background", "pink");
}
waitForKeyElements ("#someIdentifier", processTargetElement);
I'm playing around with my first Node.js Express application, and as every programmer knows, the first thing you should build when testing out a new framework is a blog! Anyway, I'd like to write the articles in Markdown and then render it in the view. I saw that Jade allows for this to be done inside the view itself, using filters, but I can't get that working.
To simplify the situation, here's an example of what I'm talking about.
//app.js
res.render("article", {
md : "Hello World!\n\n*Woo*"
});
//article.jade
section
:markdown
#{md}
But, that outputs this: <section><h1>{md}</h1></section>... it isn't substituting in the variables I've passed to it.
Then I tried this:
//article.jade
section
:markdown
!{md}
And the output is this:
<section><p>Hello World!
*Woo*</p></section>
So, now it's not parsing the markdown!
I have been able to get this to work by parsing the markdown in the app.js file and then passing the HTML to the view to display, but I don't know, that seems a bit messier.
Is there a way to pass variables into Jade filters?
You can do this with a function passed in to jade from node:
var md = require("node-markdown").Markdown;
Then pass it into the view as a local:
res.render('view', { md:md, markdownContent:data });
Then render it in the jade view by calling the function:
!= md(markdownContent)
The node module node-markdown is deprecated. The marked is advanced new version. You can try like this
var md = require('marked');
Inside your router
res.render('template', { md: md });
Inside your jade template
div!= md(note.string)
I don't think jade can do this out of the box. One way to accomplish it that might feel slightly cleaner than pre-rendering the markdown is to create a helper function called markdown that takes a markdown string and returns HTML. Then you could do something like
section
!= markdown(md)
The markdown function should be included in the locals data when you render the jade template and can directly use a markdown library to convert the markdown syntax to HTML.
If you are using Scalate's Jade support you can enter:
section
:&markdown
#{md}
You can also import external files with:
section
:&markdown
#{include("MyFile.md")}
I am trying to create a Firefox extension that uses a flex application. I have tried to wrap it in XUL types (<iframe> and <browser>) and I have no preference as to which one I use... whichever works.
The problem is that whenever I use a relative path (access through chrome:// or mySWF.html) the flash fails to load.
I have a method to search for the absolute path (it's posted below) but I cannot for the life of me figure out a way to dynamically change the src of either an iframe or browser.
<script type="text/javascript">
function loadSWF() {
alert("loadSWF!");
var fullPath = "file:///" + extensionPath.path.replace(/\\/g,"/") + "/chrome/content/HelloWorld.html";
top.document.getElementById('AppFrame').setAttribute("src",fullPath);
}
</script>
Below are my 2 methods of calling the flex app:
<iframe
type="content"
src=??????
flex="1"
id="AppFrame"
name="AppFrame"
onLoad="loadSWF();"/>
<browser
id="browserid"
type="content"
src=??????
flex="1"/>
How can I call my function to set the src attribute???
1) Dynamically setting src works fine (see testcase below).
2) To get a URL of a file, use nsIIOService.newFileURI() instead of trying to convert by hand.
3) onLoad="loadSWF();" in your iframe is suspicious, you should have posted the complete XUL code that shows how it all fits together. You should call loadSWF not from the iframe's load handler, but from your XUL document's load handler or off another event.
Testcase for #1:
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="yourwindow" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:html="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
<![CDATA[
function f() {
document.getElementById("z").setAttribute("src", "http://google.com/")
}
]]>
</script>
<iframe type="content" id="z"/>
<button onclick="f()"/>
</window>