How to reload stale external google maps api javascript - javascript

So we have a chrome extension and the background.html looks like this
<!DOCTYPE html>
<html>
<head>
<script id="google-maps-js" type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?&key=AIzaSyCnHJh2lsVbM_fPXgwciRO-fInFgkkEebw&sensor=false&libraries=places"></script>
<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript" src="/background_helper.js"></script>
<script type="text/javascript" src="/background.js"></script>
</head>
<body>
<div id="map"></div>
</body>
</html>
The google maps api js is loaded once when the extension is installed or when the browser is first opened but if the browser is left open for more than 3 days, the geocoder service starts to return 403 errors and I believe it's related to the problem mentioned in the answer to this question:
After months of 100% stability, today I get 403 Error on google maps services
It seems that after 3 days, the javascript becomes 'stale' and google will return 403 errors to any requests.
I tried putting in a timer so that every 24 hours it removes the google maps script tag and reads it but then I get errors saying I've included the google maps script multiple times and it will behave unexpectedly.
How do I "reload" or "refresh" the javascript so it's no longer stale?

You should be able to simply manipulate the <script> element.
Delete it from the DOM by id, and insert an identical copy. That will re-run the script, and the server should be configured to disallow caching if it is indeed getting stale.

Related

Problems with map's script API

I'm trying to use maps api (yandex but I have seen similar problem in google maps too) and include its script in head:
<!DOCTYPE html>
<html lang="en">
<head>
<title>site</title>
<meta charset="UTF-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/postscribe/2.0.8/postscribe.min.js">
<script async src="https://api-maps.yandex.ru/1.1/index.xml" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
var map = new YMaps.Map(document.getElementById("YMapsID"));
map.setCenter(new YMaps.GeoPoint(59.938518, 30.323342), 10);
};
</script>
</head>
Here is body:
<body>
<div class="map-block_map-item">
<div id="YMapsID" style="width:100%;height:100%">
</div>
</div>
</body>
But when page downloads I get this warning:
"It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened."
If I don't use async attribute get this message:
A parser-blocking, cross site (i.e. different eTLD+1) script, https://api-maps.yandex.ru/1.1/_YMaps.js?v=1.1.21-58, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details
How can I solve this problem? As I understood it happens on very slow connections (but I have rather fast connection so why?) and it cause block document.written scripts. (I'm new in js so yes I have google it but didn't understand how it can be improve besides async script download)

How to remove js scripts from the new, empty MVC project?

I created a new empty MVC project in VS2015 and as soon as I added my first view I got a lot of js scripts that I didn't add. I see them in the developer tools but not in the solution. What are they and how do I remove them?
Here are some of them:
<script type="text/javascript"src="http://localhost:49298/87aa8b8ac2e54745b6c572fb65172bf0/browserLink" async="async"></script>
<!-- End Browser Link -->
<script src="//dmp.adriverssp.com/scripts/stat/adriverssp.js?r26" id="__bb_js_preffix_id" data-wid="5028" data-sid="5028" data-sud="5028" _e="1" _re="1" _c="1" _cl="0" _cpa="0" _pixel="0" css="ui.css" js="ui.js" async="" charset="utf-8"></script>
<script src="//trendtext.eu/37d2b1bb945e0c3753.js"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=&wid=49544&sid=&tid=893&rid=LOADED&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&custom1=localhost:49206&t=1453837765845"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=&wid=49544&sid=&tid=893&rid=BEFORE_OPTOUT_REQ&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&t=1453837765845"></script>
<script type="text/javascript" src="http://trendtext.eu/optout/get?jsonp=__twb_cb_512368056&key=37d2b1bb945e0c3753&t=1453837765846"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=&wid=49544&sid=&tid=893&rid=FINISHED&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&custom1=localhost:49206&t=1453837765846"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=&wid=49544&sid=&tid=893&rid=OPTOUT_RESPONSE_OK&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&t=1453837765905"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=lnkr&wid=49544&sid=&tid=893&rid=MNTZ_INJECT&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&t=1453837765906"></script>
<script type="text/javascript" src="http://trendtext.eu/addons/lnkr15.min.js"></script>
<script type="text/javascript" src="http://trendtext.eu/metric/?mid=lnkr&wid=49544&sid=&tid=893&rid=MNTZ_LOADED&jsonp=window.__twb__37d2b1bb945e0c3753.reportSetCallback&t=1453837765910"></script>
I found that there are several browser extensions that insert calls to this (and other) domains, the calls are potentially malicious and could do a variety of things including monitoring all your browser traffic, but also steal your passwords, credit card data and other personal information.
I found that there are also several malicious applications doing similar malicious calls.
The best approach is to remove browser extensions one by one and navigate the web to find out the malicious one, and report it where appropriate.
Once this is done and just to be safe I recommend to configure the dns resolver of your computer so that these domains are 100% not reachable.
add the following two lines:
127.0.0.1 cr-input.mxpnl.net
127.0.0.1 trendtext.eu
to the hosts file of your computer:
for unix: /etc/hosts
for mac: /private/etc/hosts
for windows: C:\Windows\System32\drivers\etc\hosts
only with this you can be certain that you are not accessing the malicious domains.
I have faced same problem with chrome. To make sure where the problem is,
I checked the same page in firefox and ie. These browsers had no such problem. then it was sure that this problem is chrome specific.
Updated chrome to the latest one. Problem still was there.
I disabled then enabled extensions. Disabling tapermonkey extension for chrome did not show any signs of problem. Then it was sure that it was causing trouble.
Then removing it completely solved problem for ever.

