changing browser priority of js file in scritps - javascript

I'm using a script code for the subscription of web push on WordPress website. This is the code but the priority is low, and I want to be as high or even the highest.
advScriptTag = document.createElement("script");
var now = new Date();
advScriptTag = Object.assign(advScriptTag, {
src: "https://webp.com/api/webpush/gsgfvbvdfdf.js?site="+ location.host + "&ver=" + now.getFullYear() + now.getMonth() + now.getDate() + now.getHours()
});
document.head.appendChild(advScriptTag);
I want to load the API file priority as high.

Related

Creating outlook email with Javascript and formatting the email body

Before getting too far into the what I need, I want to start out that my HTML is not connected to an internal/external server (from what I have read this may be limiting my options). I really am using it as an HTML form that my employees will be able to have a standard template when sending emails. I am still really new to this so I apologize in advance if I forgot to include some important information.
I have a form that my employees will have to fill out and click a generate email button. I have everything working except the ability to format the email body. What I need to do is Bold all the Text before the answers. (IE Region: selection here) I have shorten my code to spare you how inexperienced I am.
function sendEmail() {
let reg1 = "<b>Region Name:</b> "
let reg2 = "Region Name: "
let a = document.getElementById("reg").value;
let m_to = " Main Email Addresses here"
let m_cc = "CC Email Addresses here"
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
document.location.href = "mailto:" + encodeURIComponent(m_to) + "?cc=" + encodeURIComponent(m_cc)
+ " &subject=DEAP Activation " + encodeURIComponent(b) +": NE Region " + today
+ " "
+ "&body="
+ "%0D%0A%0D%0A"
+ encodeURIComponent(bl) + "%0D%0A%0D%0A"
+ "Region Name: " + encodeURIComponent(a) + "%0D%0A"
}
I have tried:
+ reg1 + encodeURIComponent(a) but that will display as <b>Region Name:</b>
reg2.bold() + encodeURIComponent(a) but that yielded the same result as above
reg2.bold + encodeURIComponent(a) that displayed as function bold() { [native code] }
I also tried to change + "&body=" to + "&htmlbody=" but that stopped the email form generating
Any help would be greatly appreciated

Time Stamps on Omegle Using Greasemonkey/Tampermonkey

I was wondering if it was possible to add live time stamps to omegle using greasemonkey.
I did some digging up and found the function to add time but I got no experience with javascript and was not sure where am I supposed to add the code.
This is the code I found: http://stackoverflow.com/questions/10211145/getting-current-date-and-time-in-javascript
function getCurrentTime() {
var currentdate = new Date();
var datetime = currentdate.getDate() + "/"
+ (currentdate.getMonth()+1) + "/"
+ currentdate.getFullYear() + " # "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
return datetime;
}
I would appreciate it if someone could point me to the right direction (if this is even possible in the first place)
Here's what I've been using so far. It's terribly hacky but works fine.
if (!window.Timestamp$Element)
Timestamp$Element = Element;
Element = function (a, b) {
let elem = Timestamp$Element(a, b);
if (typeof a == "string" && b && (b["class"] == "youmsg" || b["class"] == "strangermsg")) {
let stamp = new Timestamp$Element("span", {"class": "timestamp"});
stamp.appendText(new Date().toLocaleTimeString() + " ");
elem.grab(stamp);
}
return elem;
};
Object.assign(Element, Timestamp$Element);
Or as a fully-cooked userscript with XrayWrapper handling and such, https://pastebin.com/czc0aLbG

CasperJS running out of memory

