c# - Modifying remote JavaScripts as they load with CefSharp? -
i building custom browser part of interface remote website. gui sucks i'm doing javascript hacks make better.
currently, make modifications ui using following greasemonkey script (on firefox):
// ==userscript== // @name winman-load // @namespace winman // @description stuff 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 able if nature cefsharp, can modify scripts on-the-fly browser loads page.
ok figured out. create requesthandler method 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 multiple search/replace make 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> /// portion of find string matching. /// </summary> private int findmatchoffset; /// <summary> /// overflow output buffer. /// </summary> private readonly list<byte> overflow = new list<byte>(); /// <summary> /// number of times the string 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) { // data read. datainread = datain == null ? 0 : datain.length; dataoutwritten = 0; // write overflow reset if (overflow.count > 0) { // write overflow last time. writeoverflow(dataout, ref dataoutwritten); } // evaluate each character in input buffer. track how many characters in // row match findstring. if findstring matched write // replacement. otherwise, write input characters as-is. (var = 0; < 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 match, increment counter findmatchoffset++; // if characters match string specified if (findmatchoffset == replace.key.length) { // complete match of find string. write replace string. writestring(replace.value, replace.value.length, dataout, ref dataoutwritten); // start on looking match. findmatchoffset = 0; replacecount++; } continue; } // character did not match find string. if (findmatchoffset > 0) { // write portion of find string has matched far. writestring(replace.key, findmatchoffset, dataout, ref dataoutwritten); // start on looking match. findmatchoffset = 0; } } // write current character. writesinglebyte(readbyte, dataout, ref dataoutwritten); } if (overflow.count > 0) { //if end overflow data we'll need return needmoredata // on next pass data written, next batch processed. return filterstatus.needmoredata; } // if match in-progress 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 output buffer. var remainingspace = dataout.length - dataoutwritten; // maximum number of bytes can write output buffer. var maxwrite = math.min(overflow.count, remainingspace); // write maximum portion fits in output buffer. if (maxwrite > 0) { dataout.write(overflow.toarray(), 0, (int) maxwrite); dataoutwritten += maxwrite; } if (maxwrite < overflow.count) { // need write more bytes fit in output buffer. // remove bytes written 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 output buffer. var remainingspace = dataout.length - dataoutwritten; // maximum number of bytes can write output buffer. var maxwrite = math.min(stringsize, remainingspace); // write maximum portion fits in output buffer. if (maxwrite > 0) { var bytes = encoding.getbytes(str); dataout.write(bytes, 0, (int) maxwrite); dataoutwritten += maxwrite; } if (maxwrite < stringsize) { // need write more bytes fit in output buffer. store // remainder in 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 output buffer. var remainingspace = dataout.length - dataoutwritten; // write byte buffer or add overflow if (remainingspace > 0) { dataout.writebyte(data); dataoutwritten += 1; } else { // need write more bytes fit in output buffer. store // remainder in overflow buffer. overflow.add(data); } } public void dispose() {} } }
keep in mind iterates through left right, top bottom, make sure search , replaces in right order.
Comments
Post a Comment