2006-06-14 Chris Howie <cdhowie@nerdshack.com>
* jscallglue/jscallglue.cpp, AspNetEdit.JSCall/CommandManager.cs: Add a method "JSEval" to execute arbitrary JavaScript in the browser. svn path=/trunk/aspeditor/; revision=61693
This commit is contained in:
Родитель
6cfc0a9394
Коммит
5eee50269b
|
@ -42,7 +42,11 @@ namespace AspNetEdit.JSCall
|
|||
static extern int PlaceFunctionCall (IntPtr embed,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string call,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string returnto,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string args);
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string args);
|
||||
|
||||
[DllImport ("jscallglue.dll")]
|
||||
static extern int ExecuteScript (IntPtr embed,
|
||||
[MarshalAs(UnmanagedType.LPWStr)] string script);
|
||||
|
||||
private Hashtable functions;
|
||||
private WebControl webControl;
|
||||
|
@ -78,7 +82,7 @@ namespace AspNetEdit.JSCall
|
|||
|
||||
if (returnTo.Length == 0)
|
||||
{
|
||||
string result = clrCall (args);
|
||||
clrCall (args);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -87,7 +91,39 @@ namespace AspNetEdit.JSCall
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public void JSEval (string script)
|
||||
{
|
||||
int result = ExecuteScript (webControl.Handle, script);
|
||||
string err;
|
||||
|
||||
switch (result)
|
||||
{
|
||||
case 0:
|
||||
return;
|
||||
|
||||
case 1:
|
||||
err = "Could not obtain IDOMDocument from GtkMozEmbed. Have you shown the window yet?";
|
||||
break;
|
||||
|
||||
case 2:
|
||||
err = "Could not create script element.";
|
||||
break;
|
||||
|
||||
case 3:
|
||||
err = "Could not cast script element to nsIDOMHTMLScriptElement.";
|
||||
break;
|
||||
|
||||
case 4:
|
||||
err = "Could not locate body element.";
|
||||
break;
|
||||
|
||||
case 5:
|
||||
err = "Could not append script element to body.";
|
||||
break;
|
||||
}
|
||||
|
||||
throw new Exception ("Glue function ExecuteScript: " + err);
|
||||
}
|
||||
|
||||
public void JSCall (string function, string returnTo, params string[] args)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
2006-06-14 Chris Howie <cdhowie@nerdshack.com>
|
||||
|
||||
* jscallglue/jscallglue.cpp,
|
||||
AspNetEdit.JSCall/CommandManager.cs:
|
||||
|
||||
Add a method "JSEval" to execute arbitrary JavaScript in the browser.
|
|
@ -40,9 +40,12 @@
|
|||
#include <nsIDOMNode.h>
|
||||
#include <nsIDOMNodeList.h>
|
||||
|
||||
#include <nsIDOMHTMLScriptElement.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
int PlaceFunctionCall(GtkMozEmbed *embed, const PRUnichar *call, const PRUnichar *returnto, const PRUnichar *args);
|
||||
int ExecuteScript(GtkMozEmbed *embed, const PRUnichar *script);
|
||||
}
|
||||
|
||||
|
||||
|
@ -111,3 +114,46 @@ int PlaceFunctionCall(GtkMozEmbed *embed, const PRUnichar *call, const PRUnichar
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Inspiration from
|
||||
// http://kmeleon.cvs.sourceforge.net/kmeleon/k-meleon/BrowserViewUtils.cpp?revision=1.53&view=markup
|
||||
int ExecuteScript(GtkMozEmbed *embed, const PRUnichar *script)
|
||||
{
|
||||
nsresult result;
|
||||
|
||||
nsCOMPtr<nsIDOMDocument> doc = GetIDOMDocument(embed);
|
||||
|
||||
if (doc == NULL)
|
||||
return 1;
|
||||
|
||||
// Create the script node
|
||||
nsCOMPtr<nsIDOMElement> scriptDOMElement;
|
||||
result = doc->CreateElement(NS_ConvertUTF8toUTF16("script"), getter_AddRefs(scriptDOMElement));
|
||||
if (NS_FAILED(result) || !scriptDOMElement)
|
||||
return 2;
|
||||
|
||||
// and the content
|
||||
nsCOMPtr<nsIDOMHTMLScriptElement> scriptE = do_QueryInterface(scriptDOMElement);
|
||||
if (!scriptE)
|
||||
return 3;
|
||||
|
||||
scriptE->SetText(nsDependentString(script));
|
||||
scriptE->SetType(NS_ConvertUTF8toUTF16("text/javascript"));
|
||||
|
||||
// find the body node.
|
||||
nsCOMPtr<nsIDOMElement> body;
|
||||
result = doc->GetDocumentElement(getter_AddRefs(body));
|
||||
if (NS_FAILED(result) || !body)
|
||||
return 4;
|
||||
|
||||
// add the element to the body
|
||||
nsCOMPtr<nsIDOMNode> unused;
|
||||
result = body->AppendChild(scriptE, getter_AddRefs(unused));
|
||||
if (NS_FAILED(result))
|
||||
return 5;
|
||||
|
||||
// gecko has executed the element, we can remove it now
|
||||
result = body->RemoveChild(scriptE, getter_AddRefs(unused));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче