Safari 5.1 extension development error -> event.userInfo returns null when it try to get selected text - safari-extension

I am trying to create a simple safari extension. My current version of Safari is 5.1.7 running in Snow Leopard.
I have 2 documents :
global.html
<!DOCTYPE HTML>
<script>
safari.application.addEventListener("command", performCommand, false);
function performCommand(event) {
if (event.command === "traducir") {
var query = event.userInfo;
alert(query);
query = query.replace(/\s+/g,"+");
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = "http://translate.google.es/#en/es/" + query ;
}
}
</script>
and the injected script : injected.js
document.addEventListener("contextmenu", handleMessage, false);
function handleMessage(msgEvent) {
var sel = '';
sel = window.parent.getSelection()+'';
sel = sel.replace(/^\s+|\s+$/g,"");
safari.self.tab.setContextMenuEventUserInfo(msgEvent, sel);
}
The extension is very simple :
1- When the user selects one text or word, click right-button and select the item of the contextual menu that raise the function.
2- The injected file gets the value of the selected text and it shared with the global.html through the userInfo.
3- The global.html script open a new tab with the url of google translate.
The problem is that event.userInfo is always NULL. I was searching in Google and all the examples are like this and I donĀ“t know where the problem is and why it returns always NULL.

This could possibly be due to a too strict setting for "Access Level" in the Extension Builder. Try setting the dropdown menu to "All", and tick the "Include Secure Pages" checkbox.

Related

Why I can't set global variable to child window in IE11?

I am trying to do new functionality.
I was able to achieve what I want in modern browsers but not in the IE11.
if (!window.IS_PRESENTING) {
childWindow = window.open(window.location.pathname);
childWindow.IS_PRESENTING = true;
setupMainWindow();
} else {
setupChildWindow();
}
In modern browsers IS_PRESENTING is being set on the child windows. But in the IE11 it doesnt work and the browser keeps opening new windows...
How can I fix this? Any suggestions?
I can reproduce the issue in IE 11. As a workaround, in IE 11, you can get the value of window.IS_PRESENTING in the opening window and set the opening window's IS_PRESENTING value according to it :
In the opening window:
targetWindow = window.opener;
var IS_PRESENTING = !targetWindow.IS_PRESENTING; //targetWindow.IS_PRESENTING can get the value of window.IS_PRESENTING in child window
alert("The value of IS_PRESENTING is " + IS_PRESENTING);
Reference link: Window.opener

How to check multiple PDF files for annotations/comments?

Problem: I routinely receive PDF reports and annotate (highlight etc.) some of them. I had the bad habit of saving the annotated PDFs together with the non-annotated PDFs. I now have hundreds of PDF files in the same folder, some annotated and some not. Is there a way to check every PDF file for annotations and copy only the annotated ones to a new folder?
Thanks a lot!
I'm on Win 7 64bit, I have Adobe Acrobat XI installed and I'm able to do some beginner coding in Python and Javascript
Please ignore the following suggestion, since the answers already solved the problem.
EDIT: Following Mr. Wyss' suggestion, I created the following code for Acrobat's Javascript console to be run only once at the beginning:
counter = 1;
// Open a new report
var rep = new Report();
rep.size = 1.2;
rep.color = color.blue;
rep.writeText("Files WITH Annotations");
Then this code should be applied to all PDFs:
this.syncAnnotScan();
annots = this.getAnnots();
path = this.path;
if (annots) {
rep.color = color.black;
rep.writeText(" ");
rep.writeText(counter.toString()+"- "+path);
rep.writeText(" ");
if (counter% 20 == 0) {
rep.breakPage();
}
counter++;
}
And, at last, one code to be run only once at the end:
//Now open the report
var docRep = rep.open("files_with_annots.pdf");
There are two problems with this solution:
1. The "Action Wizard" seems to always apply the same code afresh to each PDF (that means that the "counter" variable, for instance, is meaningless; it will always be = 1. But more importantly, var "rep" will be unassigned when the middle code is run on different PDFs).
2. How can I make the codes that should be run only once run only at the beginning or at the end, instead of running everytime for every single PDF (like it does by default)?
Thank you very much again for your help!
This would be possible using the Action Wizard to put together an action.
The function to determine whether there are annotations in the document would be done in Acrobat JavaScript. Roughly, the core function would look like this:
this.syncAnnotScan() ; // updates all annots
var myAnnots = this.getAnnots() ;
if (myAnnots != null) {
// do something if there are annots
} else {
// do something if there are no annots
}
And that should get you there.
I am not completely positive, but I think there is also a Preflight check which tells you whether there are annotations in the document. If so, you would create a Preflight droplet, which would sort out the annotated and not annotated documents.
Mr. Wyss is right, here's a step-by-step guide:
In Acrobat XI Pro, go to the 'Tools' panel on the right side
Click on the 'Action Wizard' tab (you must first make it visible, though)
Click on 'Create New Action...', choose 'More tools' > 'Execute Javascript' and add it to right-hand pane > click on 'Execute Javascript' > 'Specify Settings' (uncheck 'prompt user' if you want) > paste this code:
.
this.syncAnnotScan();
var annots = this.getAnnots();
var fname = this.documentFileName;
fname = fname.replace(",", ";");
var errormsg = "";
if (annots) {
try {
this.saveAs({
cPath: "/c/folder/"+fname,
bPromptToOverwrite: false //make this 'true' if you want to be prompted on overwrites
});
} catch(e) {
for (var i in e)
{errormsg+= (i + ": " + e[i]+ " / ");}
app.alert({
cMsg: "Error! Unable to save the file under this name ('"+fname+"'- possibly an unicode string?) See this: "+errormsg,
cTitle: "Damn you Acrobat"
});
}
;}
annots = 0;
Save and run it! All your annotated PDFs will be saved to 'c:\folder' (but only if this folder already exists!)
Be sure to enable first Javascript in 'Edit' > 'Preferences...' > 'Javascript' > 'Enable Acrobat Javascript'.
VERY IMPORTANT: Acrobat's JS has a bug that doesn't allow Docs to be saved with commas (",") in their names (e.g., "Meeting with suppliers, May 11th.pdf" - this will get an error). Therefore, I substitute in the code above all "," for ";".

