I've written scripts that do that but they got a little sloppy around the edges of the view area. One thing I would definitely call your attention to is the "viewportBound" property. You can use that property in the script method for opening popups. It avoids one problem that is easy to encounter where popups are visible but completely outside of the viewbox.
There is also the option of using the relative positioning options that are only available if you add a popup action to an event. Basically, you set "Position" to "Relative" and then select whether to offset it and which direction. Notice the "viewport bound" checkbox. That is important too.
