Modifying remote JavaScripts as they load with CefSharp? - javascript

I am building a custom browser as part of an interface to a remote website. Their GUI sucks so I'm doing some JavaScript hacks to make it look better.
Currently, to make modifications to their UI I am using the following GreaseMonkey script (on Firefox):
// ==UserScript==
// #name winman-load
// #namespace winman
// #description stuff to do when winman.js loads
// #include https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
// #version 1
// #grant none
// #run-at document-start
// ==/UserScript==
document.addEventListener("beforescriptexecute", function(e) {
src = e.target.src;
content = e.target.text;
//console.log("src: " + src);
if (src.search("winman.js") > -1) {
console.info("============ new winman ===========\n" + src);
var newContent = "";
$.ajax({
async: false,
type: 'GET',
url: '/script/winman.js',
success: function(data) {
newContent = data.replace('pos += currentPos;', 'pos += currentPos + 100;');
newContent = newContent.replace('var enable = false;', 'var enable = true;');
newContent = newContent.replace('var available = true;', 'var available = false;');
}
});
// Stop original script
e.preventDefault();
e.stopPropagation();
unsafeWindow.jQuery(e.target).remove();
var script = document.createElement('script');
script.textContent = newContent;
(document.head || document.documentElement).appendChild(script);
script.onload = function() {
this.parentNode.removeChild(this);
}
}
});
I want to be able to do something if this nature with CefSharp, where I can modify the scripts on-the-fly as the browser loads the page.

Ok I figured this out. You create a RequestHandler with a method like this:
IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) {
var url = new Uri(request.Url);
if (request.Url.Equals(scriptToUpdate, StringComparison.OrdinalIgnoreCase)) {
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(search1, replace1);
dictionary.Add(search2, replace2);
return new FindReplaceResponseFilter(dictionary);
}
return null;
}
And then for the multiple search/replace you make a FindReplaceResponseFilter:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CefSharp;
namespace CefFilters {
public class FindReplaceResponseFilter : IResponseFilter {
private static readonly Encoding encoding = Encoding.UTF8;
/// <summary>
/// The portion of the find string that is currently matching.
/// </summary>
private int findMatchOffset;
/// <summary>
/// Overflow from the output buffer.
/// </summary>
private readonly List<byte> overflow = new List<byte>();
/// <summary>
/// Number of times the the string was found/replaced.
/// </summary>
private int replaceCount;
private Dictionary<string, string> dictionary;
public FindReplaceResponseFilter(Dictionary<string, string> dictionary) {
this.dictionary = dictionary;
}
bool IResponseFilter.InitFilter() {
return true;
}
FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten) {
// All data will be read.
dataInRead = dataIn == null ? 0 : dataIn.Length;
dataOutWritten = 0;
// Write overflow then reset
if (overflow.Count > 0) {
// Write the overflow from last time.
WriteOverflow(dataOut, ref dataOutWritten);
}
// Evaluate each character in the input buffer. Track how many characters in
// a row match findString. If findString is completely matched then write
// replacement. Otherwise, write the input characters as-is.
for (var i = 0; i < dataInRead; ++i) {
var readByte = (byte) dataIn.ReadByte();
var charForComparison = Convert.ToChar(readByte);
if (replaceCount < dictionary.Count) {
var replace = dictionary.ElementAt(replaceCount);
if (charForComparison == replace.Key[findMatchOffset]) {
//We have a match, increment the counter
findMatchOffset++;
// If all characters match the string specified
if (findMatchOffset == replace.Key.Length) {
// Complete match of the find string. Write the replace string.
WriteString(replace.Value, replace.Value.Length, dataOut, ref dataOutWritten);
// Start over looking for a match.
findMatchOffset = 0;
replaceCount++;
}
continue;
}
// Character did not match the find string.
if (findMatchOffset > 0) {
// Write the portion of the find string that has matched so far.
WriteString(replace.Key, findMatchOffset, dataOut, ref dataOutWritten);
// Start over looking for a match.
findMatchOffset = 0;
}
}
// Write the current character.
WriteSingleByte(readByte, dataOut, ref dataOutWritten);
}
if (overflow.Count > 0) {
//If we end up with overflow data then we'll need to return NeedMoreData
// On the next pass the data will be written, then the next batch will be processed.
return FilterStatus.NeedMoreData;
}
// If a match is currently in-progress we need more data. Otherwise, we're
// done.
return findMatchOffset > 0 ? FilterStatus.NeedMoreData : FilterStatus.Done;
}
private void WriteOverflow(Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Maximum number of bytes we can write into the output buffer.
var maxWrite = Math.Min(overflow.Count, remainingSpace);
// Write the maximum portion that fits in the output buffer.
if (maxWrite > 0) {
dataOut.Write(overflow.ToArray(), 0, (int) maxWrite);
dataOutWritten += maxWrite;
}
if (maxWrite < overflow.Count) {
// Need to write more bytes than will fit in the output buffer.
// Remove the bytes that were written already
overflow.RemoveRange(0, (int) (maxWrite - 1));
}
else {
overflow.Clear();
}
}
private void WriteString(string str, int stringSize, Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Maximum number of bytes we can write into the output buffer.
var maxWrite = Math.Min(stringSize, remainingSpace);
// Write the maximum portion that fits in the output buffer.
if (maxWrite > 0) {
var bytes = encoding.GetBytes(str);
dataOut.Write(bytes, 0, (int) maxWrite);
dataOutWritten += maxWrite;
}
if (maxWrite < stringSize) {
// Need to write more bytes than will fit in the output buffer. Store the
// remainder in the overflow buffer.
overflow.AddRange(encoding.GetBytes(str.Substring((int) maxWrite, (int) (stringSize - maxWrite))));
}
}
private void WriteSingleByte(byte data, Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Write the byte to the buffer or add it to the overflow
if (remainingSpace > 0) {
dataOut.WriteByte(data);
dataOutWritten += 1;
}
else {
// Need to write more bytes than will fit in the output buffer. Store the
// remainder in the overflow buffer.
overflow.Add(data);
}
}
public void Dispose() {}
}
}
Keep in mind that this iterates through left to right, top to bottom, so make sure your search and replaces are in the right order.

Related

Problem transferring a json string from the client to the esp8266 server

As part of my WLAN Thermometer project, I am planning a small file management for the files stored on the ESP. In this context, I need to transfer a list of file names from the client to the server. Because there is also the wonderful ArduinoJSON library for the ESP 8266, I would like to pass the data as a JSON object. The first excerpt from the scripts.js of my webpage shows how to create the filelist (contains all available files at ESP Filesystem) and compile and transfer the deletelist (whose elements should be deleted).
let fileID = 0
for (i = 2; i < FDatas.length; i += 2)
{
let fileInfo = {
name: FDatas[i],
size: FDatas[i+1],
fileID: fileID,
marked: false};
fileList.push(fileInfo);
};
}
function deleteFiles() {
let deleteFileList = [];
let fileID = 0;
for (let i = 0; i < fileList.length; i++) {
if (fileList[i].marked == true) {
let keyname = 'fileID_' + String(fileID);
fileID += 1;
let newEntry = {[keyname]:fileList[i].name}
deleteFileList.push(newEntry);
}
}
if (deleteFileList.length > 0) {
var xhttp = new XMLHttpRequest();
var formData = JSON.stringify(deleteFileList);
xhttp.open("POST", "/deleteFiles");
xhttp.send(formData);
}
}
On the server side, communication is organized as follows:
In the setup part of the arduino code:
webserver.on("/deleteFiles", HTTP_POST, deleteFiles);
In the handler:
void deleteFiles() {
String input = webserver.arg("plain");
Serial println(input);
DynamicJsonDocument doc(2048);
DeserializationError err = deserializeJson(doc, input);
if (err) {
Serial.println(F("deserializeJson() failed with code "));
Serial.println(err.f_str());
}
JsonObject obj = doc.as<JsonObject>();
// Loop through all the key-value pairs in obj
for (JsonPair p : obj) {
Serial.println(p.key().c_str());
if (p.value().is<const char*>()) {
auto s = p.value().as<const char*>();
Serial.println(s);
}
}
webserver.send(200);
}
The result of these efforts is sobering. Nevertheless, the Serial.println(input); - command outputs the following,
[{"fileID_0":"/settings.json"},{"fileID_1":"/tdata.js"},{"fileID_2":"/scripts.js"}]
the passage through the JSON object does not result in key value pairs.
Where is my mistake? Thank you very much for your good advice.
1. Udate:
After first comment (Thank You) I've changed the arduino-code to:
void deleteFiles() {
String input = webserver.arg("plain");
Serial.println(input);
DynamicJsonDocument doc(2048);
DeserializationError err = deserializeJson(doc, input);
if (err) {
Serial.println(F("deserializeJson() failed with code "));
Serial.println(err.f_str());
}
JsonArray arr = doc.to<JsonArray>();
for(JsonVariant v : arr) {
Serial.println(v.as<const char*>());
}
webserver.send(200);
}
Unfortunately, the result is the same. No result in the loop.
Your json object consists of an array(each element of an array is indexed by a number like 0, 1, 2...), and within the array, there are these 3 json objects. So to access the data of each array element, you do doc[0] and so on. You can then access each key value pair with doc[0]['key'] notation.
StaticJsonDocument<192> doc;
DeserializationError error = deserializeJson(doc, input);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
const char* element1 = doc[0]["fileID_0"]; // "/settings.json"
const char* element2 = doc[1]["fileID_1"]; // "/tdata.js"
const char* element3 = doc[2]["fileID_2"]; // "/scripts.js"

