PhantomJS get JS refresh - javascript

I need to check every content change on a web site.
I wrote a PhantomJS function to get the whole web site. After receiving the web site I parse every row to get the data.
Now I want to hold the connection and receive all JS-updates.
What need I do to get the JS-updates?
I hope my question is clear.
Here some code explanations:
Pseudo HTML Website
<html>
<head>...</head>
<body>
<div class="value-123" onload="(function() { setInterval(retrieveData(...), 5000); })">
<!-- If retrieveData finished, VALUE will change from e.g. 1.23 to 4.235 -->
VALUE
</div>
</body>
</html>
My PhantomJS Code:
const string cEndLine = "All output received";
var sb = new StringBuilder();
var p = new PhantomJS();
p.OutputReceived += (sender, e) =>
{
if (e.Data == cEndLine)
{
callBack(sb.ToString());
}
else
{
sb.AppendLine(e.Data);
}
};
p.RunScript(#"
var page = require('webpage').create();
page.settings.loadImages = false;
page.viewportSize = { width: 1920, height: 1080 };
page.onLoadFinished = function(status) {
if (status=='success') {
setTimeout(function() {
console.log(page.content);
console.log('" + cEndLine + #"');
phantom.exit();
}," + waitAfterPageLoad.TotalMilliseconds + #");
}
};
var url = '" + url + #"';
page.open(url);", new string[0]);

Related

MediaInfoLib JavaScript with MVC 5 .NET C#

I have a website developed in C# MVC5 .NET and I need to use de MediaInfolib DLL javascript version. I am not able to load the .wasm file.
This is my .cshtml code:
<script type="text/javascript">
// Load the WebAssembly MediaInfo module if the browser supports it,
// otherwise load the asmjs module
var wasm_path ="#Url.Content("~/")" + "lib/MediaInfo_DLL/";
var MediaInfoJs = document.createElement('script');
if ('WebAssembly' in window) {
MediaInfoJs.src = wasm_path + "MediaInfoWasm.js";
} else {
MediaInfoJs.src = wasm_path + "MediaInfo.js";
}
document.body.appendChild(MediaInfoJs);
// Continue initialization
MediaInfoJs.onload = function () {
var MediaInfoModule, MI, processing = false, CHUNK_SIZE = 1024 * 1024;
var finish = function() {
MI.Close();
MI.delete();
processing = false;
}
...
MediaInfoModule = MediaInfoLib(
{'locateFile': function(path, prefix) {return wasm_path + path; }},
{ 'postRun': function () {
console.debug('MediaInfo ready');
// Information about MediaInfo
document.getElementById('result').innerText = 'Info_Parameters:\n';
document.getElementById('result').innerText += MediaInfoModule.MediaInfo.Option_Static('Info_Parameters') + '\n\n';
document.getElementById('result').innerText += 'Info_Codecs:\n';
document.getElementById('result').innerText += MediaInfoModule.MediaInfo.Option_Static('Info_Codecs') + '\n';
// Get selected file
var input = document.getElementById('input');
input.onchange = function() {
if(input.files.length > 0) {
document.getElementById('result').innerText = "Processing...";
parseFile(input.files[0], showResult);
}
}
}
});
};
</script>
I have these warnings in the MediaInfoWasm.js and the page doesn`t work:
MediaInfoWasm.js:19 failed to asynchronously prepare wasm: failed to
load wasm binary file at '/lib/MediaInfo_DLL/MediaInfoWasm.wasm'
Module.c.printErr.c.printErr # MediaInfoWasm.js:19 MediaInfoWasm.js:19
failed to load wasm binary file at
'/lib/MediaInfo_DLL/MediaInfoWasm.wasm' Module.c.printErr.c.printErr #
MediaInfoWasm.js:19
Has somebody used this MediaInfolib javascript version with MVC5?
Thanks in advance.
María José.

Can't figure out why my phantomJS script isn't showing the status and taking a picture of the page

I am not getting a status of the website nor an error when executing the code when using the URL that is being generated. However, when I use 'http://google.com', the status shows and it takes a screenshot of the page. The URL that is being used is a correct one, when I open the URL in browser it is the Page I want. It seems like the page is unreachable but I am not getting an error nor a status update.
var size = 9;
var model = 'CQ2626';
function URL(size, model){
var baseSize = 580;
var shoeSize = size - 6.5;
shoeSize *= 20;
rawSize = shoeSize + baseSize;
var URL = 'http://www.adidas.com/us/' + model + '.html?forceSelSize=' + model + '_' + rawSize;
return URL
};
console.log(URL(size, model));
var page = require('webpage');
var webPage = page.create();
webPage.open(URL(size, model), function(status){
console.log("Status: " + status);
if(status === "success") {
webPage.render('example.png');
}
phantom.exit();
});

PhantomJS Issue with Loading Webpage: "Connection closed"'

I'm working with PhantomJS and everything that I've done works fine, but I don't know what happened bit time ago. The URL doesn't open in PhantomJS. The URL is domain and the code is as follows:
var page = require('webpage').create(),fs = require('fs'), args=require('system').args;
// var url = 'http://fotogena.co';
var url = 'https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1';
page.viewportSize = { width: 400, height: 400 };
page.open(url, function (status) {
if(status==='success'){
setTimeout(loadScripts, 1500)
}else
console.log(
"Error opening url \"" + page.reason_url
+ "\": " + page.reason
);
});
page.onResourceError = function(resourceError) {
console.error('-----',resourceError.url + ': ' + resourceError.errorString, 'faber acá toy');
phantom.exit(1);
};
function loadScripts(){
setTimeout(function(){
page.render('./probandoAntecedentes.jpg')
phantom.exit(1);
},1000)
}
The console displays the following:
https://www.procuraduria.gov.co/CertWEB/Certificado.aspx?tpo=1: Connection closed
I searched for possible solutions and put --ssl-protocol=any and other options, but they didn't work.
I'm using PhantomJS 2.1.1

Unable to receive parameter in query string when redirected to other page

Hi I am developing one application in java-script. I have two pages default.aspx and addnewitem.aspx. there is one html table in default.aspx and one button. When i click on button i want to redirect to addnewitem.aspx page. I have some parameters to send in query string. I am able to redirect to addnewitem.aspx but page not found error i am getting. I am not sure why i am getting page not found error. I am trying as below.
function getValues() {
var Title = "dfd";
var PrimarySkills = "fdfd";
var SecondarySkills = "dfdf";
var url = "http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=" + encodeURIComponent($(Title)) + "&PrimarySkills=" + encodeURIComponent($(PrimarySkills)) + "&SecondarySkills=" + encodeURIComponent($(SecondarySkills));
window.location.href = url;
}
I am checking querystring in addnewitem.aspx as below.
<script type="text/javascript">
var queryString = new Array();
$(function () {
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["Title"] != null && queryString["PrimarySkills"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Title:</b> " + queryString["Title"] + " <b>PrimarySkills:</b> " + queryString["PrimarySkills"] + " <b>SecondarySkills:</b> " + queryString["SecondarySkills"];
$("#lblData").html(data);
alert(data);
}
});
</script>
"http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=%5Bobject%20Object%5D&PrimarySkills=%5Bobject%20Object%5D&SecondarySkills=%5Bobject%20Object%5D"
I tried lot to fix this. May i know where i am doing wrong? Thanks for your help.
You should use the relative path in your url instead of hard coding the entire folder structure, which is probably incorrect since you are getting a 404. And you need to change the url every time you publish the site to the hosting enviroment when you hard code it like that.
So change
var url = "http://sites/APPSTEST/JobRequisitionApp/Pages/AddNewItem.aspx?Title=...
into
var url = "/AddNewItem.aspx?Title=...
if both the pages are in the same folder. Should AddNewItem.aspx be located in the Pages folder, you have to add that folder of course: var url = "/Pages/AddNewItem.aspx?Title=...

Chrome extension : Stuck at injecting script won't execute js

I am trying to build a chrome extension. Which would do some search on the page and post the results to the extensions.
I am having a hard time running this. Whenever I try to run the extension it is just stuck on Injecting Script.
my re.js
function printDetails(document_r) {
var test = document_r.body;
var text = test.innerText;
var delim="^^ validatedCache :";
var endlim="</site>";
var idx = text.indexOf(delim);
var endInd=text.indexOf(endlim);
var tag = "accountName";
var regex = "<" + tag + ">(.*?)<\/" + tag + ">";
var regexg = new RegExp(regex,"g");
var matches = [];
while (match = regexg.exec(text.substring(idx+delim.length,endInd))) matches.push("Account Name::::::"+match[1]);
return matches;
}
chrome.extension.sendMessage({
action: "getSource",
source: "\n\n\n DETAILS>>>>>\n\n"+printDetails(document)
});
selection.js
chrome.extension.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
message.innerText = request.source;
}
});
function onWindowLoad() {
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {
file: "re.js"
}, function() {
// If you try and inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.extension.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.extension.lastError.message;
}
});
}
window.onload = onWindowLoad;
popup.html
<!DOCTYPE html>
<html style=''>
<head>
<script src='selection.js'></script>
</head>
<body style="background-image:url(12.png);width:400px; border: 2px solid black;background-color:white;">
<div id='message'>Injecting Script....</div>
</body>
</html>
I know there is some problem with these 2 lines only.
var test = document_r.body;
var text = test.innerText;
What I wan't is to extract the webpage ( contents ) into a string which I am hopefully doing by the above two lines of code.
Then do some string manipulation on the code.If I run directly this code in a console with a dummy string . I can execute it so figure something is wrong with these two lines.
My extension is stuck on " Injecting Script..."
Some help would be appreciated.
PS:yes I was able to run it earlier with the same code but somehow it doesn't run now.

Categories

Resources