Detect keyboard focus for INPUT fields - google-chrome-extension

I have an app that is in world wide use for several years and in need of an update. It monitors keystrokes and then translates these into actions (opening file/folder/document etc) or substituting user defined text. It's not a key logger and please don't suggest otherwise.
Currently it uses UIAccessibility on Windows to detect password fields and then auto disable itself.
We are trying to implement an auto disable for browsers.
Using a webextension I have a couple of questions.
1) How can I detect when an INPUT type='password' gains keyboard focus?
2) How can I call a DLL or send a message to our app? (I have seen the maybe possible using Native messaging but not sure if this is the best or correct way.)
Thanks

To answer your questions:
1) You need to add a content script to each and every page. In manifest.json, add this:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentscript-password.js"],
"run_at": "document_start"
}
],
When you have done that, you need to enumerate all inputs where type=password (getAttribute("type") == "password").
On every password input, you add an "change" event listener - https://developer.mozilla.org/en-US/docs/Web/Events/change
In that event listener, you send a message to the background script, which in turn can communicate with native messaging to some external piece of software that you wrote.
2) Yes, native messaging is the way to go. Since I have no experience in using it yet, you will have to find out how it works yourself. There is documentation about it here: https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging

Related

Trigger a dialog box in background script

I have a chrome extension, where I periodically throw out an alert based on something.
The thing is that the default alert in Javascript is very ugly and I am trying to replace it with something more beautiful.
The problem is that currently the alert is triggered from the background script. Google doesn't allow us to include any external libraries in the background html.
Given this problem, how do I go about replacing the default alert with a more modern UI alert?
I was looking to replace the default alert with something like the SweetAlert.
My background.js currently looks like this:
// on some alarm trigger
function showpopup() {
console.log(" in show popuup");
console.log(Date());
alert("ugly alert");
}
I also explored the option of injecting another js file from my background file.
function showpopup() {
console.log(" in show popuup");
console.log(Date());
var s = document.createElement('script');
// added "script.js" to web_accessible_resources in manifest.json
s.src = chrome.extension.getURL('script.js');
s.onload = function() {
this.remove();
};
(document.head || document.documentElement).appendChild(s);
}
My script.js currently just calls an alert
alert("ugly alert now in script.js");
I am not able to figure out how to create my own dialog box in this javascript file script.js.
The problem is where your alert will be shown?
In an browser/OS dialog window? That's what alert() and friends do; as you see yourself, it's ugly and inflexible. In addition, it's technically challenging: it's an old mechanism that stops execution of JS code until closed, which can lead to API malfunctioning; Firefox WebExtensions specifically don't support calling this from the background page.
In the background page? By definition, it's invisible. You can add DOM nodes with an alert there, but you will not see it. Your problem isn't loading a library, your problem is where to display results.
(invisible, so no picture here!)
In the currently open tab? Hijacking an arbitrary page to show your own UI is hard, prone to break, would require draconian permissions with user warnings at install, won't always work. Wouldn't recommend.
In a fresh window? Possible (see chrome.windows API), but hardly "modern UI" at all (at least you can hide the URL bar).
In a browser action popup? Still not possible to trigger it to open in Chrome, so that's out.
The de-facto standard for informing the user about such things is the chrome.notifications API. It offers limited customization, but that's the "modern" approach considering that your extension has no UI surfaces already open at alert time.
You can insert your code into the tab content via
JS: chrome.tabs.executeScript()
CSS: chrome.tabs.insertCSS()
The second possibility would be to use a content script (content.js). But then you would have to use messaging to communicate between background.js and content.js.

Communicating with a content script on active tab from a Chrome extension's page action popup

