I have several components who's screen position depends on the resolution of the monitor on which the browser lives.
loginBox.x = (flash.system.Capabilities.screenResolutionX - loginBox.width) / 2;
loginBox.y = (flash.system.Capabilities.screenResolutionY - loginBox.height) / 2;
The problem I'm encountering is that the flash.system.Capabilities method pulls the resolution of the primary monitor attached to the computer. In most situations this isn't a problem but on one of my computers, I have a 1680x1050 and a 1440x900. At work, I have a 1920x1200 and a 1680x1050, so if I open the page in a browser on the smaller monitor, things are not centered and my tools panel is completely off to the right of the screen.
I have a block of javascript that I've tried, both in html and through php but the problem is that if I use either httpRequest or urlLoader to grab the html file, I get the source of html and if I try the php script, I get a script block that is attempting to write a cookie. If that page has not been visited prior to loading the flash site, or if cookies are disabled, it never gets written and I'm nowhere.
Is there a method that I can use, that doesn't rely on cookies, to detect the resolution of the monitor that the browser is actually on, and not just the resolution of the first monitor?
Why don't you use stage.stageWidth and stage.stageHeight for positioning purposes?
EDIT:
Ok, here's an example.
I'm considering that you'll place this code inside the container where loginBox also resides.
The stage property is undefined unless your object is added to the DisplayList, so you need to listen for when it's added and then position the loginBox.
// in the constructor
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
and then
private function onAddedToStage(evt:Event) {
loginBox.x = (stage.stageWidth - loginBox.width)/2;
loginBox.y = (stage.stageHeight - loginBox.height)/2;
}
Also, if you have stage scaleMode set to noScale (which maybe is the case), if you need to constantly reposition the loginBox based on browser resizing, you need to listen to an Event.RESIZE event
stage.addEventListener(Event.RESIZE, onResize);
If you're trying to communicate between flash and javascript, ExternalInterface is the way to go. Rather that writing a cookie, just grab the information from javascript, and send it to flash via a callback.
Related
I am working on an web crawler/site analyzer in php. What I need to do is to extract some tags from a HTML file and compute some attributes (such as image size for example). I can easily do this using a DOM parser, but I would also need to find the pixel coordinates and size of a html/DOM tree element (let's say I have a div and I need to know which area it covers and on which coordinate does it start and if). I can define a standard screen resolution, that is not a problem for me, but I need to retrieve the pixel coordinates automatically, by using a server-side php script (or calling some java app from console or something similar, if needed).
From what I understand, I need a headless browser in php and that would simulate/render a webpage, from which I can retrieve the pixel coordinates I need. Would you recommend me a open-source solution for that? Some code snippets would also be useful, so I would not install the solution and then notice it does not provide pixel coordinates.
PS: I see people who answered missed the point of the question, so it means I did not explain well that I need this solution to work COMPLETELY server-side. Say I use a crawler and it feeds html pages to my script. I could launch it from browser, but also from console (like 'php myScript.php').
maybe you can set the coordinates as some kind of metadata inside your tag using javascript
$("element").data("coordinates",""+this.offset.top+","+this.offset.left);
then you have to request with php
$html = file_get_contents($url);
$doc = new DOMDocument();
#$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('element');
foreach ($tags as $tag) {
echo $tag->getAttribute('data'); <-- this will print the coordinates of each tag
}
A Headless browser is an overkill for what you're trying to achieve. Just use cookies to store whatever you want.
So any time you get some piece of information, such as an X,Y coordinate, scroll position, etc. in javascript, simply send it to a PHP script that makes a cookie out of it with some unique string index.
Eventually, you'll have a large array of cookie data that will be directly available to any PHP or javascript file, and you can do anything you'd like with it at that point.
For example, if you wanted to just store stuff in sessions, you could do:
jquery:
// save whatever you want from javascript
// note: probably better to POST, since we're not getting anything really, just showing quick example
$.get('save-attr.php?attr=xy_coord&value=300,550');
PHP:
// this will be the save-attr.php file
session_start();
$_SESSION[$_GET['attr']] = $_GET['value'];
// now any other script can get this value like so:
$coordinates = $_SESSION['xy_coord'];
// where $coordinates would now equal "300,550"
Simple continue this pattern for whatever you need access to in PHP
I currently use Selenium 2 with a local Selenium web server and the PHP-Webdriver written by Facebook.
Now I want to write an automated test for the Facebook Like Button. Because this Button is loaded through an iframe, I first select this frame via $driver->frame(array('id' => 1)) (I Found out, that Facebook loads normally two frames and the second frame is the Like Button). After clicking the Like Button a new Frame is loaded, where the user also can send a comment to his wall. Unfortunately the focus is still on the Like Button Frame so that I have to switch to the second frame. How can I do this?
Because I do not use Selenium RC there is no Selenium.SelectFrame("relative=top") method. I also cannot use the method driver.switchTo().defaultContent() because I do not use the Java webdriver. It seems that I just can use methods specified in the JsonWireProtocol. How can I switch between frames or change the focus back to the top frame?
Python webdriver client has these methods for switching between iframes, windows and pop ups:
switch_to_active_element
switch_to_alert
switch_to_default_content
switch_to_frame
switch_to_window
switch_to_default_content is the one you need. Find its analog for php client.
UPDATE:
Since you mentioned JasonWireProtocol:
http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/frame
POST /session/:sessionId/frame
Change focus to another frame on the page.
If the frame ID is null, the server should switch to the page's default content.
I had this same problem. The issue I discovered, that the "frame" in the PHP webdrivers only switches to frames below the current one. So if you want to switch to a different frame that is above the current frame, you are out of luck. I had to select the main window, which essentially reset me to the top frame. From there I was able to select the correct frame.
//putting a sleep so the page can load
sleep(SLEEPTIME + 5);
//getting a list of windows on the page
$windows = $webdriver->getWindows(); // function below
//switching to the
$webdriver->selectWindow($windows[0]);
$webdriver->focusFrame('cpAppFrame');
public function getWindows() {
$request = $this->requestURL . "/window_handles";
$response = $this->execute_rest_request_GET($request);
return $this->extractValueFromJsonResponse($response);
}
We've been developing an affiliate system and would like to detect somehow that a compiled, SWF advert implements clickTAG or not. Is there any way to automate this process?
When I debug flash banners I use flasm (http://www.nowrap.de/flasm.html windows+linux) to decompile the swf file. You can both get output to console (-d) or dump it to a file:
$ flasm -d file.swf > out.txt
Then search the file/output for clickTag/clickTAG.
Requires exec privileges.
This is a complex problem.
The suggested solution only addresses the case of an incorrect clicktag (e.g. clickTAG vs clickTag). Here are other potential problems:
- no clickable layer, no clicktag code
- clickable layer with hard-coded URL
- clickable layer only covering a small portion of the banner
- All of the above in AS3 (flasm only supports AS2)
http://adopstools.net lets you submit a swf and check it for clicktags as well as other things
If I've understood correctly what you need to do, it should be possible to build a semiautomated testing swf by loading the ad, then simulating clicks on everything in its display tree.
You can pass parameters to a loaded swf using the data property of a URLRequest like so:
var loader:Loader = new Loader();
var req:URLRequest = new URLRequest("ad.swf");
var clickTagURL:String = "http://www.example.com";
req.data = new URLVariables("clickTAG=" + clickTagURL + "&clickTag=" + clickTagURL + "&clicktag=" + clickTagURL);
loader.load(req);
(Though you'll need to run that in a browser or standalone as the Flash IDE complains about query string parameters.)
Then you can step recursively through the display list triggering clicks:
testClicks(loader.content as DisplayObjectContainer);
function testClicks(target:DisplayObjectContainer):void {
var numC:uint = target.numChildren;
for (var i:uint = 0; i < numC; i++) {
target.getChildAt(i).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
if (target.getChildAt(i) is DisplayObjectContainer) {
testClicks(target.getChildAt(i) as DisplayObjectContainer);
}
}
}
If you set the folder with your test ad as trusted, or use the debug player, you'll be able to see if any of those clicks cause the ad to open a URL.
It's probably worth triggering MOUSE_DOWN and MOUSE_UP too in case the developer has used those instead, and obviously this won't reveal problems like very small click areas as jdangu mentions, but hopefully it's useful as a basic test.
You can use a clicktag checker like www.adbannerking.com It will reveal the clicktag that is in the SWF file. The software even allows you to change the clicktag accordingly without the need of the source files (.fla). At the same time you can check / change x amount of SWF files at the same time quickly.
I want to send some info back to my database when a user prints a certain web page. I can do this in IE with onbeforeprint() and onafterprint() but I would like to browser agnostic way of doing the same thing. Don't care which combination of technologies I have to use (PHP, MySQL, JavaScript, HTML) so long as it gets done. Any ideas?
EDIT:
Still having some problems with this. I tried the putting my function in my Print.css as an image, but I am messing it up some how. Then I tried just adding a event listener, but I cannot get that to work quite right either. If anyone can provide some more details on how I might call a function right before print in ANY browser I would appreciate it.
EDIT:
I am giving up on this for now, I have settled with another way of doing what I want. I look forward to the day when FireFox supports onbeforeprint() and onafterprint().
Many browsers now support window.matchMedia. This API allows you to detect when CSS media queries go into effect (e.g., rotating the screen or printing the document). For a cross-browser approach, combine window.matchMedia with window.onbeforeprint/window.onafterprint.
The following may result in multiple calls to beforePrint() and afterPrint() (for example, Chrome fires the listener every time the print preview is regenerated). This may or may not be desirable depending on the particular processing you're doing in response to the print.
if ('matchMedia' in window) {
// Chrome, Firefox, and IE 10 support mediaMatch listeners
window.matchMedia('print').addListener(function(media) {
if (media.matches) {
beforePrint();
} else {
// Fires immediately, so wait for the first mouse movement
$(document).one('mouseover', afterPrint);
}
});
} else {
// IE and Firefox fire before/after events
$(window).on('beforeprint', beforePrint);
$(window).on('afterprint', afterPrint);
}
More: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
I m not sure other browsers will allow you to. You could of course specify an image somewhere in a print stylesheet, which probably only will be called on a print, for the onbeforeprint
Try masking the native window.print() with your own...
// hide our vars from the global scope
(function(){
// make a copy of the native window.print
var _print = this.print;
// create a new window.print
this.print = function () {
// if `onbeforeprint` exists, call it.
if (this.onbeforeprint) onbeforeprint(this);
// call the original `window.print`.
_print();
// if `onafterprint` exists, call it.
if (this.onafterprint) onafterprint(this);
}
}())
Updated: comments.
I think that it's simply not possible to this properly. Or at least - not with any technology I know nor with any of the answers given previously.
Both using onafterprint and using serverside dynamic-image-generating script would tell you that the page was printed even when the visitor merely went to print preview mode and then canceled out.
However, I would like to learn how to get the proper information, so that I can be sure that page was actually printed.
is there a "document" property named ssh? It's a simple question. I've seen this in some code at work, but no one in the office wrote the code, so I'm stucked.
The line was document.ssh.firstPing(...)
firstPing was a method in the code, that is writen in js+php. But I've searched with eclipse throughout all the code and there is no ssh anywhere.
There's no standard ssh property on the document object in the Javascript DOM bindings. If you're loading Javascript libraries, they could always add one (one can add properties to document if one likes). For instance, this is perfectly valid:
document.foo = {
bar: function() {
alert("Hi there!");
}
};
document.foo.bar(); // alerts "Hi there"
More on the standard bindings here.
ssh has to have been defined in some script somewhere. Since your codebase is partially PHP it may be a generated script and that's why it's not showing up obviously.
A technique you might try is open your page in FireFox with Firebug and analyze the list of scripts it shows to have loaded (under the scripts tab). The advantage here is that Firebug shows you eval scripts, not just the static script files. Firebug also lets you search through these. Then you may be able to backtrack from there into where it's being defined based on phrases.