Get source code of website loaded in current tab - Chrome extension programming - google-chrome-extension

I'm programming a host proof application which "by definition" requires to chcek source code on client side (html,js,css).
This is best done (I believe) by extension for browser that takes source code, and generate checksum (e.g. sha1 or md5 which is implemented via javascript)
Is there any way to get source code of currently opened tab ?
(body.innerHTML or head.innerHTML is not enough)
application is after developement standalone, so all javascript,css is bundled into just one file, therefore checking is easy (no need for parsing and hashing any external scripts, style sheets,...)

body.innerHTML and head.innerHTML should be enough. On this test doc:
<script>alert("1");</script>
<html>
<script>alert("2");</script>
<head>
<title>test</title>
</head>
<script>alert("3");</script>
<body>
body
</body>
<script>alert("4");</script>
</html>
<script>alert("5");</script>
it automatically put alerts 1, 2, and 3 into head.innerHTML, while alerts 4 and 5 went to body.innerHTML.

Related

Include mathjax equations in CHM file

I'm looking to create a chm file with a topic with some mathjax equations. The html file corresponding to the topic is very simple:
<html>
<head>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
</head>
<body>
<p>
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</p>
</body>
</html>
When I compile in HTML Help Workshop, it's all good. But when I open the resulting chm file and navigate to that topic, I get this issue:
and then the equations don't render - I just get whatever is written in plain text mode. Is there any way at all to get mathjax equations render properly in a chm file?
The CHM help file format is very old and hasn't been updated by Microsoft in a long time: internally it still uses a very old version of Internet Explorer to display the content of the topics.
Recent versions of MathJax are not compatible with older web browsers and that is probably why you are seeing this error.
To avoid this problem, you can either:
Use an older version of MathJax which is compatible with older web browsers such as Internet Explorer 6
Create a hyperlinks in your CHM help file to a webpage which shows the problematic content: it will be opened by the system's default web browser which is (almost) guaranteed to be much newer
Some help authoring tools also include a way to change the Internet Explorer compatibility settings which could be used to force Microsoft Edge to be used to display content: it should allow MathJax to run properly
MathJax used to be able to work in CHM files, but it was a bit fiddly to get it to work. As I recall, you had to use an explicit configuration rather than the ?config=... approach for one thing. There are some very old discussion about it in the MathJax user's forum; see here. it was always a bit difficult to get it to work, and these discussions were about very early versions of MathJax (v1.1, v2.0, v2.1), so you might need to explicitly select older versions of MathJax. Also note that the cdn.mathjax.org address was retired in 2017 (it still exists, but redirects to another CDN, and that might also be a problem for CHM files), so you may want to use one of the other CDNs that serve MathJax, e.g., cdn.jsdelivr.net/npm/mathjax#2/MathJax.js, instead.
As a first simple step you'd try to add following line into your HTML topic files:
<meta http-equiv="X-UA-Compatible" content="IE=11">
Tested and compiled by using FAR HTML with HTML file shown below and some css stuff. I did a reverse test by deleting the line mentioned above only and the script error window appears again.
For further information using X-UA-Compatible see also: https://stackoverflow.com/a/6771584/1981088
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=11">
<title>MathJax Test</title>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<link rel="stylesheet" href="../design.css">
</head>
<body>
<h1>MathJax Test</h1>
<p>
When \(a \ne 0\), there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
</p>
</body>
</html>
is resulting in a CHM topic like this:
Of course you need a internet connection.

What's the best practice to fix history.pushState which is deleted by office.js?