I'm not getting how to pass data between content script and page action popup.
I've started with the following simple skeleton, that shows an page action for any page having a minus-dash in title:
Extension manifest (manifest.json):
{
…
"permissions": ["http://example.org/*"],
"background": {"scripts": ["background.js"]},
"page_action": {"default_popup": "popup.html", …},
"content_scripts": {
"matches": ["http://example.org/*"],
"js": ["content.js"]
}
}
Background script (background.js):
chrome.extension.onRequest.addListener(function (msg, sender, respond) {
if (msg && msg.what === "match") {
console.log("Match:", msg.title);
chrome.pageAction.show(sender.tab.id);
}
}
Content script (content.js), checking document titles:
var title = document.title;
if (title.indexOf("-") >= 0) {
chrome.extension.sendRequest({"what": "match", "title": title});
}
Now, in a page action's popup, I want to show matched page's title. Not the last loaded page's title (as would be done by using this answer), but the title of the active tab.
My first though was to send a query to the content script, but according to documentation chrome.extension.sendMessage will send it to all listeners (i.e. all my content scripts on all tabs), without any clear definition on whose response I'll receive back. Also, I can't use chrome.tabs.sendMessage as it requires tabId. Trying to find the current tab using chrome.tabs.getCurrent will return null, as the query comes from non-tab context (from a popup).
I guess I could probably use chrome.tabs.executeScript to communicate, but this just feels dirty.
Still, I believe, this is a basic thing that should be very simple to do and I'm just missing something. Could someone, please, help me, preferably, with an example code?
Update: I've found Mappy example extension and it uses chrome.tabs.onUpdated to keep track of the active tab. This, unfortunately, requires tabs permission. Personally, I'd like to use least privileges possible.
So, is it just unfortunately bad permission system design and I have no choice but to do it this way, or there's any workaround? I'd be happy if chrome.pageAction.onClicked event handler (which provides Tab object that I need) would allow me to show a popup...
I think you need to add the onClick event listener in your popup:
chrome.pageAction.onClicked.addListener(function(tabs.Tab tab) {...});
See documentation here.
Callback of the event listener would provide you the tabId which would surely be the active tab.
There are multiple Problems in your code
chrome.extension.sendRequest in chrome.extension.sendRequest({"what": "match", "title": title}); is deprecated
chrome.pageAction.onClicked will not fire when you have "page_action": {"default_popup": "popup.html", …}, in your manifest.
chrome.extension.sendMessage will send it to all listeners (i.e. all my content scripts on all tabs), is an invalid assumption, it will send to Extension Pages.
I tried to read your question multiple times but couldn't understand what is you want to achieve, could you explain it?

Key binding on a flash page

I would like to know if the following is possible - I'm having trouble finding information regarding my question on the web. I was looking into creating a Google Chrome extension to work with a webpage, until I realized that the page was in fact built with flash. Originally I wanted to bind an onClick event for an input on the webpage to display a form (which would be through the extension I'm building).
However, because the page is build in flash I no longer believe that this will be a possibility. So, my next idea was to bind the form's loading to a certain combination of keys (ctrl + d) for example, that the user would click.
For example:
The user is using the flash form with my extension running in the background. The user focuses on an input (text box let's say) and proceeds to click the keys (Ctrl + D). Upon clicking these keys, my extension pops open a web-form. The user types in what he wants in the form and then clicks "Save" - A submit button on my web-form. When the user clicks 'save', the data from the web-form is stored in the clipboard and the form closes. Finally, the focused input is filled in with the data from the clipboard.
Two questions:
Do Google Chrome extensions support key binding like this?
Will I be able to copy input from my generated form (through the extension) and paste it back into the input field on the flash page?
1. Do Google Chrome extensions support key binding like this?
Yes, you can specify custom key board shortcuts catered to platform using chrome.commands API.
You can directly trigger browser action\page action popup page or do any customized functionality by defining a key.
Sample Code of manifest
{
"sample": {
"suggested_key": {
"default": "Ctrl+Shift+S",
"windows": "Ctrl+Shift+P"
},
"description": "Thisismycustomkeyforchromeextension"
},
"_execute_browser_action": {
"suggested_key": {
"windows": "Ctrl+Shift+Y",
"mac": "Command+Shift+Y",
"chromeos": "Ctrl+Shift+U",
"linux": "Ctrl+Shift+J"
}
}
}
However, maximum of 4 keys are supported so far! For further information refer this answer
2. Will I be able to copy input from my generated form (through the extension) and paste it back into the input field on the flash page?
Yes, with combination of content scripts and message passing it is possible.
References
Content Scripts
Background Pages
Message Passing
Commands API

Is it possible to restrict the user to take snapshot of screen in C#

I have an application developed in C#. I need a mechanism to restrict the user to take screen shot when the application window on top.
Is it possible to restrict the user to take screenshot??
I have already listen the PrtScr Button to restrict the built in mechanism of windows to take screenshot.
But if user uses some capturing application to capture the skin.. I think there might be some windows event or such like thing, which is triggered when a a screenshot is taken. I need to know about it. Any help will be appreciated...
Already discussed/answered on SO: How do I prevent print screen
try searching a bit before asking! ;-)
In windows form application, Use this code in form keyup event,
if (e.KeyCode == Keys.PrintScreen)
{
Clipboard.Clear();
}
form keypreview should be true.

Adapting Modal Dialog Script to Firefox

I'm adapting my regression tests to test a web app in firefox. The biggest stumbling block seems to be how to automate the modal dialogs in firefox.
In ie I use variations of the script below, but it doesn't work in Firefox. Is there an alternative that will work in both ie and firefox?
popup=Thread.new {
autoit=WIN32OLE.new('AutoItX3.Control')
ret=autoit.WinWait(title,"",60)
if (ret==1)
puts "There is popup."
autoit.WinActivate(title)
button.downcase!
if button.eql?("ok") || button.eql?("yes") || button.eql?("continue")
autoit.Send("{Enter}")
else
autoit.Send("{tab}")
autoit.Send("{Enter}")
end
elsif (ret==0)
puts "No popup, please check your code."
end
}
at_exit { Thread.kill(popup) }
end
button.click_no_wait
check_for_popups("Message from webpage", "OK")
Given you are talking about a javascript created dialog, I really have to ask, is there a lot of value in actually testing those?
It basically amounts to testing the functionality of the browser
If you are talking about the type of popups described here http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups then I think the first solution, of overriding the javascript may well be your best cross platform option.
The problem with modal dialogs like this is that they are basically a UI even that is happening out at the OS level, it's no longer inside the browser DOM, and thus you need tools that are specific to the OS (like stuff that depends on win32ole, such as autoit) in order to generate the necessary interaction with the native UI and click buttons, send keystrokes etc. Most of the solutions presented should I think work with FF on windows (with proper renaming of expected window titles etc) but would fail on a mac or *nix OS. That means you need a different solution for each OS, which is a pain.
It might simply be easier to verify you can find the proper stuff that would fire the event in the HTML of the page, so you know an event WOULD be fired, and then override things so it isn't. After all it's not really your job to validate that the browser pops up a local dialog when something like alert('This is an alert box') is invoked in javascript. Your concern is that in the HTML a given element is coded to fire off the event that is needed e.g. that there's something like this onClick = 'javascript:x = confirm('Do you really want to do this');" affiliated with the element
I am experiencing a similar problem in Firefox (and I do have to test in Firefox). I can see the code calling the Javascript but when I try to override as described above nothing happens. Is there any kind of a workaround for this? Anticipated updates to Watir? ;-)

Resources