I'm running the following script with CasperJS and after about 1/3rd of the way through the array it starts running out of swap space and the machine becomes extremely slow. What am i doing wrong here?
searchPages is an array of 54 numbers corresponding to a URL value for a search page.
casper.each(searchPages,function(casper,index){
loadSearch(casper,index);
});
function loadSearch(casper,index){
var currentTime = new Date();
var month = currentTime.getMonth() + 2;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var dateStart = month + "/" + day + "/" + year;
month = currentTime.getMonth() + 3;
var dateEnd = month + "/" + day + "/" + year;
casper.thenOpen(url,function(){
var myfile = "data-"+year + "-" + month + "-" + day+".html";
this.evaluate(function(j) {
document.querySelector('select[name="searchParameters.localeId"]').selectedIndex = j;
},index);
this.evaluate(function(start) {
$("#leaveDate").val(start);
},dateStart);
this.evaluate(function(end) {
$("#returnDate").val(end);
},dateEnd);
this.evaluate(function() {
$("#OSB_btn").click();
});
this.waitForSelector('#destinationForPackage', function() {
if (this.exists('#destinationForPackage')){
var name = casper.evaluate(function() {
return $("#destinationForPackage option[value='" + $("#destinationForPackage").val() + "']").text()
});
if (name != "Going To"){
if (name == null){
console.log("it's null");
}else{
name = name.replace("/","_");
casper.capture('Captures/Searches/search_' + name + '.jpg');
console.log("Capturing search_" + name);
}
}
}else{
console.log("Still doesn't exist...retry");
loadSearch(casper,index);
}
},function(){
console.log("Search page timed-out.");
},20000);
});
}
And it adds about 3GB per loop.
Well turns out this is a very well-known issue with PhantomJS. 3+ years as an open bug and apparently it has something to do with QT Webkit. Nonetheless, i was able to solve it by closing each page during the loop and re-opening a new Phantom page. It's a bit of a hacky work-around, but the memory consumption is far less. However, after about 200 pages, it still has a pretty high memory usage (1GB+). So, i break up my scripts into blocks of 200 and just start the next one upon completion. Here is the finished product that completes successfully without too much memory usage. It uses less on MacOS than Windows for some reason.
casper.start(url,function(){
this.echo('continuing captures...');
}).each(searchPages,function(casper,index){
loadSearch(this,index);
});
function loadSearch(casper,index){
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate() + 1;
var year = currentTime.getFullYear();
var dateStart = month + "/" + day + "/" + year;
var fortnightAway = new Date(+new Date + 12096e5);
var dateEnd = fortnightAway.getMonth() + 1 + "/" + fortnightAway.getDate() + "/" + fortnightAway.getFullYear();
casper.page.close();
casper.page = require('webpage').create();
casper.thenOpen(url,function(){
var myfile = "data-"+year + "-" + month + "-" + day+".html";
this.evaluate(function(j) {
document.querySelector('select[name="searchParameters.localeId"]').selectedIndex = j;
},index);
this.evaluate(function(start) {
$("#leaveDate").val(start);
},dateStart);
this.evaluate(function(end) {
$("#returnDate").val(end);
},dateEnd);
this.evaluate(function() {
$("#OSB_btn").click();
});
this.waitForSelector('#destinationForPackage', function() {
if (this.exists('#destinationForPackage')){
var name = casper.evaluate(function() {
return $("#destinationForPackage option[value='" + $("#destinationForPackage").val() + "']").text()
});
if (name != "Going To"){
if (name == null){
console.log("it's null");
}else{
name = name.replace("/","_");
name = name.replace("/","_");
casper.capture('Captures/Searches/search_' + name + '.jpg');
console.log("Capturing search_" + name);
}
}
}else{
console.log("Search failed to load. Retrying");
loadSearch(casper,index);
}
},function(){
console.log("Search page timed-out. Retrying");
loadSearch(casper,index);
},20000);
});
}
There might be a better solution to the original issue, but for a quick fix on running out of memory, try setTimeout to make the recursive call without winding up the stack...
setTimeout(() => loadSearch(casper,index), 0);
(This idea assumes that the memory issue is the result of too much recursive depth over a long wait time).

Javascript - Unterminated string constant

When I run the website, it says "Error: Unterminated String Constant". The code that it says is the problem is:
var icsdate = currentDate.getFullYear() + currentDate.getMonth() + currentDate.getDate() + "T" + currentDate.getHours() + currentDate.getMinutes() + currentDate.getSeconds() + "Z";
The full script is:
<script type="text/javascript">
var currentDate = new Date();
var icsdate = currentDate.getFullYear() + currentDate.getMonth() + currentDate.getDate() + "T" + currentDate.getHours() + currentDate.getMinutes() + currentDate.getSeconds() + "Z";
var inonehour = currentDate.getHours() + 1;
var icsenddate = currentDate.getFullYear() + currentDate.getMonth() + currentDate.getDate() + "T" + inonehour + currentDate.getMinutes() + currentDate.getSeconds() + "Z";
var location = "USA"
var msgData1 = icsdate;
var msgData2 = icsenddate;
var msgData3 = location;
var icsMSG = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Our Company//NONSGML v1.0//EN\nBEGIN:VEVENT\nUID:me#google.com\nDTSTAMP:20120315T170000Z\nATTENDEE;CN=My Self ;RSVP=TRUE:MAILTO:me#gmail.com\nORGANIZER;CN=Me:MAILTO::me#gmail.com\nDTSTART:" + msgData1 +"\nDTEND:" + msgData2 +"\nLOCATION:" + msgData3 + "\nSUMMARY:Our Meeting Office\nEND:VEVENT\nEND:VCALENDAR";
$('.button').click(function () {
window.open("data:text/calendar;charset=utf8," + escape(icsMSG));
});
</script>
location is a readonly property in JavaScript
The exact line of code you've singled out itself doesn't give the "Error: Unterminated String Constant" error message, however specifying var location = "USA" in the global scope will attempt to override the readonly window.location object and will attempt to redirect your browser to the page /USA, and I imagine that is possibly what's affecting your code here.
You should rename location to something else.
For example, running the following code snippet in Chrome...
var location = "USA";
...will give the following error message:
In other browsers it'll possibly throw different errors, such as the one you're getting on whichever browser it is you're using.

