blocking Javascript in browser
Added by Tino Michael about 5 years ago
Hi all,
I am trying to load some external javascript into my Wt app. I do so via `app->require(...)`.
This seems to be working, though when I try to access the page with a javascript blocker up (e.g. uBlock Origin),
I get a browser message that it failed to load the external script.
I tried asking `WEnvironment` to conditionally load the script but it thinks JS is available.
How can I tell the browser to --- instead of "require" a script --- "load it when it's not too much of a hassle"?
Replies (7)
RE: blocking Javascript in browser - Added by Roel Standaert about 5 years ago
Require does what it says, it requires that JavaScript to be loaded, the idea being that any JavaScript you execute after that could depend on it. For non-essential JavaScript, we don't currently have something ready-made for that. We either just insert the <script>
tag using a WText
with TextFormat::UnsafeXHTML
, or using doJavaScript()
, usually for Google Analytics or stuff like that. Usually those actually provide the piece of JavaScript you need to execute. Maybe it's an idea to add something to maybe load, but not require, some JavaScript, but there's no standard way for it at the moment.
RE: blocking Javascript in browser - Added by Tino Michael about 5 years ago
Hi Roel, thanks for your reply.
So, I want to include Google AdSense on my website and need to include their
script tag somehow within my HTML header:
<script data-ad-client="ca-pub-xxxxxxx" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
After some more searching in this forum for an unrelated issue, I found the <head-matter>
where
All contents will be inserted into the
tag verbatim
Now, having added the script tag, starting the Wt-app immediately ends up in an exception call:
terminate called after throwing an instance of 'Wt::WServer::Exception'
what(): Error reading: /path/to/config/wt_config.xml: expected =
After a little playing around, removing async
made the Wt-app start, though the browser shows a blank page and the wt session dies after a timeout:
[2020-Mar-16 09:42:29.404] 4682 - [info] "Wt: session created (#sessions = 1)"
[2020-Mar-16 09:42:29.404] 4682 [/web U3R0YkDOH4tqMZ7i] [info] "WEnvironment: UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
127.0.0.1 - - [2020-Mar-16 09:42:29.406] "GET /web/ HTTP/1.1" 200 2416
[2020-Mar-16 09:42:29.406] 4682 - [info] "WebRequest: took 2.085 ms"
127.0.0.1 - - [2020-Mar-16 09:42:31.436] "GET /web?wtd=U3R0YkDOH4tqMZ7i&request=style&page=1 HTTP/1.1" 200 0
[2020-Mar-16 09:42:31.436] 4682 - [info] "WebRequest: took 2000.66 ms"
127.0.0.1 - - [2020-Mar-16 09:42:31.462] "GET /icons/fire-32.png HTTP/1.1" 200 691
[2020-Mar-16 09:42:40.510] 4682 [/web U3R0YkDOH4tqMZ7i] [info] "WebController: timeout: expiring"
[2020-Mar-16 09:42:40.510] 4682 [/web U3R0YkDOH4tqMZ7i] [info] "Wt: session destroyed (#sessions = 0)"
So, bottom line: I'd like to insert this <script>
tag into my header without breaking the page when the browser has an ad-blocker running...
RE: blocking Javascript in browser - Added by Roel Standaert about 5 years ago
It seems to me that you didn't supply the -c
or --config
argument properly. You need to change the path there to the actual path to your wt_config.xml
. Right now it's looking for /path/to/config/wt_config.xml
, which likely does not exist.
RE: blocking Javascript in browser - Added by Tino Michael about 5 years ago
Hi Roel,
I manually changed /path/to/config/wt_config.xml
before posting here to not quote my local dev environment verbatim here.
The path is correct, the file can be found and other changes in there are reflected in the app (e.g. favicon, upload file size limits, user agents etc.)
RE: blocking Javascript in browser - Added by Roel Standaert about 5 years ago
Aha, now I see. The xml
file needs to be valid XML. Just "async" on its own may be valid HTML, but it's not valid XML. It should be fine if you change it to async="async"
.
RE: blocking Javascript in browser - Added by Tino Michael about 5 years ago
Oh, I see, thanks for pointing that out (the documentation of the tag said it's all added "verbatim").
Changing it to async="async"
, I get the same problem as without the async: The page stays blank and the session gets destroyed after the time out.
As a matter of fact, this happens also with a completely empty script tag, both <script />
and <script></script>
...
The <link>
tags for the favicon in the same <head-matter>
work without any issues.
I managed to add the tag using a WText
with TextFormat::UnsafeXHTML
with the same result that an ad-blocker breaks the whole website...
Funny enough, allowing javascript, reloading and blocking it again shows the webpage... (using uBlock Origin, so that problem might actually be on their side)
RE: blocking Javascript in browser - Added by Roel Standaert about 5 years ago
Yes, it is indeed added verbatim but it's still an XML file, so it needs to be valid XML. Maybe it works with a CDATA
section.
We actually add a <script>
tag on the Wt website like this:
<script type="text/javascript">
/*<![CDATA[*/
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-*******-*', 'auto');
ga('send', 'pageview');
/* ]]> */
</script>
With UA-*******-*
your tracking id.
We just put that in a WTemplate
.