How to call custom event in VBscript - html

I have an C++ application which will take care of script execution. currently i want to execute custom event firing in Vb script and J script. I written code for J Script and working fine, how can i achieve this in Vb Script. (mainly targeting IE)
function customEventFn()
{
alert("sample");
}
var element = document.getElementById("elemneid");
customEventFn.call(element);
Please help me to solve this

Last i found answer to my problem. The above methods does not work in Internet Explorer before version 9. use attachEvent method to register an event handler in earlier Internet Explorer versions. but it wont fire custom event.
you need to install IE9 for correct working of the script.
Add Following tag to the HEAD of the htm file.
-meta http-equiv="X-UA-Compatible" content="IE=9" -
write event body wrapped with function
get reference to an element by using getElementById’
Create custom event and attach that event to the element with call back as function defined in step1.
fire custom event
remove custom event from listener(removeEventListener).
Function CustomFunction()
'function body
End Function
set customFn = getRef("CustomFunction")
set element = document.getElementById("elemid");
element.addEventListener "OnSampleEvent", customFn
set Event = document.createEvent("sampleEvent")
Event.initCustomEvent "OnSampleEvent", false, false, nil
element.dispatchEvent(Event)
element.removeEventListener "OnSampleEvent", customFn

Related

Is there a "broadcast" method in HTML/CSS/Javascript?

So in the site scratch.mit.edu, there is a way so that once a block of code is finished, it can broadcast a statement. Its a really useful tool and I was wondering if there is something like/similar to that in coding!
Does anyone know if there is or not?
Look into creating and triggering custom events.
Example:
//create a new custom Event object called build
const event = new Event('build');
// create an event listener (attached to the global window object)
//This is like a Scratch receiver hat block. The first parameter, 'build', is the
//name of the broadcast. The second parameter, the function, is your code
//that reacts to the broadcast.
window.addEventListener('build', function (e) { /* your code here */ });
//This line of code "sends" the broadcast.
window.dispatchEvent(event);
Have fun learning JavaScript!

Javascript Just Get copyed text from clipboard on Chrome

Can you tell me how can I just get text who is copied in clipboard. I don't want to make a copy because data are copied from Excel.
In IE I use :
var clipText = window.clipboardData.getData('Text');
And it's work perfect.
Is it possible in chrome ? or maybe Firefox ?
Thanks for advance
The window.clipboardData object is only available in IE. It seems like a big security vulnerability to me for a website to be able to access your clipboard data, especially without you knowing. According to the specification, it's mostly deprecated as of Microsoft Edge.
Instead, you can access the data by listening to the paste event:
document.addEventListener('paste', function (event) {
var clipText = event.clipboardData.getData('Text');
});
If you're looking to use jQuery and bind an element to the 'paste' event then you can access the clipboard data by using the originalEvent property on the calling event.
Check the window object to see if the clipboardData is undefined. This will mean that you're not using IE or Edge.
this.bind('paste', function(e){
if (window.clipboardData === undefined)
clipText = e.originalEvent.clipboardData.getData('Text') // use this method in Chrome to get clipboard data.
else
clipText = window.clipboardData.getData('Text') // use this method in IE/Edge to get clipboard data.
});

Copy an Image from Flex Application (Web) and paste it outside the application

I have a requirement that we should be able to copy an image displayed in our application, to Clipboard and paste it outside (Like on Excel).
I was trying the below code snippet (Inside a button Click).
Clipboard.generalClipboard.clear();
var dataLoaded:Boolean = Clipboard.generalClipboard.setData(ClipboardFormats.RICH_TEXT_FORMAT,
byteArray, false);
The dataLoaded object is true, however it does not paste anything when tried on Excel or MsPaint.
Do we have any way to achieve this?
Thanks.
The code you are showing is not enough in itself to get a successful transfer. Like many other operations within the security sandbox of a FP app (web) this code can only respond to a direct user interaction. So your code without any valid context cannot work of course but if called within a mouse down listener for example (a true user generated mouse event, creating a fake mouseevent would still not work) it should respond correctly:
private function handleMouseClick(event:MouseEvent):void
{
Clipboard.generalClipboard.clear();
var dataLoaded:Boolean = Clipboard.generalClipboard.setData(ClipboardFormats.RICH_TEXT_FORMAT, byteArray, false);
}

Unhandled exception when firing WinRT 8.1 WebView ScriptNotify

