General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
How to read URL variable from macro
Posted by: DannyD
Date: December 18, 2009 03:56PM

Hello K-Meleon macro developers,

I have got one question that you can probably answer in a minute, but I am unable to find the solution anywhere in the documentation or this forum.

What I want to do is to call a new URL, based on text that the user has selected on the current page plus the content of one variable that is part of the current page's URL.

(FYI: I need this to substitute a missing link that the vendor of some braindead proprietary application forgot to provide.)

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- extra_link.kmm -------------------------------------------------------
#
# Dependencies        : extra_link.kmm
# Resources           : -  
# Preferences         : -
# Version             : 0.1  2009-12-18   DannyD
# ---------------------------------------------------------------------------------

$sBaseUrl = "http://host/otherpage?action=do";
$sUrlVariable = #DONT_KNOT_HOW_TO_ACCESS#;

_ExtraLink{
	$sFullUrl = $sBaseUrl . "&THIS_ID=&" . $sUrlVariable . "&OTHER_ID=" . $SelectedText;
	opentab( $URL );
}

_ExtraLink_BuildMenu{
	# add another option to context menu of selected text
	setmenu( SelectedText, macro, "Go there...", _ExtraLink );
}
		
$OnInit = $OnInit . "_ExtraLink_BuildMenu;";
$macroModules = $macroModules ."extra_link;";

I think the idea is clear: If I open the page "http://host/thispage?action=do&THIS_ID=123&what=ever" and mark the text "456" on that page, then I want the context menu entry to call "http://host/otherpage?action=do&THIS_ID=123&OTHER_ID=456"

But how can I access the contents of variable "THIS_ID" from a macro?

As always, I am certain that the answer is all too simple, but I just cannot find it anywhere.

Cheers
DannyD



Edited 2 time(s). Last edit at 12/23/2009 11:23PM by DannyD.

Options: ReplyQuote
Re: How to read URL variable from makro
Posted by: siria
Date: December 18, 2009 04:46PM

Is that "123" always the same number of characters? If so, perhaps crop the original url-string after the xxth character, then replace the string "thispage" with "otherpage" and append the rest... But if it's different, no idea... (and no wonder, seeing that I just completed my very first own macro, LOL!)

Options: ReplyQuote
Re: How to read URL variable from macro
Posted by: DannyD
Date: December 18, 2009 07:00PM

No, unfortunately the length of the value varies, and so does the position of the variable in the URL string.



Edited 1 time(s). Last edit at 12/23/2009 11:17PM by DannyD.

Options: ReplyQuote
Re: How to read URL variable from makro
Posted by: JamesD
Date: December 18, 2009 07:23PM

I am not sure how you want to get to first URL but this change will insert the selected text after "&OTHER_ID=".

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- extra_link.kmm -------------------------------------------------------
#
# Dependencies        : extra_link.kmm
# Resources           : -  
# Preferences         : -
# Version             : 0.1  2009-12-18   DannyD
# ---------------------------------------------------------------------------------


_ExtraLink{
$_EL_CurURL = $URL;
$_EL_NextURL = urlencode($_EL_CurURL . "&OTHER_ID=" . $SelectedText);
alert($_EL_NextURL, "This is the next URL  DEBUG", INFO);
opentab($_EL_NextURL);

}

_ExtraLink_BuildMenu{
	# add another option to context menu of selected text
	setmenu( SelectedText, macro, "Go there...", _ExtraLink );
}
		
$OnInit = $OnInit . "_ExtraLink_BuildMenu;";
$macroModules = $macroModules ."extra_link;";


Options: ReplyQuote
Re: How to read URL variable from makro
Posted by: JohnHell
Date: December 18, 2009 09:11PM

Quote
DannyD
But how can I access the contents of variable "THIS_ID" from a makro?

If the name of THIS_ID is ever the same is very simple, if not, you could add a prompt to force you (the user) to request it.


You need to search for THIS_ID and it's as simple as use index and than substr to get value.

Let's say the variable is &numbers. First you search for it in the $BaseUrl.

$whereareyou = index($BaseUrl, "&numbers") #this gives the position of the variable.

Ok, &numbers has 8 characters plus the equal sign, 9 then.

$variablevalueis = substr($BaseUrl, $whereareyou+9, 3);

Ok, you may be asking why I wrote $whereareyou+9 and 3 up there. With the first thing we are telling substr where must it start to extract the string from (after the equal) and the 3 is the length of the numbers variable. I'm assuming the numbers variable would have ever 3 characters.

If for any reason the length of the variable value varies, you then must search for the next "&" that indicates the beginning of the new variable. You can do this by applying what I explained here.



Edited 1 time(s). Last edit at 12/18/2009 09:13PM by JohnHell.

Options: ReplyQuote
Re: How to read URL variable from macro
Posted by: DannyD
Date: December 18, 2009 09:43PM

Exactly this is what came to my mind in the meantime!

Just for the records, if somebody ever tries to solve the same problem:

$sVarName = "THE_NAME";
$iVarStart = index($URL, $sVarName);
$sVarContent = substr($URL , $iVarStart + length($sVarName) +1);
$iNextVarStart = index($sVarContent, "&");
if ($iNextVarStart > -1) {
	$sVarContent = substr($sVarContent, 0, $iNextVarStart);
}

If the current page's URL is "http://server/path/page.html?THE_NAME=THE_VALUE&foo=bar"
then the lines above put "THE_VALUE" into $sVarContent.

Thanks everybody
DannyD



Edited 1 time(s). Last edit at 12/23/2009 11:22PM by DannyD.

Options: ReplyQuote


K-Meleon forum is powered by Phorum.