General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 06:15PM

I started to use the OnLoad function (or macro call) so when a page it's loaded executes some javascript.

Well, the thing is that this works only when the window is focused, but if you open the window in the background and you don't focus it before it loads, the macro doesn't work (the JS to be executed).

In the other hand, I can't use OnActivateWindow because it's a toogle macro so each time I focus the window it will execute giving undesired results.

Any ideas?

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 07:52PM

Nah, I'm playing with this, and I think I would have to use an accel :/

I was trying to avoid a mouse click and keyboard shortcuts but I can't.

Anyway, to avoid wasting this topic, can you test it on 1.5.x (I still use 1.1.6).

I don't think this can be solved due to its nature so I'm not going to suggest as a feature.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 09, 2009 07:56PM

I think the OnLoad function works, but the macro is working only for the focused window/tab. That is to say that OnLoad will inform me when a tab finishes loading in a background tab, but any code called by that OnLoad function runs in the tab with focus.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 07:59PM

mmm... and, how to attach the code to run to an specific window? maybe by url? because I think is not possible to know the window name :/

EDIT: Mmmm maybe opening the new window through a macro call with JS script... and assigning a name with window.open...


(I'm complicated, Am I not?)



Edited 1 time(s). Last edit at 10/09/2009 08:03PM by JohnHell.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 09, 2009 08:23PM

My memory is not as good as it once was, but I think someone on the forum informed that macros only work in the tab with focus. Am I correct in assuming that you need some action preformed on a window/tab which is loading in the background?

The closest I have been able to come to that is to save the URL of the current page, open the second page and do an action based on the OnLoad event, and have the second page reload the original page. As you might guess, that is not real pretty and a resource hog.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 08:30PM

Quote
JamesD
My memory is not as good as it once was, but I think someone on the forum informed that macros only work in the tab with focus. Am I correct in assuming that you need some action preformed on a window/tab which is loading in the background?

That it is. Usually I have the window in front of others, but when I don't have I hoped that still works :/

Quote
JamesD
As you might guess, that is not real pretty and a resource hog.

Yep, that is what I'm seeing...

I was trying right now to InjectJS or something to make a window.focus to that window instead what I tell before, but I think... no way grinning smiley

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 09, 2009 08:45PM

Question about the page to which you wish to add some JS code. Is that page a known URL ( always the same ) or does it vary?

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 08:53PM

Vary, but I have a checking macro. It's about youtube pages with fmt, if the page matches it executes, else nothing.

(example: http://www.youtube.com/watch?v=rnW5FjcT69k&fmt=18)

But as you said, macros only run on focused window, indeed, the error console is showing it confused smiley (bla, bla ,bla, this funcion may not load from the current window, indeed it can't)

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 09, 2009 11:09PM

I discovered that alert, confirm and prompt, written before the rest of the code, brings the window to front and then applies the rest of the macro code, but is not a way as annoy :/ prompt doesn't annoy very much (doesn't have sound) but anyway..., not good.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: mslayer
Date: October 10, 2009 12:50AM

Yes it works that way, OnLoad will give you an event when the tab has focus.
I was thinking in something like retrieving the url from all the tabs from the cfg files, and which one was the last in the "kmeleon.plugins.sessions2.Last Session.window0.tab#" list , but really I don't know how. It's an idea, can that be done?

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 11, 2009 01:38AM

Maybe I have to retract something I said before. Perhaps a macro can run on a tab not in focus. I think now that it cannot change from one page to another, but if it starts on the page in the background tab, it can run there. Try the following macro. Come to this thread and right click on the youtube link that JohnHell provided to load that page in the background. Give it time to load and then click on the tab. All items in the youtube page should be highlighted.

YTubeTest.kmm

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- YTubeTest.kmm
# ---------- K-Meleon Macro Language youtube testing ---
#
# Dependencies			: main.kmm
# Resources				: - 
# Preferences			: -
# Version				: 0.1 Oct. 10, 2009 
# --------------------------------------------------------------------------------

_YTubeTest_RunLoad {

$YTubeTest_OldURL = $URL ;
if (hostname($YTubeTest_OldURL) == "www.youtube.com") {
	#alert($YTubeTest_OldURL, "$YTubeTest_OldURL DEBUG", INFO);
	if (index($YTubeTest_OldURL, "&fmt=") != -1) {
		#alert("'&fmt=' found in URL", "DEBUG", INFO);
		id(ID_EDIT_SELECT_ALL);
		}
	}
}

$OnLoad=$OnLoad."_YTubeTest_RunLoad;";
$macroModules=$macroModules."YTubeTest;";
## *** END OF YTubeTest.kmm ***


Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 11, 2009 07:02PM

Maybe it can load, but the macro language as itself, but not with javascript.

Maybe my problem is that I use open("javascript:function();").

Anyway, I can't test your code on 1.1.6 (if it was intended for me) and I'm lazy to try it today tongue sticking out smiley

Well, now, I don't care much, look at my kmm (the macro is intended for several future sites that is why I created like this):


$OnLoad = $OnLoad."testjssite;";

$onJS_ytb = "youtube.com/watch?v=";

testjssite{

$onJSidx_ytb = index($URL, $onJS_ytb)+index($URL, "&fmt=");

$onJSidx_ytb >= 11 ? &onJS_ytb : 0 ;

}

onJS_ytb{
open("javascript:yt.www.watch.player.enableWideScreen(!_hasclass(_gel('baseDiv'), 'watch-wide-mode'), true);");
}

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 11, 2009 08:27PM

New version of code for 1.1.x versions. I don't know what to expect so I don't know if the results are what you wish or not. I don't think I have exactly the same tests that you have.

YTubeTest.kmm
#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- YTubeTest.kmm
# ---------- K-Meleon Macro Language youtube testing ---
#
# Dependencies			: main.kmm
# Resources				: - 
# Preferences			: -
# Version				: 0.2   Oct. 11, 2009 
# --------------------------------------------------------------------------------

_YTubeTest_RunLoad {
$YTubeTest_OldURL = $URL ;
$YTubeTest_OldURL) == "www.youtube.com" ? &_YTubeTest_Pass1 :0;
}
_YTubeTest_Pass1 {
index($YTubeTest_OldURL, "&fmt=" != -1 ? &_YTubeTest_Pass2 :0;
}
_YTubeTest_Pass2 {
open("javascript:yt.www.watch.player.enableWideScreen(!_hasclass(_gel('baseDiv'), 'watch-wide-mode'), true);"); 
}

$OnLoad=$OnLoad."_YTubeTest_RunLoad;";
$macroModules=$macroModules."YTubeTest;";
## *** END OF YTubeTest.kmm ***


Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: disrupted
Date: October 11, 2009 09:38PM

i don't know if this could be of any help,; i use this macro(js by lexi1) to automatically add a download link on youtube sites.. as johnhell hs mentioned, it has that problem.. if the page/tab in question is not the one in focus it won't inject the js

http://kportal.clix5.com/siria/youtubedownload.kmm

# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)

# ----- link to  download video files on youtube (js by Lexi1) 

tubecheck{
substr($URL,0,29)=="http://www.youtube.com/watch?"?&youtubevid:0;
}

youtubevid{
$_jscurrent=getpref(BOOL,$pref_JavaScript);
setpref(BOOL,$pref_JavaScript, true);
injectJS("(function(){var getVideo=function(flv){var url=location.href;if(url.indexOf('youtube.com/watch?v=')!=-1)return'http://www.youtube.com/get_video?fmt='+(window.isHDAvailable?'22':'18')+'&video_id='+window.swfArgs['video_id']+'&t='+window.swfArgs['t'];if(url.indexOf('metacafe.com/watch')!=-1)return/http:\\/\\/[^&=]+flv[^&]*/i.exec(flv.replace('&gdaKey=','?__gda__='));return/http:\\/\\/[^\\s\\x22=&?]+flv/i.exec(flv)};var createLink=function(l,t){var oLnk=document.createElement('a');oLnk.setAttribute('href',l);oLnk.appendChild(document.createTextNode(t));oLnk.setAttribute('style','display:inline-block;font:12px Bitstream Vera sans;');return oLnk};var showLink=function(oEle,oSrc,oFlv){if(!oEle||!oSrc)return;var cName='ujs_downlink';var p=oEle.previousSibling;if(p&&p.className==cName){oEle.parentNode.removeChild(p);return};var oDiv=document.createElement('div');oDiv.className=cName;oDiv.appendChild(createLink(oSrc,'Download'));if(oFlv){var v=getVideo(decodeURIComponent(oFlv));if(v){oDiv.appendChild(document.createTextNode(' ('));oDiv.appendChild(createLink(v,'Video'));oDiv.appendChild(document.createTextNode(')'))}};oEle.parentNode.insertBefore(oDiv,oEle)};var getParam=function(e,n){var v='';var re=new RegExp('^('+n+')$','i');var param=e.getElementsByTagName('param');for(var i=0,p;p=param;i++){if(p.getAttribute('name').match(re)){v=p.getAttribute('value');break}}return v};var searchEmbed=function(w){if(!w)return;var obj=w.document.getElementsByTagName('object');for(var i=0,oi;oi=obj;i++){showLink(oi,oi.getAttribute('data')||oi.getAttribute('src')||getParam(oi,'movie|data|src|code|filename|url')||(oi.getElementsByTagName('embed').length&&oi.getElementsByTagName('embed')[0].getAttribute('src')),getParam(oi,'flashvars'))};var em=w.document.getElementsByTagName('embed');for(var i=0,ei;ei=em;i++){if(ei.parentNode.tagName.toLowerCase()!='object')showLink(ei,ei.getAttribute('src'),ei.getAttribute('flashvars'))};for(var j=0,f;f=w.frames[j];j++){try{arguments.callee(f)}catch(e){}}};searchEmbed(window.top)})()");
}


$OnLoad=$OnLoad."tubecheck;";
# --------------------------------------------------------------------
$macroModules=$macroModules."youtube downloader;"; 

(forum messes up js code)

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 11, 2009 09:44PM

Beyond some brackets problems and some test with the variables, your macro is just mine, James winking smiley and therefore, it executes as mine.

About what you said, disrupted, the JS is being injected (for example you still can kill flash trough keyboard shortcut or any injected code to any macro or js through other systems).

So, the problem comes only beyond the open command or the direct interaction with the background window (I use windows instead tabs, but as you are testing with tabs, it doesn't matter). I say this because if you place an alert, before the open command (with my code), yes, the alert is being executed in the background page and then brings it to front, but the open command, alone, by itself, can't be executed in the background.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JamesD
Date: October 12, 2009 12:47AM

I don't know much about JS. Does the open(javascript:...) work if the youtube site is opened in the tab with focus? I don't see any difference.

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: disrupted
Date: October 12, 2009 01:16AM

yes it works, the download link is right under the video title like this:


but only works when the youtube tab is at focus /active tab when the page is still loading..(as as it loads the link displays), if i'm browsing another tab, like youtube search; it will not inject

use the macro at link, forum messes up the js code

it's kinda annoying when you need to check another tab and must wait for the tab you know it will inject to finish loading first.. i'm not sure if it's a bug or how kko intended it to be

Options: ReplyQuote
Re: OnLoad function doesn't work if not focused window
Posted by: JohnHell
Date: October 12, 2009 12:43PM

Quote
JamesD
I don't know much about JS. Does the open(javascript:...) work if the youtube site is opened in the tab with focus?
Of course.

Quote
JamesD
I don't see any difference.
Use my code, yours is "buggy" tongue sticking out smiley


EDIT, answering disrupted: Yes, you are right, I didn't think that when you KillFlash, the JS is injected when you execute de macro, not while loading, that is why it works :/

I don't think it is a bug, just the way it may work, but maybe the onload function wasn't intended for make tasks while in the background or independent for each page loading.



Edited 1 time(s). Last edit at 10/12/2009 12:46PM by JohnHell.

Options: ReplyQuote


K-Meleon forum is powered by Phorum.