How to submit a form in Geb (WebDriver) that has no submit button

I'm building up a test in Geb (WebDriver) that has the need to work with a form that has no submit button. From the user's perspective, it is as simple to use as typing in the search term and hitting the enter key on their keyboard.
Using Geb in a purely script form I can get around this by appending the special key code to the text being typed in, as seen in the following:
import org.openqa.selenium.Keys
$('input[id=myInputField]') << "michael"+Keys.ENTER
That works fine. But if I want to use Geb's recommended Page Object pattern (http://www.gebish.org/manual/0.7.1/pages.html#the_page_object_pattern), I don't see what I should do. What do I define in the content section of my EmployeeSearchPage object to duplicate the missing searchButton and its "to" object reference that tells Geb how to handle the resulting page?
class EmployeeSearchPage extends Page {
static url = "http://localhost:8888/directory/"
static at = { title == "Employee Directory" }
static content = {
searchField { $("input[id=myInputField]") }
// THE FOLLOWING BUTTON DOESN'T EXIST IN MY CASE
searchButton(to: EmployeeListPage) { $("input[value='SUBMIT']") }
}
}
I realize that I could add a submit button to the form that I could for the test and use CSS to position it out of the user's view, but why should I have to adapt the app to the test? Things should work the other way around.
I've been evaluating a lot of web testing frameworks and find that this type of form presents a problem for many of them - at least as far as their documentation is concerned.
Any ideas? Thanks!
You don't need to use js integration to achieve what you want.
You can also define methods on your page class, not only content. You could implement a submit method that would do what you are looking for in the following way:
class EmployeeSearchPage extends Page {
static url = "http://localhost:8888/directory/"
static at = { title == "Employee Directory" }
static content = {
searchField { $("input[id=myInputField]")
}
void submitForm() {
searchField << Keys.ENTER
browser.page EmployeeSearchResultsPage
}
}
and then to use it:
to EmployeeSearchPage
searchField << 'michael' // searchField = 'michael' would have the same effect
submitForm()
Geb provides support to execute JavaScript in the context of the browser, details can be found here in the Geb documentation.
You could use this to submit the form exactly like you would submit it using JavaScript in the webapp itself. For example, if you are using jQuery it would be as simple as:
js.exec('$("#myForm").submit()')

Get value by getProperties in Titanium

I have two window :
window1 : have an option_dialog width 3 option : 'Video','Image','Document'
var optionsDialogOpts = {
options:['Video','Image','Document'],
title:'Type'
};
var dialog_search_click = Titanium.UI.createOptionDialog(optionsDialogOpts);
dialog_search_click.addEventListener('click',function(e){
//spinner_search_click.setTitle(e.index);
if(e.index==0)
{
//save option select All
Ti.App.Properties.setString('option_dialog','0');
}
if(e.index==1)
{
Ti.App.Properties.setString('option_dialog','1');
}
if(e.index==2)
{
Ti.App.Properties.setString('option_dialog','2');
}
})
window2 : display option that selected in window1..
var option=Ti.App.Properties.getString('option_dialog');
Ti.API.info(option);
In the first time, option is displayed in window2 is true but when click back to window1 and select other option in option_dialog and go to window 2,it also only display option as the first time ,not change.Can you help me.
This code is right but, I thought one error with this.
your window1 and window2 both code in one .JS file. so this problem is ocurring.
var option=Ti.App.Properties.getString('option_dialog');
This line is not update always, it store information once.
You try this type,
add a new Button on Second window and check on Click Event this is working properly.
button.addeventListener('click',function(){
var option=Ti.App.Properties.getString('option_dialog');
alert("Option :- " + option);
});
If, this is working then. I think you can understand, What i want to say.
Cheers.....!

ROR + Reopen a new tab in same window using RUBY code

In my project, I want to re-open a new tab using ruby code. When user clicks at attachment link, then that pdf should be open in new tab of same window. I tried a lot but I am not getting the way to solve it. Please guide me.
I am not sure if this is possible using Ruby since it deals with the UI part. It is indeed very much possible using HTML and Jquery.
You could simply set the target attribute as blank in the hyperlink redirecting to the PDF and it will open the file in a new tab. Something similar to this :
<a href="http://www.mysite.com/files/xyz.pdf" target="_blank">
If you want to use JQuery for this, you can try something like this :
$(document).ready(function() {
$("a").click(function() {
link_host = this.href.split("/")[2];
document_host = document.location.href.split("/")[2];
if (link_host != document_host) {
window.open(this.href);
return false;
}
});
});