How can I detect if the clipboard is reachable in firefox? - javascript

For my JavaScript project I need to detect if the clipboard is reachable. Because in Firefox you need to configure an access for every site which needs it, otherwise some functions (like the execCommand with cut, copy or paste attribute) can't be executed and I need to know that.

You could try saving something into the clipboard. If it fails, you know it is not accessible.
try
{
// Use some library to save some data into the clipboard.
}
catch (ex)
{
alert("Your browser seems to block access to the clipboard.");
}

Access to the clipboard is disallowed in Chrome and Firefox per default. With pure javascript it can't be done in other browsers than Internet Explorer. You will need a Flash-shim.
You can find an article about how to achieve that crossbrowser-compatibly here.
A good library for this is Zeroclipboard.

Related

How do we check if a browser's auto-update is enabled through javascript

I want to check form the javascript if a browser's auto-update is enabled.
The reason I want this is that our software supports only a set of Browser versions and we want to alert them if auto-update is on.
Is there any way we can do this?
Abhi
No. Browsers are highly sand-boxed and will give you almost no information about the users system.
You'll instead have to just do something like check the version and alert them if it isn't up-to-date, with maybe some information on how to check if they have auto-updated turned on.
Not sure why you require a set version, but one thing to do is check if the browser supports a specific method (One that your app requires).
If not, tell them to update their browser. This example test if the user has WebSockets:
if (window.WebSocket) {
alert("BROWSER SUPPORTED");
} else {
alert("BROWSER NOT SUPPORTED");
}

How can I access Safari extension settings from outside of the global scope?

I am trying to allow the user to configure my safari extension through a HTML preference page (as many Safari extensions do). I open this page from a javascript call inside my global html file:
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = safari.extension.baseURI + "settings/settings.html";
What I can NOT manage to do is write anything from this settings.html into the actual Safari extension settings or access the global page.
safari.extension.settings.MY_SETTINGS = settingsData;
safari.extension.globalPage
Both of these calls result in exceptions and the objects appear undefined.
I then also tried to send messages, but never seem to receive them in the global space, where I thought I could then again access the settings.
safari.self.tab.dispatchMessage("store_settings", settingsData); //settings javascript
These message are not received by my event listener.
safari.self.addEventListener("message", process_messages, false); //GLOBAL javascript
Any idea why I can not access the extension settings? Do I need to initialise something for my settings.html to be able to access the extension settings?
PS: I have seen a similar approach working inside the ClickToPlugin Safari extension - so it should be possible, but I can't get it to work :(
In the global script, try safari.application.addEventListener.
If your html page is part of your extension then your settings.js script file will have access to safari.extension.globalPage. This object points to the window of your global.html.
From there you can call any object in that context. Debugging this however is a pain to say the least. Good luck :-)

Can I tell if my Chrome Extension is running on Windows?

In my Google Chrome Extension I need to copy text onto the Clipboard, and I need to know if my extension is installed on Windows OS or not. Is it possible?
PS. If it is Windows, then I will replace end-lines with "\r\n", which makes multi-line text look better on Windows.
Two ways, at least
You can simply rely on navigator.platform
Better option is to use Chrome API: chrome.runtime.getPlatformInfo():
chrome.runtime.getPlatformInfo(function callback)
Returns information about the current platform.
In the form of a PlatformInfo object.
chrome.runtime.getPlatformInfo( function(info) {
if(info.os == "win") { /* do stuff */ }
});

JavaScript for Chrome extension - get Windows username

I am working on a Chrome extension, where in my JavaScript function should identify the logged in Windows username.
I want to access Windows' username within JavaScript and display it in my Chrome extension web page.
I tried following <script>, this works well in IE. Is there any equivalent way possible with the Chrome browser?
function GetUserName()
{
var wshell = new ActiveXObject("WScript.Shell");
alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
}
Please let me know any built-in method to extract the username.
You're using ActiveX which is only available in IE. In my opinion you should never be able to access information like this from within a browser (and usually can't). I doubt there is a solution which functions like you want.

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine's clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use Flash?
My primary target is IE8, but would like to support FF and Chrome also.
I have seen the technique to do this using Flash, but am looking for a pure js route:
Clipboard access using Flash
Since this is a big security risk, all browsers that care about safety don't allow JS to access the clipboard.
The main reason is that many people put their passwords into a text file and then use cut&paste to login. Crackers could then collect the password (and possibly other private information like the word document which you just copied) from the clipboard by cracking a popular site and installing some JS that sends them the content of the clipboard.
Which is why I have flash disabled all the time.
No, not in FF and Chrome. It works in IE (not sure about 7 and 8, but definitively 6), and from Flash. That is why Flash is always used.
Forget pure JS.
There is no standard API for accessing the clipboard, and few browsers implement a propriety method.
Flash is the 'standard' method.
You're looking for the execCommand function, at least the best I can tell. Here are some resources:
Insert text in Javascript contenteditable div
http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/execCommandisappliedto.htm
Unfortunately, this runs into the same security loophole that Flash sealed in Flash 9. Since people were spamming the clipboard, the clipboard is now only accessible through direct user interaction, and honestly, it is better that way. And I'll wager that most browsers have similar (if not stricter policies).
In IE, to do this is pretty painless. For Firefox, you need to update users.js and/or prefs.js (you can google for Clipboard access in Firefox). For Chrome, you need to write an extension.
In you extension background_page, have a place holder (an IFrame)
in your webpage, have buttons or links like 'cut', 'copy' and 'paste'. also have a hidden iframe paste_holder on your page to get back the text read by the background_page of your extension. In your extension's manifest file, have code like below:
manifest.json
"background_page": "mypaste_helper.html",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["mypaste_helper.js"],
"all_frames": true
}
],
"permissions": [
"clipboardRead",
"clipboardWrite",
"tabs"
]
mypaste_helper.js
get references to your cut, copy and copy buttons on the page
cutButton.addEventListener("click", function()
{
get selected content using window.getSelection()
pass that text to handleCut function in mypaste_helper.html
}, false);
copyButton.addEventListener("click", function()
{
get selected content using window.getSelection()
pass that text to handleCopy function in mypaste_helper.html
}, false);
pasteButton.addEventListener("click", function()
{
get content from handlePaste function in mypaste_helper.html
}, false);
in the callback function
get the contents sent by background_page function
set innerHTML of paste_holder frame's document.body with received text.
mypaste_helper.html
handleCopy and handleCut are identical
get reference to your iframe document.body as clipboardholder
set innerHTML of the clipboardholder.contentDocument.body with the data passed by mypaste_helper.js
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('copy')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js
handlePaste
get reference to your iframe document.body as clipboardholder
you may want to clear the contents of clipboardholder.contentDocument.body
capture selection through window.getSelection()
selection.selectAllChildren(clipboardholder);
document.execCommand('paste')
read contents of the clipboardholder
pass the text back to callback in mypaste_helper.js
http://www.rodsdot.com/ee/cross_browser_clipboard_copy_with_pop_over_message.asp implements the ZeroClipboard flash object correctly and is cross browser. It also discusses the potential problems with ZeroClipboard and possible work-arounds. Also compatible with Flash 10+.
Here's a pure JS implementation that lets you paste image data which works in Google Chrome: http://strd6.com/2011/09/html5-javascript-pasting-image-data-in-chrome/
It was clear this issue, but I still have doubts because there is the option to do this in javascript and another thing is for windows forms it is totally possible to do using the command
Clipboard.Clear()
Ref: System.Windows.Forms
Any malicious software that can do well.

Categories

Resources