Our team wants to build a documentation website that can be opened in browsers and Excel add-ins.
We chose Docusaurus V2 as the main framework to build the documentation website, and embedded office.js in it.
Office.js deletes history.pushState and history.replaceState APIs after being loaded,
so I added some JS code to polyfill it, as follows:
<html>
<head>
... ...
<script type="text/javascript">
if (history) {
var pushStateRef = history.pushState;
var replaceStateRef = history.replaceState;
}
function patch() {
if (history && !history.pushState) {
history.pushState = pushStateRef;
history.replaceState = replaceStateRef;
}
}
function onOfficejsLoad() {
Office.onReady(function() {
console.log('office.js is ready.');
patch();
});
}
</script>
<script
type="text/javascript"
src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
onload="onOfficejsLoad();"
></script>
</head>
</html>
The above code made the website to work well within our add-in in Excel Online in Chrome, Safari, as well as IE 11. However, it did not work well in Excel for Windows: when we clicked to tigger a router event, e.g. clicking on docusaurus' sidebar, there was a error, the router had no effect, and the sidebar did not work well (see Screenshot).
I managed to fix this error by adding the loading of history.js:
<html>
<head>
... ...
<script
type="text/javascript"
src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"
onload="onOfficejsLoad();"
></script>
<script
nomodule
type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/html5-history-api/4.2.10/history.js"
></script>
</head>
</html>
I still post the question, because I don't understand why the previous version did work our in add-in in Excel Online IE 11, but not in Excel for Windows, shouldn't their behaviors the same? Most importantly, when developing Excel add-ins, is there any best practice to follow to manage the conflit of history.pushState and office.js?
I added some JS code to polyfill it
I think what you have done is what I would have done too. I don't think Office.js is right to delete/override the history methods, but perhaps they had good reasons to do so (e.g. only allowing full page refreshes).
However, it did not work well in Excel for Windows
Do you know what browser is being used in Excel for Windows? It could be an entirely different browser that doesn't conform to the standards/runs in a different environment (e.g. not all the HTML5 APIs are provided on the window object). That could be why there's the weird behavior.
Sorry I don't have a Windows machine to debug this issue.

How to use Appcache with web frameworks?

I have a problem about changing the main page, I use Tornado, and in Tornado, there is a special value which is generated everytime the server is reached, it is a token to avoid xsrf attack, but when I use .appcache file, the problem is that it caches everything! and I only show to cache static like css, js, fonts, here is what it contains:
CACHE MANIFEST
# v = 2
/static/css/meteo.css
/static/css/semantic.min.css
/static/js/jquery-2.1.1.min.js
/static/css/main.css
/static/js/semantic.min.js
/static/js/geo.js
/static/js/meteo.js
/static/fonts/icons.woff2
/static/fonts/icons.woff
/static/fonts/WeatherIcons-Regular.woff
NETWORK:
/
FALLBACK:
It doesent work, the / get cached!
So how to do this with new Framework, where it we dont make the html file in the route, but the uri that is bound to a function/class?
Here is a video I made about it
And it seems that the master is always cached :
Update: From this page, it is noway!
But, you say, why don’t we not cache the HTML file, but cache all the rest.
Well. AppCache has a concept of “master entries”. A master entry is an HTML file that includes a manifest attribute in the html element that points to a manifest file (which is the only way to create an HTML5 appcache BTW). Any such HTML file is automatically added to the cache. This makes sense a lot of the time, but not always. In particular, when an HTML document changes frequently, we won’t want it cached (as a stale version of the page will most likely be served to the user as we just saw).
Is there no way to over-​​ride this? Well, AppCache has the idea of a
NETWORK whitelist, which instructs the appcache to always use the
online version of a file. What if we add HTML files we don’t want
cached to this? Sorry, no dice. HTML files in a master entry stay
cached, even when included in the NETWORK whitelist. See what I mean.
Poor AppCache didn’t make these rules. He’s just following them
literally. He’s not a douchebag, he’s a pain in the %^&*, a total
“jobs-​​worth”.
I got the solution from here:
I made a hack.html which contains:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Hack 4 Manifest</title>
</head>
<body>
{% raw xsrf_form_html() %}
</body>
</html>
And then
Add this in the main page:
<iframe style='display: none;' src='/hack'></iframe>
And then in Tornado:
(r"/hack", handlers.Hack),
class Hack(MainHandler):
#tornado.gen.coroutine
def get(self):
self.render("hack.html")
And then I use the javascript call:
xsrf = $("iframe").contents().find("input").val()
$("#laat").html('<input id="lat" type="hidden" name="lat"></input><input type="hidden" name="_xsrf" value='+xsrf+'><input id="lon" type="hidden" name="lon"></input><input class="ui fluid massive yellow button" value="Get forecast" type="submit"/>');