I'm trying to create an MVVM Caliburn-based WinRT 8.1 app (I know that CM won't be perfectly compatible with 8.1 until version 2.0 is out, but the error does not seem to be related, as it is raised also when the handler is placed in the view code behind). One of its views contains a WebView control, whose content is set via NavigateToString (HTML contents come from app's installed assets). The HTML loaded into this control includes several hyperlinks, most of them representing cross-references to other asset-based HTML content. So when users click the link I want to override the standard navigation action, get my viewmodel notified, and let it load another HTML content from the app assets.
Here is what I did, following the post Open links in external browser in WebView (WinRT):
in the XAML code, I added to the WebView control an attribute for attaching the ScriptNotify event to my VM: cal:Message.Attach="[Event ScriptNotify] = [Action GetFromLink($eventArgs)]" (see https://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions).
in my VM, the method signature is public void GetFromLink(NotifyEventArgs e).
whenever my VM loads some HTML into the WebView, it first injects a script in the HTML head which replaces the click handler of each anchor representing a cross-reference (all these anchors are marked by a class="xref" attribute). This script is hold in a constant in my VM:
private const string SCRIPT = "for (var i = 0; i < document.links.length; i++) {" +
"var className = document.links[i].getAttribute(\"class\");" +
"if (className && className === \"xfer\") {" +
"document.links[i].onclick = function() {" +
"window.external.notify('url:' + this.href);" +
"return false;" +
"}}}";
Now, when I launch the app, load an item containing one of these xref's and click on it, I get an unhandled exception telling me that "navCancelInit is undefined". I suppose this error is surfacing from JS code, but I cannot see where and how this function should be defined.
According to http://msdn.microsoft.com/library/windows/apps/br227713, I do not need any additional step for ScriptNotify when HTML has been loaded via NavigateToString. Could anyone suggest a solution?
I got an answer from a MS guy about this, so credit is not mine for this answer: it is a timing issue. I must ensure that the page has fully loaded before running the script which changes the DOM; pretty simple, if you think. Just moving the script at the end of the page, or wrapping it in an onload handler, should make the trick. Hope this can save some hair-pulling to others!
If you listen to this event: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.domcontentloaded.aspx WebView.DOMContentLoaded event, All of the script should be loaded in the WebView and you should be able to access and execute, if you are trying to do this before the scripts won't be loaded.

Is it possible to inject a javascript code that OVERRIDES the one existing in a DOM? (e.g default alert function)

Ok, so what I want is to override a method that already exists inside a tab, what I'm going to use is the default alert function.
Override it inside the JS function would be very easy. just add
window.alert = function(){
//Do Something
}
but the problem is that when I try to use chrome.tabs.executeScript("window.alert = function() { };"); it doesn't work. I tried to do this manually by using the Console from Chrome in the tab that I wanted to override the function, I typed that override function in the log and pressed enter, and done, the alert function was overridden, but I can't do this via Chrome Extension.
When you add executeScript, it seems like it creates a Javascript apart from the one inside the tab DOM, because I can create functions with the name of a function that already exists inside the tab DOM.
Is there a way to make executeScript to write the script inside of the tab DOM, so it can actually override any function that was written by the .js file the page generated?
Thanks!
Functions don't exist as part of the DOM; instead, they exist within an execution environment that includes the DOM. Content scripts (including scripts run with executeScript) and actual web pages share the same DOM, but have separate execution environments. So calling window.alert = function() {} only rewrites window.alert within your content script's execution environment, not in the actual page's one.
The typical way to reach the execution environment of the actual page is to inject a <script> tag into the DOM. This can be done in several ways. One method is to white-list a script in web_accessible_resource, and insert the <script> element referring to this script in the document. The required absolute URL can be obtained via chrome.extension.getURL.
var s = document.createElement("script");
s.src = chrome.extension.getURL("script_in_extension.js");
(document.head||document.documentElement).appendChild(s);
Make sure that the script is configured to "run_at": "document_start", so that the overwrite takes place before any of the page's functions are loaded.
Note: Your action can easily be undone by the page:
window.alert = function(){ /*...*/ }; // Your overwrite
delete window.alert; // Run from the page/console/...
window.alert('Test?'); // Displays alert box.
If it's critical that the overwritten function cannot be removed, use Object.defineProperty to define an immutable method. For more details, see Stop a function from execute with Chrome extension.