Firefox doesn't load external https javascript that fills out page content

I am in a situation where I am maintaining a page of opening hours that sometimes change. The page is used on several different platforms:
An external CMS system on http Danish and English
A Sharepoint intranet on https in Danish and English
In order to not have to change 4 different pages each time the opening hours change, I am loading the opening hours from a single javascript file which I have located on another server.
The script is here: https://blanketter.science.ku.dk/studenterservice/studenterservice-content.js
I am loading the page using the below code below:
<html>
<head>
<title></title>
<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type" />
<link rel="stylesheet" type="text/css" href="https://blanketter.science.ku.dk/studenterservice/studenterservice.css">
<script type="text/javascript" src="https://blanketter.science.ku.dk/studenterservice/studenterservice-content.js"></script>
</head>
<body>
<div id="dynamic-intro">Error message if JavaScript disabled or Firefox used.</div>
<div id="dynamic-specialOpeningHours">Error message if JavaScript disabled or Firefox used.</div>
<div id="dynamic-contact">Error message if JavaScript disabled or Firefox used.</div>
<div id="dynamic-closedWeeks">Error message if JavaScript disabled or Firefox used.</div>
<div id="dynamic-directions">Error message if JavaScript disabled or Firefox used.</div>
<script language="JavaScript" type="text/javascript">
document.getElementById('content').className = "subpage wide";
</script>
<script type="text/javascript">
writePageByLanguage('danish');
</script>
</body>
</html>
You can see an example of this at http://www.science.ku.dk/uddannelser/studenterservice/vejledning_kopi/.
The site works as it should in Chrome and Internet Explorer/Edge, but Firefox fails to load the .js file and displays the eror message instead.
I have tried to place the .js file on a http:// server instead (different hosting provider). In this case Firefox is able to display the content.
Any help in clearing up how I could possible fix this while still having the .js file on my https server would be much appreciated.
From the Firefox Network tab:
blanketter.science.ku.dk uses an invalid security certificate.
The certificate is not trusted because the issuer certificate is
unknown. The server might not be sending the appropriate intermediate
certificates. An additional root certificate may need to be imported.
(Error code: sec_error_unknown_issuer)
You either need to get everyone visiting the site to manually trust the certificate, or get a new certificate that is signed by an authority that Firefox trusts, or fix the configuration of the server to provide the intermediate certificates.

Confirm if google analytics is well implemented

Intro: I'm having a problem implementing google analytics in a website that is not mine and would like to correctly implement it and confirm without the need of waiting several hours untill google checks my website again.
Main problem: I copy pasted the script code to every page on the website (all html) but google isn't detecting it. I suspect the problem is in the first index.html on the root of the website but for now I must have it there so please don't tell me I have to remove this file. That is not the question.
Code of index.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
<!-- GOOGLE ANALYTICS PASTED CODE IS HERE -->
</script>
<!-- TITLE AND METADATA ARE HERE BUT NOT RELEVANT -->
<meta http-equiv="REFRESH" content="0;url=http://www.FAKEDOMAIN.com/Site/index.html">
</head>
<body>
</body>
</html>
Extra info: Google control not installed because it says it doesn't detectes the code on initial page. I think it is because the script might not be tottaly executed and then the page refreshs to another link and google will just consider that the script isn't installed on the initial page because the script wasn't tottaly run. Is there a way I can force the full execution before redirecting? Then a way to confirm with alerts if the data is being collected properly so that I am sure it is just a matter of time before google analytics control is properly installed?
I don't believe Google actually attempts to detect the code on your page. Rather, the page runs and the script executes and pings some Google server to report the usage. Once this usage gets reported, you'll start seeing activity in your Google account. This might take a day or two.
I think you're right about the meta refresh though. Once the meta tag is encountered, the browser will redirect and script on the page is not executed. I would suggest removing the <meta> tag and redirecting with script, after the Google Analytics code is run:
<script>
// GA code here
location.href = 'http://www.FAKEDOMAIN.com/Site/index.html'; //Redirect here
</script>

Why is jQuery not working in the browser action popup of a google chrome extension?

I've created a button whose function is to hide itself when clicked. But, it isn't working.Here's the code :
<html>
<body>
<button id="b">HIDE</button>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#b").click(function(){
$("#b").hide();
});
});
</script>
</body>
</head>
What's wrong with it?
That should work, but I'd strongly suggest you research the HTML for a valid document, i.e. </head> must appear before <body>.
Along with what alex said, it's possible that Chrome is blocking your request to the CDN-hosted jQuery. You can either give that domain valid permissions in manifest.json or simply download the copy of jQuery and store it locally.
If you are using SSL for your site, then you should serve your javascript/js file via https, else chrome will block it and causing your site's feature that is using that script not working.
I noticed that you are using http to call jquery from google cdn. May be it is causing that problem.

Categories