How to receive the height of DOM element when printing with wkhtmltopdf library?

When I try to get offsetHeight or of any DOM element with Javascript when printing with wkhtmltopdf library, the height is never determined and is always equal to 0. When I execute the same JS code in any browser it works correctly and results in a specific height of element.
I googled for a long time and I found out that it might be related with wkhtmltopdf in which the width and height of document and window are equal to 0. I tried to override the size of the body tag with CSS and override the viewport size with wkhtmltopdf configuration parameters, but the offsetHeight still results in 0.
Is there any known walkaround to receive height of DOM element when printing with wkhtmltopdf?
I use the latest stable version of the printing library (0.12.6)
I have used wkHtml2Pdf in the past.
My advice is to stop right now, because wkhtmltopdf uses a very old browser version, and you're likely to run into problems anyway. Also, wkHtmlToPdf doesn't work properly (and performance is crap).
Instead, you can use a much better option.
That option is to use the Chrome DevTools with the remote-debugging-protocol:
https://chromedevtools.github.io/devtools-protocol/
Which basically runs Chrome like this
chrome.exe --remote-debugging-port=9222
With optional
$"--user-data-dir=\"{directoryInfo.FullName}\"";
and
"--headless --disable-gpu";
Here's how I start the Chrome process on the server (C# Code)
public IChromeProcess Create(int port, bool headless)
{
string path = System.IO.Path.GetRandomFileName();
System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(
System.IO.Path.Combine(
System.IO.Path.GetTempPath(), path)
);
string remoteDebuggingArg = $"--remote-debugging-port={port}";
string userDirectoryArg = $"--user-data-dir=\"{directoryInfo.FullName}\"";
const string headlessArg = "--headless --disable-gpu";
// https://peter.sh/experiments/chromium-command-line-switches/
System.Collections.Generic.List<string> chromeProcessArgs =
new System.Collections.Generic.List<string>
{
remoteDebuggingArg,
userDirectoryArg,
// Indicates that the browser is in "browse without sign-in" (Guest session) mode.
// Should completely disable extensions, sync and bookmarks.
"--bwsi",
"--no-first-run"
};
if (false)
{
string proxyProtocol = "socks5";
proxyProtocol = "http";
proxyProtocol = "https";
string proxyIP = "68.183.233.181";
string proxyPort = "3128";
string proxyArg = "--proxy-server=\"" + proxyProtocol + "://" + proxyIP + ":" + proxyPort + "\"";
chromeProcessArgs.Add(proxyArg);
}
if (headless)
chromeProcessArgs.Add(headlessArg);
if(IsRoot)
chromeProcessArgs.Add("--no-sandbox");
string args = string.Join(" ", chromeProcessArgs);
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(ChromePath, args);
System.Diagnostics.Process chromeProcess = System.Diagnostics.Process.Start(processStartInfo);
string remoteDebuggingUrl = "http://localhost:" + port;
return new LocalChromeProcess(new System.Uri(remoteDebuggingUrl), () => DirectoryCleaner.Delete(directoryInfo), chromeProcess);
}
I used this C# library here to interface with the DevTools (via WebSockets):
https://github.com/MasterDevs/ChromeDevTools
If you use NodeJS on the server, you could use this:
https://github.com/cyrus-and/chrome-remote-interface
or for TypeScript:
https://github.com/TracerBench/chrome-debugging-client
In order to generate a PDF, you need to issue the PrintToPDF-Command:
Dim cm2inch As UnitConversion_t = Function(ByVal centimeters As Double) centimeters * 0.393701
Dim mm2inch As UnitConversion_t = Function(ByVal milimeters As Double) milimeters * 0.0393701
Dim printCommand2 As PrintToPDFCommand = New PrintToPDFCommand() With {
.Scale = 1,
.MarginTop = 0,
.MarginLeft = 0,
.MarginRight = 0,
.MarginBottom = 0,
.PrintBackground = True,
.Landscape = False,
.PaperWidth = mm2inch(conversionData.PageWidth),
.PaperHeight = mm2inch(conversionData.PageHeight) '
}
And to create a raster graphic, you need to issue the CaptureScreenshot-Command :
Dim screenshot As MasterDevs.ChromeDevTools.CommandResponse(Of CaptureScreenshotCommandResponse) = Await chromeSession.SendAsync(New CaptureScreenshotCommand With {
.Format = "png"
})
System.Diagnostics.Debug.WriteLine("Screenshot taken.")
conversionData.PngData = System.Convert.FromBase64String(screenshot.Result.Data)
Note that for the screenshot to work properly, you need to set the width and the height via the SetDeviceMetricsOverride-Command:
Await chromeSession.SendAsync(New SetDeviceMetricsOverrideCommand With {
.Width = conversionData.ViewPortWidth,
.Height = conversionData.ViewPortHeight,
.Scale = 1
})
You might have to put overflow:hidden on the HTML, or some sub-elements just so you don't screenshot the scrollbars ;)
By the way, if you need a specific version of Chrome for Windows (Chromium, because old Chrome versions are not available for security reasons), you can get them from the Chocolatey-Repository:
https://chocolatey.org/packages/chromium/#versionhistory
Here's my full test-code for reference (minus some classes)
Imports MasterDevs.ChromeDevTools
Imports MasterDevs.ChromeDevTools.Protocol.Chrome.Browser
Imports MasterDevs.ChromeDevTools.Protocol.Chrome.Page
Imports MasterDevs.ChromeDevTools.Protocol.Chrome.Target
Namespace Portal_Convert.CdpConverter
Public Class ChromiumBasedConverter
Private Delegate Function UnitConversion_t(ByVal value As Double) As Double
Public Shared Sub KillHeadlessChromes(ByVal writer As System.IO.TextWriter)
Dim allProcesses As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcesses()
Dim exeName As String = "\chrome.exe"
If System.Environment.OSVersion.Platform = System.PlatformID.Unix Then
exeName = "/chrome"
End If
For i As Integer = 0 To allProcesses.Length - 1
Dim proc As System.Diagnostics.Process = allProcesses(i)
Dim commandLine As String = ProcessUtils.GetCommandLine(proc)
If String.IsNullOrEmpty(commandLine) Then Continue For
commandLine = commandLine.ToLowerInvariant()
If commandLine.IndexOf(exeName, System.StringComparison.InvariantCultureIgnoreCase) = -1 Then Continue For
If commandLine.IndexOf("--headless", System.StringComparison.InvariantCultureIgnoreCase) <> -1 Then
writer.WriteLine($"Killing process {proc.Id} with command line ""{commandLine}""")
ProcessUtils.KillProcessAndChildren(proc.Id)
End If
Next
writer.WriteLine($"Finished killing headless chromes")
End Sub
Public Shared Sub KillHeadlessChromes()
KillHeadlessChromes(System.Console.Out)
End Sub
Private Shared Function __Assign(Of T)(ByRef target As T, value As T) As T
target = value
Return value
End Function
Public Shared Function KillHeadlessChromesWeb() As System.Collections.Generic.List(Of String)
Dim ls As System.Collections.Generic.List(Of String) = New System.Collections.Generic.List(Of String)()
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
Using sw As System.IO.StringWriter = New System.IO.StringWriter(sb)
KillHeadlessChromes(sw)
End Using
Using tr As System.IO.TextReader = New System.IO.StringReader(sb.ToString())
Dim thisLine As String = Nothing
While (__Assign(thisLine, tr.ReadLine())) IsNot Nothing
ls.Add(thisLine)
End While
End Using
sb.Length = 0
sb = Nothing
Return ls
End Function
Private Shared Async Function InternalConnect(ByVal ci As ConnectionInfo, ByVal remoteDebuggingUri As String) As System.Threading.Tasks.Task
ci.ChromeProcess = New RemoteChromeProcess(remoteDebuggingUri)
ci.SessionInfo = Await ci.ChromeProcess.StartNewSession()
End Function
Private Shared Async Function ConnectToChrome(ByVal chromePath As String, ByVal remoteDebuggingUri As String) As System.Threading.Tasks.Task(Of ConnectionInfo)
Dim ci As ConnectionInfo = New ConnectionInfo()
Try
Await InternalConnect(ci, remoteDebuggingUri)
Catch ex As System.Exception
If ex.InnerException IsNot Nothing AndAlso Object.ReferenceEquals(ex.InnerException.[GetType](), GetType(System.Net.WebException)) Then
If (CType(ex.InnerException, System.Net.WebException)).Status = System.Net.WebExceptionStatus.ConnectFailure Then
Dim chromeProcessFactory As MasterDevs.ChromeDevTools.IChromeProcessFactory = New MasterDevs.ChromeDevTools.ChromeProcessFactory(New FastStubbornDirectoryCleaner(), chromePath)
Dim persistentChromeProcess As MasterDevs.ChromeDevTools.IChromeProcess = chromeProcessFactory.Create(9222, True)
' await cannot be used inside catch ...
' Await InternalConnect(ci, remoteDebuggingUri)
InternalConnect(ci, remoteDebuggingUri).Wait()
Return ci
End If
End If
System.Console.WriteLine(chromePath)
System.Console.WriteLine(ex.Message)
System.Console.WriteLine(ex.StackTrace)
If ex.InnerException IsNot Nothing Then
System.Console.WriteLine(ex.InnerException.Message)
System.Console.WriteLine(ex.InnerException.StackTrace)
End If
System.Console.WriteLine(ex.[GetType]().FullName)
Throw
End Try
Return ci
End Function
Private Shared Async Function ClosePage(ByVal chromeSession As MasterDevs.ChromeDevTools.IChromeSession, ByVal frameId As String, ByVal headLess As Boolean) As System.Threading.Tasks.Task
Dim closeTargetTask As System.Threading.Tasks.Task(Of MasterDevs.ChromeDevTools.CommandResponse(Of CloseTargetCommandResponse)) = chromeSession.SendAsync(New CloseTargetCommand() With {
.TargetId = frameId
})
' await will block forever if headless
If Not headLess Then
Dim closeTargetResponse As MasterDevs.ChromeDevTools.CommandResponse(Of CloseTargetCommandResponse) = Await closeTargetTask
System.Console.WriteLine(closeTargetResponse)
Else
System.Console.WriteLine(closeTargetTask)
End If
End Function
Public Shared Async Function ConvertDataAsync(ByVal conversionData As ConversionData) As System.Threading.Tasks.Task
Dim chromeSessionFactory As MasterDevs.ChromeDevTools.IChromeSessionFactory = New MasterDevs.ChromeDevTools.ChromeSessionFactory()
Using connectionInfo As ConnectionInfo = Await ConnectToChrome(conversionData.ChromePath, conversionData.RemoteDebuggingUri)
Dim chromeSession As MasterDevs.ChromeDevTools.IChromeSession = chromeSessionFactory.Create(connectionInfo.SessionInfo.WebSocketDebuggerUrl)
Await chromeSession.SendAsync(New SetDeviceMetricsOverrideCommand With {
.Width = conversionData.ViewPortWidth,
.Height = conversionData.ViewPortHeight,
.Scale = 1
})
Dim navigateResponse As MasterDevs.ChromeDevTools.CommandResponse(Of NavigateCommandResponse) = Await chromeSession.SendAsync(New NavigateCommand With {
.Url = "about:blank"
})
System.Console.WriteLine("NavigateResponse: " & navigateResponse.Id)
Dim setContentResponse As MasterDevs.ChromeDevTools.CommandResponse(Of SetDocumentContentCommandResponse) = Await chromeSession.SendAsync(New SetDocumentContentCommand() With {
.FrameId = navigateResponse.Result.FrameId,
.Html = conversionData.Html
})
Dim cm2inch As UnitConversion_t = Function(ByVal centimeters As Double) centimeters * 0.393701
Dim mm2inch As UnitConversion_t = Function(ByVal milimeters As Double) milimeters * 0.0393701
Dim printCommand2 As PrintToPDFCommand = New PrintToPDFCommand() With {
.Scale = 1,
.MarginTop = 0,
.MarginLeft = 0,
.MarginRight = 0,
.MarginBottom = 0,
.PrintBackground = True,
.Landscape = False,
.PaperWidth = mm2inch(conversionData.PageWidth),
.PaperHeight = mm2inch(conversionData.PageHeight) '
}
'.PaperWidth = cm2inch(conversionData.PageWidth),
'.PaperHeight = cm2inch(conversionData.PageHeight)
If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.GetVersion) Then
Try
System.Diagnostics.Debug.WriteLine("Getting browser-version")
Dim version As MasterDevs.ChromeDevTools.CommandResponse(Of GetVersionCommandResponse) = Await chromeSession.SendAsync(New GetVersionCommand())
System.Diagnostics.Debug.WriteLine("Got browser-version")
conversionData.Version = version.Result
Catch ex As System.Exception
conversionData.Exception = ex
System.Diagnostics.Debug.WriteLine(ex.Message)
End Try
End If
If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.ConvertToImage) Then
Try
System.Diagnostics.Debug.WriteLine("Taking screenshot")
Dim screenshot As MasterDevs.ChromeDevTools.CommandResponse(Of CaptureScreenshotCommandResponse) = Await chromeSession.SendAsync(New CaptureScreenshotCommand With {
.Format = "png"
})
System.Diagnostics.Debug.WriteLine("Screenshot taken.")
conversionData.PngData = System.Convert.FromBase64String(screenshot.Result.Data)
Catch ex As System.Exception
conversionData.Exception = ex
System.Diagnostics.Debug.WriteLine(ex.Message)
End Try
End If
If conversionData.ChromiumActions.HasFlag(ChromiumActions_t.ConvertToPdf) Then
Try
System.Diagnostics.Debug.WriteLine("Printing PDF")
Dim pdf As MasterDevs.ChromeDevTools.CommandResponse(Of PrintToPDFCommandResponse) = Await chromeSession.SendAsync(printCommand2)
System.Diagnostics.Debug.WriteLine("PDF printed.")
conversionData.PdfData = System.Convert.FromBase64String(pdf.Result.Data)
Catch ex As System.Exception
conversionData.Exception = ex
System.Diagnostics.Debug.WriteLine(ex.Message)
End Try
End If
System.Console.WriteLine("Closing page")
Await ClosePage(chromeSession, navigateResponse.Result.FrameId, True)
System.Console.WriteLine("Page closed")
End Using ' connectionInfo
End Function ' ConvertDataAsync
Public Shared Sub ConvertData(ByVal conversionData As ConversionData)
ConvertDataAsync(conversionData).Wait()
End Sub
End Class
End Namespace
Note that if anyone is using C#, it's better to use this library:
https://github.com/BaristaLabs/chrome-dev-tools-runtime
which uses less external depencencies, and is NetCore. I used the other only because I had to backport it to an old framework version...

