private static string CopyImageElementToDataImageString(GeckoHtmlElement element, string imageFormat, float xOffset, float yOffset, float width, float height) { if (width == 0) { throw new ArgumentException("width"); } if (height == 0) { throw new ArgumentException("height"); } string data; using (AutoJSContext context = new AutoJSContext(GlobalJSContextHolder.GetJSContextForDomWindow(element.OwnerDocument.DefaultView.DomWindow))) { context.EvaluateScript("window.__selectedElement = this", (nsISupports)(nsIDOMElement)element.DomObject, out data); context.EvaluateTrustedScript(string.Format(@"(function(canvas, ctx) {{ canvas = document.createElement('canvas'); canvas.width = {2}; canvas.height = {3}; ctx = canvas.getContext('2d'); ctx.drawImage(window.__selectedElement, -{0}, -{1}); return canvas.toDataURL('{4}'); }} )()" , xOffset, yOffset, width, height, imageFormat), out data); } return(data); }
public void EvaluateScript_JavascriptIncreasePriviligesCrossFrameAccess_ScriptExecutesAndReturnsExpectedResult() { string result; string aboutBlank; using (var page = new GeckoWebBrowser()) { IntPtr unused = page.Handle; page.CreateWindow += (s, e) => { e.WebBrowser = browser; }; using (AutoJSContext context = new AutoJSContext(page.Window.JSContext)) { Assert.IsTrue(context.EvaluateScript("window.open('http://www.google.com'); window.location.href", out aboutBlank)); } browser.NavigateFinishedNotifier.BlockUntilNavigationFinished(); using (AutoJSContext context = new AutoJSContext(browser.Window.JSContext)) { Assert.IsTrue(context.EvaluateScript("window.location.href", out result)); Assert.AreNotEqual(aboutBlank, result); // cross frame access Assert.IsFalse(context.EvaluateScript("opener.location.href", out result)); Assert.IsTrue(context.EvaluateTrustedScript("opener.location.href", out result)); Assert.AreEqual(aboutBlank, result); } page.Stop(); } }