Javascript runs in browser, not in phonegap app with cordova

This code below is for a form that needs to save some values into local storage, I have got it to work in a browser, but when I load this thing up in xcode/cordova, it wont fire the function. I tried debugging, hence the many, many alerts, I tried in DWCS6 with live view, but no avail, I can't seem to get it to run. Can you please find the error?
The function saveSpanning() has an if loop and when it goes out of the if-then-else loop it doesn't continue in xcode simulator. In the browser it does continue.
update: what is does: there's a slider, that produces a value, this value needs to be saved in local storage along with two other values (generated by JavaScript), namely: the date and the record count. So three values in all.
The script runs, using jquery mobile, the button to start this function works, I have use document.ready instead onBodyLoad, it basically works, but the function saveSpanning just does not go further in phonegap/ios/xcode simulator or device.
function saveSpanning() {
alert("saveSpanning gestart!");
var inputSpanning = document.getElementById("valSliderSpanning").value;
alert("input spanning = " + inputSpanning);
//For Time
var mes_time = document.getElementById("tijdSpanning").value;
var mestimearr = mes_time.split(":");
//For Date
var mes_date = document.getElementById("datumSpanning").value;
var mesdatearr = mes_date.split("-");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
var curr_hours = d.getHours();
var curr_min = d.getMinutes();
var curr_sec = d.getSeconds();
//newDate = curr_year + "/" + curr_month + "/" + curr_date + " " + curr_hours + ":" + curr_min + ":" + curr_sec
// origienel opmaak datum newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2] + " " + mestimearr[0] + ":" + mestimearr[1] + ":00";
newDate = mesdatearr[0] + "/" + mesdatearr[1] + "/" + mesdatearr[2];
alert("deze datum wordt opgelsage: " + newDate);
//var itemId = newDate.getTime(); //creates a unique id with the milliseconds since January 1, 1970
var itemId = "spanningKey";
var values = new Array();
values.push(newDate); //push each value into our values array
values.push(inputSpanning); //push each value into our values array
//alert(inputSpanning);
var spanningCountVal = localStorage.getItem('spanning_count');
//alert(spanningCountVal);
if (spanningCountVal == null) {
spanningCountVal = 1;
alert("spanningCountVal was null, en wordt dus nu 1: " + spanningCountVal);
}
else {
spanningCount = parseInt(spanningCountVal) + 1;
alert("zit nu in de else loop: " + spanningCount);
}
alert("uit de ifthenelseloop, spanningCount = " + spanningCount);
itemId = itemId + '-rec-' + spanningCount;
alert("itemid: " + itemId);
alert("spanningCountVal: " + spanningCount);
localStorage.setItem("spanning_count", spanningCount); //store the item in the database
localStorage.setItem(itemId, values.join("|")); //store the item in the database
alert("Successfully Saved.");
}
$(document).ready(function() {
$("#button").click(function() {
alert("hallo functie");
});
$("p").text("The DOM is now loaded and can be manipulated.");
$('#button2').click(function() {
alert('Button has been clicked');
});
$('#knopje').click(function() {
saveSpanning();
});
});​
document.ready is called when all the DOM Elements are loaded, and accessible. In a browser this typically is a good time to start executing code that requires the DOM to be ready.
However in Phonegap/Cordova, there are a number of steps that run after the document.ready event is fired, including connecting to the debugging console.
Instead, you'll need to wait for the deviceready event to know when phonegap/cordova is fully loaded and ready to be executed. Inside $(document).on('ready') you'll need to add an event listener for deviceready which should fire your methods.

Categories