How to inject javascript in existing HTML response with node.js and cloudflare workers

I have a vanity URL pointing to a GitBook. GitBook doesn't support the insertion of arbitrary javascript snippets. At the moment GitBook has 4 "integrations" only.
I could route through my own VM server to accomplish this, but I have CloudFlare and I want to try out workers. (Javascript running at the CDN edge).
The CloudFlare worker environment makes header injection very easy, but there is no obvious way to do this.
It's important to process with a TransformStream so that processing is async and doesn't require memory buffering (for scalability and to minimise GC) - there's only a 5ms CPU time budget.
Overview:
To use for yourself, change the strings forHeadStart, forHeadEnd, and forBodyEnd.
This deferredInjection approach is the recommended way that minimises CPU time for the worker. It's more efficient because it only needs to parse the very start of the HTML. The other approach requires parsing of the whole head section for headInjection, and if you use bodyInjection it practically needs to parse the whole html response.
The deferredInjection approach works by injecting the content into the start of the head tag, then on the client-side at runtime your HTML content will be deployed to the desired places.
You can inject directly if needed using headInjection and/or bodyInjection. Uncommenting related code, including code in injectScripts, and setting the strings for tagBytes that will be encoded.
This solution will only parse HTML content types
This solution works directly on bytes (not strings) for better efficiency. Searching for the bytes of the end-tag strings.
You could potentially target more end-tags, but usually you don't need to target more than these two
Processes data with streaming (the whole HTML string is not cached in memory). This lowers peak memory usage and speeds up time to first byte.
Handles a rare edge case where the closing tag is on a text read boundary. I believe a boundary might occur every ~1000 bytes (TCP packets 1000-1500 bytes each), and this can vary due to gzip compression.
Keeps the injection parsing code separate for the code to simply forward the rest for clarity.
You can disable the second body-tag injector by commenting it out if you don't need it - that will speed up processing.
I have tested this exact code for myself and it works. There might be remaining bugs (depending on location of closing tag, and depending if your server replies with partial html templates (body only)). I may have fixed one today 2019-06-28
Code
addEventListener('fetch', event => {
event.passThroughOnException();
event.respondWith(handleRequest(event.request))
})
/**
* Fetch and log a request
* #param {Request} request
*/
async function handleRequest(request) {
const response = await fetch(request);
var ctype = response.headers.get('content-type');
if (ctype.startsWith('text/html') === false)
return response; //Only parse html body
let { readable, writable } = new TransformStream();
let promise = injectScripts(response.body, writable);
return new Response(readable, response);
}
let encoder = new TextEncoder('utf-8');
let deferredInjection = function() {
let forHeadStart = `<script>var test = 1; //Start of head section</script>`;
let forHeadEnd = `<script>var test = 2; //End of head section</script>`;
let forBodyEnd = `<script>var test = 3; //End of body section</script><button>click</button>`;
let helper = `
${forHeadStart}
<script>
function appendHtmlTo(element, htmlContent) {
var temp = document.createElement('div');
temp.innerHTML = htmlContent;
while (temp.firstChild) {
element.appendChild(temp.firstChild);
};
}
let forHeadEnd = "${ btoa(forHeadEnd) }";
let forBodyEnd = "${ btoa(forBodyEnd) }";
if (forHeadEnd.length > 0) appendHtmlTo(document.head, atob(forHeadEnd));
if (forBodyEnd.length > 0) window.onload = function() {
appendHtmlTo(document.body, atob(forBodyEnd));
};
</script>
`;
return {
forInjection: encoder.encode(helper),
tagBytes: encoder.encode("<head>"),
insertAfterTag: true
};
}();
// let headInjection = {
// forInjection: encoder.encode("<script>var test = 1;</script>"),
// tagBytes: encoder.encode("</head>"), //case sensitive
// insertAfterTag: false
// };
// let bodyInjection = {
// forInjection: encoder.encode("<script>var test = 1;</script>"),
// tagBytes: encoder.encode("</body>"), //case sensitive
// insertAfterTag: false
// }
//console.log(bodyTagBytes);
encoder = null;
async function injectScripts(readable, writable) {
let processingState = {
readStream: readable,
writeStream: writable,
reader: readable.getReader(),
writer: writable.getWriter(),
leftOvers: null, //data left over after a closing tag is found
inputDone: false,
result: {charactersFound: 0, foundIndex: -1, afterHeadTag: -1} //Reused object for the duration of the request
};
await parseForInjection(processingState, deferredInjection);
//await parseForInjection(processingState, headInjection);
//await parseForInjection(processingState, bodyInjection);
await forwardTheRest(processingState);
}
///Return object will have foundIndex: -1, if there is no match, and no partial match at the end of the array
///If there is an exact match, return object will have charactersFound:(tagBytes.Length)
///If there is a partial match at the end of the array, return object charactersFound will be < (tagBytes.Length)
///The result object needs to be passed in to reduce Garbage Collection - we can reuse the object
function searchByteArrayChunkForClosingTag(chunk, tagBytes, result)
{
//console.log('search');
let searchStart = 0;
//console.log(tagBytes.length);
//console.log(chunk.length);
for (;;) {
result.charactersFound = 0;
result.foundIndex = -1;
result.afterHeadTag = -1;
//console.log(result);
let sweepIndex = chunk.indexOf(tagBytes[0], searchStart);
if (sweepIndex === -1)
return; //Definitely not found
result.foundIndex = sweepIndex;
sweepIndex++;
searchStart = sweepIndex; //where we start searching from next
result.charactersFound++;
result.afterHeadTag = sweepIndex;
//console.log(result);
for (let i = 1; i < tagBytes.length; i++)
{
if (sweepIndex === chunk.length) return; //Partial match
if (chunk[sweepIndex++] !== tagBytes[i]) { result.charactersFound = 0; result.afterHeadTag = -1; break; } //Failed to match (even partially to boundary)
result.charactersFound++;
result.afterHeadTag = sweepIndex; //Because we work around the actual found tag in case it's across a boundary
}
if (result.charactersFound === tagBytes.length)
return; //Found
}
}
function continueSearchByteArrayChunkForClosingTag(chunk, tagBytes, lastSplitResult, result)
{
//console.log('continue');
//Finish the search (no need to check the last buffer at all)
//console.log('finish the search');
result.charactersFound = lastSplitResult.charactersFound; //We'll be building on the progress from the lastSplitResult
result.foundIndex = (-1 * result.charactersFound); //This won't be used, but a negative value is indicative of chunk spanning
let sweepIndex = 0;
result.afterHeadTag = 0;
for (let i = lastSplitResult.charactersFound; i < tagBytes.length; i++) //Zero-based
{
if (sweepIndex === chunk.length) return result; //So we support working on a chunk that's smaller than the tagBytes search size
if (chunk[sweepIndex++] !== tagBytes[i]) { result.charactersFound = 0; result.afterHeadTag = -1; break; }
result.charactersFound++;
result.afterHeadTag = sweepIndex;
}
}
function continueOrNewSearch(chunk, tagBytes, lastSplitResult, result)
{
//console.log('continueOrNewSearch');
if (lastSplitResult == null)
searchByteArrayChunkForClosingTag(chunk, tagBytes, result);
else
{
continueSearchByteArrayChunkForClosingTag(chunk, tagBytes, lastSplitResult, result);
if (result.charactersFound === tagBytes.length)
return result;
else
return searchByteArrayChunkForClosingTag(chunk, tagBytes, result); //Keep searching onward
}
}
async function parseForInjection(processingState, injectionJob)
{
if (processingState.inputDone) return; //Very edge case: Somehow </head> is never found?
if (!injectionJob) return;
if (!injectionJob.tagBytes) return;
if (!injectionJob.forInjection) return;
let reader = processingState.reader;
let writer = processingState.writer;
let result = processingState.result;
let tagBytes = injectionJob.tagBytes;
//(reader, writer, tagBytes, forInjection)
let lastSplitResult = null;
let chunk = null;
processingState.inputDone = false;
for (;;) {
if (processingState.leftOvers)
{
chunk = processingState.leftOvers;
processingState.leftOvers = null;
}
else
{
let readerResult = await reader.read();
chunk = readerResult.value;
processingState.inputDone = readerResult.done;
}
if (processingState.inputDone) {
if (lastSplitResult !== null) {
//Very edge case: Somehow tagBytes is never found?
console.log('edge');
throw 'tag not found'; //Causing the system to fall back to the direct request
}
await writer.close();
return true;
}
//console.log(value.length);
continueOrNewSearch(chunk, tagBytes, lastSplitResult, result)
//console.log(result);
if (result.charactersFound === tagBytes.length) //Complete match
{
//Inject
//console.log('inject');
if (result.foundIndex > 0)
{
let partValue = chunk.slice(0, result.foundIndex);
//console.log(partValue);
await writer.write(partValue);
}
console.log('injected');
if (parseForInjection.insertAfterTag)
{
await writer.write(injectionJob.forInjection);
await writer.write(injectionJob.tagBytes);
}
else
{
await writer.write(injectionJob.tagBytes);
await writer.write(injectionJob.forInjection);
}
let remainder = chunk.slice(result.afterHeadTag, chunk.length - 1);
processingState.leftOvers = remainder;
lastSplitResult = null;
return;
}
if (lastSplitResult !== null)
{
//console.log('no match over boundary');
//The remainder wasn't found, so write the partial match from before (maybe `<` or `</`)
let failedLastBit = injectionJob.tagBytes.slice(0, lastSplitResult.charactersFound);
await writer.write(failedLastBit);
lastSplitResult = null;
}
if (result.charactersFound === 0)
{
//console.log('not found')
await writer.write(chunk);
continue;
}
if (result.charactersFound < tagBytes.length)
{
//console.log('boundary: ' + result.charactersFound);
lastSplitResult = result;
let partValue = chunk.slice(0, result.foundIndex);
//console.log(partValue);
await writer.write(partValue);
continue;
}
}
}
async function forwardTheRest(processingState)
{
try
{
if (processingState.inputDone) return; //Very edge case: Somehow </head> is never found?
if (processingState.leftOvers)
{
chunk = processingState.leftOvers;
await processingState.writer.write(chunk);
}
processingState.reader.releaseLock();
processingState.writer.releaseLock();
await processingState.readStream.pipeTo(processingState.writeStream);
//Should there be an explicit close method called? I couldn't find one
}
catch (e)
{
console.log(e);
}
}
Further explanation of working directly with (utf-8) bytes:
Only working with byte values. This is possible at least by searching for the first distinctive utf-8 byte of a character (< 128 and > 192). But in this case, we're searching for </head> which is made up of lower-than-128 bytes, very easy to work with.
Given the nature of searching for utf-8 (which is the trickiest), this should work with ['utf-8', 'utf8', 'iso-8859-1', 'us-ascii']. You will need to change the snippet encoder to match.
This isn't thoroughly tested. The boundary case, didn't trigger for me. Ideally, we would have a testing rig for the core functions
thanks to Kenton Varda for challenging me
Please let me know if there's a CloudFlare workers way to do pipeTo in the forwardTheRest function
You might find continueOrNewSearch and the two sub-functions to be an interesting approach to finding multi-bytes across a chunk boundary. Up until the boundary we just count how many bytes are found. There's no need to keep those bytes (we know what they are). Then on the next chunk we continue where we left off. We always cut the array buffer around the header, and make sure we write the header bytes (using the tagBytes)

