Determining When Web Page Is Loaded In Opera

I have a script which loads many web pages and closes ones that don’t contain a piece of text. It used a fixed delay (5 sec) after opening the url before testing the condition. Most times the page opens quicker than 5 sec but a small percentage (10-20%) take longer. Looking for an applescript method of signalling when the page finishes loading.

Spent about 2 days searching for an answer and found some Safari techniques which do not apply to Opera. Now seeking the advice of the forum members.


set varbr to "Opera"
tell application "System Events"
		tell process varbr
			open location hwurl
			delay 5
		end tell
		
		tell application "System Events"
			tell process varbr
				click menu item "Find." of menu "Edit" of menu bar item "Edit" of menu bar 1 of application process varbr of application "System Events"
				tell application "System Events"
					tell process varbr
						keystroke "No ads for"
						set var2 to value of static text of window 1 of application process varbr of application "System Events" as text
						if var2 = "1 of 1" then
							keystroke "w" using command down

Can you parse the source code for “” first? That should signal the page is loaded.

Try searching for page loaded in javascript on this site.

gl,
kel

Sorry but I’m a bit of a newbie web-wise. Your suggestion to search using “” works with Safari. For Opera the statement “set test_html to source of document 1” fails. Any suggestions how to obtain the Opera source.

on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

tell application "Safari"
	open location "http://www.redflagdeals.com"
	my waitforload()
	beep
end tell

Hello.

I have two questions for you, in order to try to solve your problem:

If you drop the Opera app onto the library window of AppleScript editor, do a dictionary for Opera turn up then?

B. If you indeed can see an Opera dictionary, do something look like execute javascript, or execute script, or do javascript, or do script, when you search for javascript, script respectively?

But, just saying, you may never be 100% sure if a page is totally loaded, by looking for the html tag, I also suspect, that if you install a javascript onto the document.ready() handler, not even that will guarrantee a perfect result, the problem is of course, that images, may just have started to load, so the page isn’t really ready, even if the page thinks it is!

So, checking for document ready, only takes you so far, but if you are a bit discerning about what kind of pages, you want to be sure of is loaded, then something may work for you. :slight_smile:

Unfortunately the Opera dictionary didn’t appear after dropping the icon into the Library window. In my situation, the full page with images isn’t necessary. I’m trying to search for text in the body of the page using CMD F then entering the text. If the results from the find are “1 of 1” then the text is present and can take appropriate actions. Kind of a brute force method since Opera doesn’t support the nicer “set test_html to source of document 1” statement. I thought about running the View Source menu item then searching the text but still wouldn’t know when the page was finished.

DOMContentLoaded seems suitable but I don’t know how to code if that (java?) event is true.

If no dictionary appears, then Opera isn’t a scriptable browser.

I suggest you move to Safari or Chrome if you want to script it, or perform the download of pages in the background with curl, and filter the resutlting pages with sed/awk/perl.

Thanks to all for your valuable suggestions. I’m probably more hack than proper coder so I appreciate the insights given.

Hello.

I’m sorry, but it is the truth, you just can’t script that with Opera on OS X. So either changing to another favourite browser, or using curl in a unix process in the background are the only two options.

It may please you to hear that using curl in a background unix process is your best option. :slight_smile:

Using Curl to download the full page, pretty much guarrantees you that the page will be as fully loaded, when it lands, (the fie containing the contents are closed after writing on your disk).

If I were you, I’d read up on curl, in the context of downloading, and also google for solutions involving curl that extracts data from downloaded html pages. Given that there exists a solution that doesn’t involve you watching it.

On the other hand, you can download the pages, look for the contents that should be there, and probably open that page in Opera.

Just for the hell of it, try the two scripts below, and see if any of them works, since every app on OSX should respond to the basic scripting commands, activate,quit, print and open (I believe).

tell application "Opera"
	open location "http://www.bbc.com"
end tell

and:

tell application "Opera"
	open "http://www.bbc.com"
end tell

I realize that I suggests that you’ll invert your workflow, instead of closing windows that doesn’t contain the text, this approach simply would not open windows with web pages that doesn’t contain the text.

You may have many reasons for that this approach won’t work for you, but I hope you consider it, in order to solve your problem. :slight_smile:

open location is part of Standard Additions. It opens the URL according to the launch services scheme settings
open is not a basic script command (print isn’t either). The four basic commands are launch, activate, reopen and quit

I’ve learned a lot already.

I won’t say I’m an expert but if you have questions on evacuated tube solar water heaters I can try the help. Here’s a photo of the unit on my roof.

At least it was then:

I see that the run command, aren’t there in the Standard suites, but I find the other commands there.

Open Location can be utilized by the OP, if he has configured Opera to be the default browser on his system, then I believe the Open location command will work on his machine, from AppleScript, even if Opera per say isn’t scriptable.

Just for the record, Here is Apple’s own documentation about what events an application should respond to/Are likely to get from OS X, its from 2008, but this is a document they hopefully would have revised. -They do revise docs! :slight_smile:

I won’t say I’m an expert but if you have questions on evacuated tube solar water heaters I can try the help. Here’s a photo of the unit on my roof.

That looks nice, here, it would hardly work for the summer months, my altitude is almost the same as Anchorage, Alaska. :slight_smile:

You can do wonderful things with your Mac, once you have learned to script it. AppleScript is also faboulous for compiting numbers. :slight_smile:

Yes “open location” works and Opera is the default browser. Opera interprets the plain “open” as file open “file:///http/::www.bbc.com” and replies as an error.

Been playing with curl and I think it will work.
Thanks again!

Good Luck! :slight_smile:

I thought you could use javascript on any browser.

Hi,

Don’t mind me but I just thought that Apple added javascript, so you you could use it in any browser.

I don’t have the code down though.

gl,
kel

I think you’ll need a scripting dictionary kind of, in order to use JXA (Javascript for Automation) , as well as for AppleScript, as I have understood it.

Read the full piece here

Opera dropped Applescript - and AppleEvents support some time ago, that is at least what I have read when I googled the subject earlier today. :confused:

I’m quite sure that you can use javascrripot in any mac browser, but hopefully we’ll hear from one that is knowing about that stuff.

BTW, I think we’re entering into a new ice age.