Natural Templating .html (like Thymeleaf) for Node.js

Is there Natural Templating for Node.js
Natural Templating (like Thymeleaf template engine) is when template page or it's part can be perfectly displayed by a browser as a prototype, without being executed at all.
That is, I have template with .html extension, and I can just open in browser to preview it, and use standard HTML dev tools to edit.
UPDATE: I have added answered, that were added in comments. (Do new StackOverflower users fear to add answers?) Hope to hear from developers, that actually use those things (just add new answer)
github.com/flatiron/plates (answered by generalhenry)
Plates (short for templates) binds data to markup. Plates has NO
special syntax. It works in the browser and in Node.js.
Active
I have a jinja like template engine in the backend.
My frontend sometimes needs to retrieve data from the server and interpolate with the content of a HTML5 template tag.
I could do that with query selectors. But I want something more closer to thymeleaf or mustache without special syntax (especially not to conflict with my backend template engine and not have any issues when presented in the HTML5 template tag).
So I created this:
https://github.com/marcodpt/tint
While this works in the browser (and that's fine for my personal use), it's not difficult to use a DOM API to port it to node or deno.
<html>
<head>
<script type="module">
import compile from "https://cdn.jsdelivr.net/gh/marcodpt/tint#2.0.0/template.js"
const render = compile(document.getElementById("app"))
render({
message: "Hello World!"
})
</script>
</head>
<body>
<div id="app">
<h1 :text="message">Loading...</h1>
</div>
</body>
</html>
{{ mustache }} (answered by etienne)
Logic-less templates.
Available in Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C,
Java, .NET, Android, C++, Go, Lua, ooc, ActionScript, ColdFusion,
Scala, Clojure, Fantom, CoffeeScript, D, and for node.js.
Works great with TextMate, Vim, Emacs, and Coda.
I was using Handlebars.
There are actually quite a few template engines in JavaScript and here you can decide what you need.

merge javascript files in directory with node uglify + watch?

Goal:
I am trying to build an effortless workflow for client side templating development.
Doc Root:
/views/uncompiled/
/static/js/compiled/
We start in /views/uncompiled/
This is where I can build stuff out, like /views/uncompiled/index.html for example.
Im building dust.js templates, so I am using dusterjs too listen for changes in my /views/uncompiled/ directory, and automatically render compiled *.js counterparts in my /static/js/compiled/ directory.
So /views/uncompiled/index.html renders out /static/js/compiled/index.js anytime a change is saved.
Problem:
My layout.html is growing bigger. I need to include a new *.js script everytime I add another template:
<head>
<script src='/static/js/compiled/index.js'></script>
<script src='/static/js/compiled/header.js'></script>
<script src='/static/js/compiled/footer.js'></script>
<script src='/static/js/compiled/baconstrips.js'></script>
...
</head>
Solution:
Use another watch on the /static/js/compiled/ folder too automatically concat *.js into a single app.js that will always be included in my <head> anytime the contents of that folder changes:
<head>
<script src='/static/js/app.js'></script>
</head>
Question:
I would like to use a concatonation tool like Uglify.js that also does compression.
Are there any node packages that automate the solution above?
Is there a native function of Uglify.js that already does this?
Use grunt.js. It has the ability to watch/concentate/minify your files through various contributed modules. Takes a little getting used to (I still am myself) but you will end up with a custom build process that works the way that you want it to work, which is priceless.

Resources