Getting Script on Top of the page so getting error like $ not define

i am use asp.net core code for popup and append html and js file in main view but i get error like $ not found if anyone know how to solve please help
My ActionFilter Code:-
private readonly IStoreContext _storeContext;
private readonly ISettingService _settingService;
private readonly ILogger _logger;
private readonly ILocalizationService _localizationService;
private readonly IWorkContext _workContext;
private readonly ITopicService _topicService;
private readonly INewsLetterSubscriptionService _newsLetterSubscriptionService;
#endregion
#region const
public PopupEngageFilterAttribute()
{
this._storeContext = EngineContext.Current.Resolve<IStoreContext>();
this._settingService = EngineContext.Current.Resolve<ISettingService>();
this._logger = EngineContext.Current.Resolve<ILogger>();
this._localizationService = EngineContext.Current.Resolve<ILocalizationService>();
this._workContext = EngineContext.Current.Resolve<IWorkContext>();
this._topicService = EngineContext.Current.Resolve<ITopicService>();
this._newsLetterSubscriptionService = EngineContext.Current.Resolve<INewsLetterSubscriptionService>();
}
#endregion
#region methods
public void PopupEngageOnResultExecuted(ActionExecutedContext filterContext)
{
var storeId = _storeContext.CurrentStore.Id;
LicenseImplementer licenseImplementer = new LicenseImplementer();
// load plugin settings
var _setting = _settingService.LoadSetting<PopupEngageSetting>(storeId);
var allStoreSettings = _settingService.LoadSetting<PopupEngageSetting>(0);
//check plugin is enabled or not
if (_setting.PopupEngageEnabled)
{
// check license
//if (!licenseImplementer.IsLicenseActive(allStoreSettings.LicenseKey, allStoreSettings.OtherLicenseSettings))
// return;
StringBuilder sb = new StringBuilder();
string bioepEngageScript = string.Empty;
string popupEngageView = string.Empty;
string popupEngageScript = string.Empty;
string newsLetterScript = string.Empty;
// get current customer
var customer = _workContext.CurrentCustomer;
// check customer cart
string customerCart = Convert.ToString(customer.HasShoppingCartItems);
// set cookie for customer cart
filterContext.HttpContext.Response.Cookies.Append("CustomerCart", customerCart, new CookieOptions() { Path = "/", HttpOnly = false, Secure = false });
if(customerCart == "True")
{
// get bioep script file
Stream bioepScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.bioep.min.js");
if (bioepScriptFile != null)
using (StreamReader reader = new StreamReader(bioepScriptFile))
{
bioepEngageScript = reader.ReadToEnd();
}
// get PopupEngage script
string path = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Script"), "PopupEngage.js");
if (File.Exists(path))
{
popupEngageScript = File.ReadAllText(path);
}
// check current customers role
var customerRole = customer.CustomerRoles.Where(x => x.Name == "Guests").FirstOrDefault();
if (customerRole != null)
{
// get Popup View file
string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngageNewsLetter.html");
if (File.Exists(popupEngageViewFile))
{
popupEngageView = File.ReadAllText(popupEngageViewFile);
}
// get NewsLetter Script file
Stream newsLetterScriptFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("Nop.Plugin.XcellenceIt.PopupEngage.Script.NewsLetter.js");
if (newsLetterScriptFile != null)
using (StreamReader reader = new StreamReader(newsLetterScriptFile))
{
newsLetterScript = reader.ReadToEnd();
}
}
else
{
// get Popup View file
string popupEngageViewFile = Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Environment.CurrentDirectory.ToString(), "Plugins"), "XcellenceIt.PopupEngage"), "Views"), "PopupEngage"), "PopupEngage.html");
if (File.Exists(popupEngageViewFile))
{
popupEngageView = File.ReadAllText(popupEngageViewFile);
}
}
var topicBody=string.Empty;
// get topic from settings
var topic = _setting.TopicName;
if (!string.IsNullOrEmpty(topic))
{
// get topic by system name
var topicRecord = _topicService.GetTopicBySystemName(topic);
if(topicRecord != null)
{
topicBody = topicRecord.Body;
}
}
// replace new line with slash and double coute with single coute
popupEngageView = popupEngageView.Replace(Environment.NewLine, String.Empty).Replace("\"", "'");
topicBody = topicBody.Replace(Environment.NewLine, String.Empty).Replace("\"", "'");
// append script
sb.Append("<script type=\"text/javascript\" src=\"/wwwroot/lib/jquery-1.10.2.min.js\">\n\t");
sb.Append(bioepEngageScript);
sb.Append(popupEngageScript);
sb.Append("$(\"" + popupEngageView + "\").insertAfter(\".newsletter\");");
sb.Append("$('.popupengage_popupmsg').html(\"" + topicBody + "\");");
sb.Append(newsLetterScript);
sb.Append("</script>\n");
var bytes = Encoding.ASCII.GetBytes(sb.ToString());
filterContext.HttpContext.Response.Body.WriteAsync(bytes,0, bytes.Length);
}
}
}
#endregion
file append in perfect way but it append script in top of the page before jquery. and that script append by string builder.Popup js example
if u are using jquery, make sure it is included before the script files that use jquery functionality;
For ex: if u have a js file named 'main.js' which has includes a line like $().forEach then your order of inclusion in the html file should be
<script>jquery.js </scrpt>
<script>main.js </scrpt>

Creating custom Cordova plugin for NfcV Tags

I need to read the Data off NfcV (ISO 15693) Tags, I already tried the Phonegap-Nfc Plugin from Chariotsolution, but it seems this plugin can only read the TagId and Type off NfcV tags, so i decided i could create my own custom plugin, I first foud the echo Hello World Plugin tutorial which worked fine and without problems, so I looked for Java NfcV reader and found this Java NfcV Reader.
From what I understood so far is, that I need to call the cordova.exec function and extend my Java class from CordovaPlugin, which is working.
I don't know Java since I am a Webdeveloper, which makes it a little hard for me.
This actually looks pretty good but i tried to implement it in the way of the Hello World echo example which didnt work out as planned.
I have now a plugin folder with
Hello.java
package org.apache.cordova.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
/**
* This class echoes a string called from JavaScript.
*/
public class Hello extends CordovaPlugin
{
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("hello")) {
String message = args.getString(0);
this.hello(message, callbackContext);
return true;
}
return false;
}
private void hello(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}
ReadNfcV.java from the link above
package org.apache.cordova.plugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import org.apache.http.util.ByteArrayBuffer;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.NfcV;
import android.nfc.tech.TagTechnology;
//import android.os.Parcelable;
import android.util.Log;
/**
* #author uhahn
*
*/
public class ReadNfcV extends CordovaPlugin implements TagTechnology {
protected NfcV mynfcv;
// protected Tag mytag; // can be retrieved through mynfcv
private final String TAG=this.getClass().getName();
protected final int maxtries=3;
protected boolean isTainted=true; // Tag info already read?
protected byte[] mysysinfo=null; // NfcV SystemInformation - or generated
protected byte[] myuserdata=null; // buffer user content
protected boolean[] blocktainted; // true when block is to be uploaded to tag
protected byte[] blocklocked; // 0 means writable
protected byte afi=0;
public byte nBlocks=0;
public byte blocksize=0;
public byte[] Id;
public byte[] UID; // becomes valid when a real tag is contacted
public byte DSFID = -1;
public int maxtrans=0; // tag dependent max transceive length
public byte lastErrorFlags=-1; // re-set by each transceive
public byte lastErrorCode=-1; // re-set by each transceive
public byte manuByte=0;
public static final byte BYTE_IDSTART=(byte)0xe0;
public static final byte MANU_TAGSYS=0x04;
public static final HashMap<Byte,String> manuMap = new HashMap<Byte, String>();
static{
manuMap.put(MANU_TAGSYS, "TagSys");
}
/**
* read new NfcV Tag from NFC device
*/
public ReadNfcV(Tag t) {
UID = t.getId(); // sysinfo holds the UID in lsb order - Id will be filled lateron from sysinfo!
// Log.d(TAG,"getId: "+toHex(t.getId()));
mynfcv=NfcV.get(t);
try {
mynfcv.connect();
mysysinfo=getSystemInformation();
// explore Nfcv properties..
//initfields(); // done by getSys..
maxtrans=mynfcv.getMaxTransceiveLength();
DSFID=mynfcv.getDsfId();
Log.d(TAG,nBlocks + " x " + blocksize + " bytes");
blocklocked=new byte[nBlocks]; // init the lock shadow
getMultiSecStatus(0, nBlocks); // and fill from tag
blocktainted=new boolean[nBlocks];
taintblock(0,nBlocks);
// Log.d(TAG,"maxtrans "+maxtrans);
// init space for userdata ?
myuserdata= new byte[nBlocks*blocksize];
} catch (IOException e) {
// TODO Auto-generated catch block
lastErrorFlags=-1;
Log.d(TAG, "MyNfcV failed: "+e.getMessage());
e.printStackTrace();
}
}
/**
* recreate NfcV Tag from log
* #param sysinfo: the logged system info only
*/
public ReadNfcV(String sysinfo){
int startat=0;
sysinfo.toLowerCase(); // ignore case
if(sysinfo.startsWith("0x")){ // lets believe in HEX
startat=2;
}
mysysinfo=hexStringToByteArray(sysinfo.substring(startat));
initfields();
// init space for userdata TODO limit size?
//myuserdata= new byte[nBlocks*blocksize];
isTainted=false;
// TODO fake Tag? mytag = Tag.CREATOR.createFromParcel(???);
}
/**
* recreate NfcV Tag from log
* #param sysinfo: the logged system info
* #param userdata: the logged userdata
*/
public ReadNfcV(String sysinfo, String userdata){
this(sysinfo);
// TODO fake userdata
int startat=0;
userdata.toLowerCase(); // ignore case
if(userdata.startsWith("0x")){ // lets believe in HEX
startat=2;
}
myuserdata=hexStringToByteArray(userdata.substring(startat));
}
/**
* parse system information byte array into attributes
* with respect to the flags found
* DSFID
* AFI
* memsize values (block count and length)
*/
private void initfields(){
byte[] read=mysysinfo;
if((null!=read)&&(12<read.length)&&(0==read[0])){// no error
char flags=(char)read[1]; //s.charAt(1);
// String s=new String(read);
//s.substring(2, 9).compareTo(Id.toString()) // the same?
//set the Id from mysysinfo
int pos=2;
boolean forwardId=false; // the Id field is in lsb order
if(BYTE_IDSTART==read[pos]){
forwardId=true;
manuByte=read[pos+1];
}else if(BYTE_IDSTART==read[pos+7]){
manuByte=read[pos+6];
forwardId=false;
}else
Log.e(TAG,"Id start byte not found where expected");
if(null==Id){ // dont overwrite, if given
Id=new byte[8];
for(int i=0;i<8;i++)
// TODO decide if Id to be reversed (Zebra needs msb order, that is Id[7] changes between tags)
Id[i]=(forwardId? read[pos+i] : read[pos + 7 - i]); //reverse?!
Log.d(TAG,"Id from sysinfo (reversed): "+toHex(Id));
}
pos=10; // start after flags, Infoflags and Id TODO: change if transceive should eat up the error byte
if(0<(flags&0x1)){ // DSFID valid
pos++; // already implemented
}
if(0<(flags&0x2)){ // AFI valid
afi=(byte)read[pos++];//s.charAt(pos++);
}
if(0<(flags&0x4)){ // memsize valid
nBlocks=(byte)(read[pos++]+1);//(s.charAt(pos++)+1);
blocksize=(byte)(read[pos++]+1); //((s.charAt(pos++)&0x1f)+1);
}
}
}
/**
* #return the stored afi byte
*/
public byte getAFI(){
if(isTainted){ // system info not read yet
getSystemInformation(); // fill in the fields
}
return afi;
}
public byte getDsfId(){
// return mynfcv.getDsfId(); // avoid re-reading
return DSFID;
}
public int getblocksize(){
return (int)blocksize;
}
public int getnBlocks(){
return (int)nBlocks;
}
public byte[] getSystemInformation(){
if(isTainted){ // dont reread
mysysinfo=transceive((byte)0x2b);
isTainted=false; // remember: we have read it and found it valid
if(0==lastErrorFlags){// no error
isTainted=false; // remember: we have read it and found it valid
initfields(); // analyze
}}
return mysysinfo;
}
/**
* overload method transceive
* #return resulting array (or error?)
*/
protected byte[] transceive(byte cmd){
return transceive(cmd, -1, -1, null);
}
protected byte[] transceive(byte cmd, int m){
return transceive(cmd, m, -1, null);
}
protected byte[] transceive(byte cmd, int m ,int n){
return transceive(cmd, m, n, null);
}
/**
* prepare and run the command according to NfcV specification
* #param cmd command byte
* #param m command length
* #param n
* #param in input data
* #return
*/
protected byte[] transceive(byte cmd,int m, int n, byte[] in){
byte[] command;
byte[] res="transceive failed message".getBytes();
ByteArrayBuffer bab = new ByteArrayBuffer(128);
// flags: bit x=adressed,
bab.append(0x00);
bab.append(cmd); // cmd byte
// 8 byte UID - or unaddressed
// bab.append(mytag.getId(), 0, 8);
// block Nr
if(-1!=m)bab.append(m);
if(-1!=n)bab.append(n);
if(null!=in)bab.append(in, 0, in.length);
command=bab.toByteArray();
Log.d(TAG,"transceive cmd: "+toHex(command));
// Log.d(TAG,"transceive cmd length: "+command.length);
// TODO background!
try {
if(!mynfcv.isConnected()) return res;
for(int t=maxtries;t>0;t++){ // retry reading
res=mynfcv.transceive(command);
if(0==res[0]) break;
}
}
catch (TagLostException e){ //TODO roll back user action
Log.e(TAG, "Tag lost "+e.getMessage());
try {
mynfcv.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return e.getMessage().getBytes();
}
catch (IOException e) {
Log.d(TAG, "transceive IOEx: "+e.getMessage()+toHex(res));
// e.printStackTrace();
return e.getMessage().getBytes();
}
finally{
Log.d(TAG,"getResponseFlags: "+mynfcv.getResponseFlags());
lastErrorFlags=res[0];
Log.d(TAG,"Flagbyte: "+String.format("%2x", lastErrorFlags));
if(0!=lastErrorFlags){
lastErrorCode=res[1];
Log.d(TAG,"ErrorCodebyte: "+String.format("%2x", lastErrorCode));
}
}
if(0==mynfcv.getResponseFlags())
return (res);
else
// return new String("response Flags not 0").getBytes();
return res;
}
public void taintblock(int i, int n){
for(int j=0;j<n;j++)
setblocktaint(j,true);
}
public void taintblock(int i){
setblocktaint(i,true);
}
protected void setblocktaint(int i, boolean b){
blocktainted[i]=b;
}
/* (non-Javadoc)
* #see android.nfc.tech.TagTechnology#getTag()
*
*/
#Override
public Tag getTag() {
// TODO Auto-generated method stub
//return mytag;
return mynfcv.getTag();
}
/* (non-Javadoc)
* #see android.nfc.tech.TagTechnology#close()
*/
#Override
public void close() throws IOException {
try {
mynfcv.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.d(TAG, "close failed: "+e.getMessage());
e.printStackTrace();
}
}
/* (non-Javadoc)
* #see android.nfc.tech.TagTechnology#connect()
*/
#Override
public void connect() throws IOException {
try {
mynfcv.connect();
} catch (IOException e) {
lastErrorFlags=-1; // TODO discriminate error states
Log.d(TAG,"connect failed: "+e.getMessage());
e.printStackTrace();
}
}
/* (non-Javadoc)
* #see android.nfc.tech.TagTechnology#isConnected()
*/
#Override
public boolean isConnected() {
// TODO Auto-generated method stub
// mynfcv.getDsfId();
return mynfcv.isConnected(); // better?
}
public byte[] readSingleBlock(int i){
byte[] read=transceive((byte)0x20,i);
setblocktaint(i,false); // remember we read this block
if(0!=lastErrorFlags)return read; // TODO not so ignorant..
byte[] res=new byte[read.length-1]; // drop the (0) flag byte TODO: in transceive?
for (int l = 0; l < read.length-1; l++) {
res[l]=read[l+1];
myuserdata[i*blocksize+l]=res[l]; // sort block into our buffer
}
return res;
}
/**
*
* #param i starting block number
* #param j block count
* #return block content concatenated
*/
public byte[] readMultipleBlocks(int i,int j){
if(0==blocksize){
Log.e(TAG,"readMult w/o initfields?");
getSystemInformation(); // system info was not read yet
}
byte[] read = transceive((byte)0x23,i,j);
if(0!=read[0])return read; // error flag set: TODO left as exercise..
byte[] res=new byte[read.length-1]; // drop the (0) flag byte
for (int l = 0; l < read.length-1; l++) {
res[l]=read[l+1];
myuserdata[i*blocksize+l]=res[l]; // sort block into our buffer
}
if(res.length<j*blocksize) return read; // da fehlt was
for (int k = i; k < j; k++) { // all blocks we read
setblocktaint(k, false); // untaint blocks we read
// #TODO reverting block order should be done on demand - or under user control (done again in DDMData)
// reverse(res,k*blocksize,blocksize); // swap string positions
}
return res;
}
public byte[] getMultiSecStatus(int i,int n){
byte[] read = transceive((byte)0x2c,i,n-1);
Log.d(TAG,"secstatus "+toHex(read));
if(0!=read[0])return read;
int startat=1; // TODO transceive will skip the error field soon
for(int j=0;j<nBlocks;j++)
blocklocked[j]=read[startat+i+j];
return read;
}
/**
* move anywhere to utils
* #param s
* #return
*/
public static String toHex(byte[] in){
String text=String.format("0x");
for (byte element : in) {
text=text.concat(String.format("%02x", element));
}
return text;
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}
my hello.js file
var hello = {
world: function(str, callback) {
cordova.exec(callback, function(err) {
callback('Nothing to hello.');
}, "Hello", "hello", [str]);
}
}
var ReadNfcV = {
read: function (str, callback) {
cordova.exec(callback, function (err) {
callback('Nothing to hello.');
}, "Hello", "hello", [str]);
}
}
module.exports = hello;
module.exports = ReadNfcV;
and my plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="org.apache.cordova.plugin"
version="0.1.0">
<js-module src="hello.js" name="hello">
<clobbers target="hello" />
</js-module>
<!-- Android -->
<platform name="android">
<source-file src="Hello.java" target-dir="src/org/apache/cordova/plugin" />
<source-file src="ReadNfcV.java" target-dir="src/org/apache/cordova/plugin" />
<config-file target="res/xml/config.xml" parent="/*">
<feature name="Hello" >
<param name="android-package" value="org.apache.cordova.plugin.Hello"/>
</feature>
</config-file>
</platform>
</plugin>
I was able to deploy the app so I can test a bit, My problem is that I dont really understand how i can call the ReadNfc class from the ReadNfcV.java file from within my app via javascript. I just did the same as in the Tutorial but now the hello.World function is not a function anymore so i guess i did smth wrong in my hello.js file. I would really appreciate it if someone could help and explain me how i can call my java class via javascript and then return the result from the java class back to my javascript. I looked 2 Days for an already existing plugin but didnt find anything on that subject but the phonegap-nfc plugin.
Kind regards Christopher
Update Day1
I added tech.NfcV to the Import List
import android.nfc.tech.NfcV;
Changed the execute function as suggested
#Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "execute " + action);
if (!getNfcStatus().equals(STATUS_NFC_OK)) {
callbackContext.error(getNfcStatus());
return true; // short circuit
}
createPendingIntent();
if (action.equals(REGISTER_DEFAULT_TAG)) {
addTechList(new String[]{NfcV.class.getName()}); //changed this form Mifare to NfcV
registerDefaultTag(callbackContext);
} else if (action.equalsIgnoreCase(INIT)) {
init(callbackContext);
} else {
// invalid action
return false;
}
return true;
}
Problem seems to be that I get Invalid action returned at the moment so something is wrong here
I changed the registerDefault function to
private void registerDefaultTag(CallbackContext callbackContext) {
addTechFilter();
callbackContext.success();
}
And i changed the Parse Message function to
void parseMessage() {
cordova.getThreadPool().execute(new Runnable() {
#Override
public void run() {
Log.d(TAG, "parseMessage " + getIntent());
Intent intent = getIntent();
String action = intent.getAction();
Log.d(TAG, "action " + action);
if (action == null) {
return;
}
if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)){
Tag tagFromIntent = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NfcV mfc = NfcV.get(tagFromIntent);
fireTagEvent(tag);
}
/*if (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)) {
Ndef ndef = Ndef.get(tag);
fireNdefEvent(NDEF_MIME, ndef, messages);
} else if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
for (String tagTech : tag.getTechList()) {
Log.d(TAG, tagTech);
if (tagTech.equals(NdefFormatable.class.getName())) {
fireNdefFormatableEvent(tag);
} else if (tagTech.equals(Ndef.class.getName())) { //
Ndef ndef = Ndef.get(tag);
fireNdefEvent(NDEF, ndef, messages);
}
}
}
if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
fireTagEvent(tag);
}*/
setIntent(new Intent());
}
});
}
So currently i get the error invalid action as soon as i click on start Listening and calling the Taglisteners in Javascript atm I use all 4 different Listeners to see if any work. It seems that I need to write a new fireEvent function specific for NfcV since the existing ones arent working
Update 2
I have managed to compile the plugin and deploy the app but nothing is happening i am not getting a Tag object back
The parse Message Function
void parseMessage() {
cordova.getThreadPool().execute(new Runnable() {
#Override
public void run() {
Log.d(TAG, "parseMessage " + getIntent());
Intent intent = getIntent();
String action = intent.getAction();
Log.d(TAG, "action " + action);
if (action == null) {
return;
}
if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)){
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag != null){
byte[] id = tag.getId();
// set up read command buffer
byte blockNo = 0; // block address
byte[] readCmd = new byte[3 + id.length];
readCmd[0] = 0x20; // set "address" flag (only send command to this tag)
readCmd[1] = 0x20; // ISO 15693 Single Block Read command byte
System.arraycopy(id, 0, readCmd, 2, id.length); // copy ID
readCmd[2 + id.length] = blockNo; // 1 byte payload: block address
NfcV tech = NfcV.get(tag);
if (tech != null) {
// send read command
try {
tech.connect();
byte[] data = tech.transceive(readCmd);
fireTagEvent(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
tech.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
setIntent(new Intent());
}
}
});
}
My fireTagEvent
private void fireTagEvent(byte[] data) {
String s2 = new String(data);
String command = MessageFormat.format(javaScriptEventTemplate, TAG_DEFAULT, s2);
Log.v(TAG, s2);
this.webView.sendJavascript(s2);
}
The execute function
#Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "execute " + action);
if (!getNfcStatus().equals(STATUS_NFC_OK)) {
callbackContext.error(getNfcStatus());
return true; // short circuit
}
createPendingIntent();
if (action.equals(REGISTER_DEFAULT_TAG)) {
addTechList(new String[]{NfcV.class.getName()});
registerDefaultTag(callbackContext);
} else if (action.equalsIgnoreCase(INIT)) {
init(callbackContext);
} else {
// invalid action
return false;
}
return true;
}
Thats pretty much it the app starts the "addTagDiscoveredListener" is registered but im not getting any Object back so either the nfcv tag is not read or i just dont get anything back not really sure...
I used the Chariotsolution plugin as a start to build my own nfc reading plugin. I think it would save you much time if you don't start from scratch.
There are many things you can remove because the original plugin only deals with NDEF tags, but removing lines is faster than re-inventing the wheel.
It's a bit old in my head, so I'm not sure I can explain everything right...
My nead was to read info in Mifare classic tags, but maybe you can adapt for your needs...
So, if you look at NfcPlugin.java, in the execute function, all I kept was the code for the actions REGISTER_DEFAULT_TAG and INIT.
Updated the code in REGISTER_DEFAULT_TAG to register the listening for mifare classic tag. And modified registerDefaultTag function to call addTechFilter instead of addTagFilter.
So this leaves us with
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "execute " + action);
if (!getNfcStatus().equals(STATUS_NFC_OK)) {
callbackContext.error(getNfcStatus());
return true; // short circuit
}
createPendingIntent();
if (action.equals(REGISTER_DEFAULT_TAG)) {
addTechList(new String[]{MifareClassic.class.getName()});
registerDefaultTag(callbackContext);
} else if (action.equalsIgnoreCase(INIT)) {
init(callbackContext);
} else {
// invalid action
return false;
}
return true;
}
private void registerDefaultTag(CallbackContext callbackContext) {
addTechFilter();
callbackContext.success();
}
Now what you need to understand is that once you called from the js the init function, the parseMessage function of the plugin will be called each time the device sees a nfc tag.
So in the parseMessage function I have a test
if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED))
in wich I have all the code to deal with my tag.
In that code can get info from the tag in the intent using something like this :
Tag tagFromIntent = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
MifareClassic mfc = MifareClassic.get(tagFromIntent);
and then depending on your treatment you can call either fireErrorEvent or a fire...Event of your own that would return the data to the javascript using the webView.sendJavascript function.
I'm running out of time to detail the js part.
Not sure sure if it will help you or if it's the way you want to go (don't know how the tag you're using is working). Let me know if it helps you and if you need more details.
Okay so I finally managed to get it working
In the Phonegap-nfc.js I added an eventlistener for the NFCV tag
addNfcVListener: function (callback, win, fail) {
document.addEventListener("nfcv", callback, false);
cordova.exec(win, fail, "NfcPlugin", "registerNfcV", []);
},
The matching Execute function looks like this
#Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
Log.d(TAG, "execute " + action);
if (!getNfcStatus().equals(STATUS_NFC_OK)) {
callbackContext.error(getNfcStatus());
return true; // short circuit
}
createPendingIntent();
if (action.equalsIgnoreCase(REGISTER_NFCV)) {
registerNfcV(callbackContext);
}else {
// invalid action
return false;
}
return true;
}
Here is where the Tag gets added to the techlist
private void registerNfcV(CallbackContext callbackContext) {
addTechList(new String[]{NfcV.class.getName()});
callbackContext.success();
}
Here the Tag gets parsed and fires an event
void parseMessage() {
cordova.getThreadPool().execute(new Runnable() {
#Override
public void run() {
Log.d(TAG, "parseMessage " + getIntent());
Intent intent = getIntent();
String action = intent.getAction();
Log.d(TAG, "action " + action);
if (action == null) {
return;
}
if (action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) || action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)){
NfcvData ma;
Tag tagFromIntent = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Parcelable[] messages = intent.getParcelableArrayExtra((NfcAdapter.EXTRA_NDEF_MESSAGES));
NfcV mfc = NfcV.get(tagFromIntent);
Tag tag = mfc.getTag();
fireNfcVReadEvent(NFCV, mfc, messages);
}
setIntent(new Intent());
}
});
}
Then this event is called
private void fireNfcVReadEvent(String type, NfcV nfcv, Parcelable[] messages) {
JSONObject jsonObject = buildNfcVReadJSON(nfcv, messages);
String tag = jsonObject.toString();
String command = MessageFormat.format(javaScriptEventTemplate, type, tag);
Log.v(TAG, command);
this.webView.sendJavascript(command);
}
Which sends the Taginformation back to my Javascript
It appears that you are not sending any data back in you callback, just the cordova SUCCESS. callbackContext.success(returndatahere); See the cordova plugin spec for supported data types.

Categories