Author Topic: Need macro help  (Read 4301 times)

0 Members and 1 Guest are viewing this topic.

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Need macro help
« on: November 17, 2009, 02:31:52 AM »
I have some external links at my site.  The problem I'm having is that when a user activates a link, the log doesn't log this activity and therefore I don't know if the links are being used.  I have not been successful in creating a macro that works for me.

What I would like to see in the log is an entry when a external link is activated with the name of the link used. 

A perfect spot for this would be in hfs.events.

Can anyone help me out with this request?


   
maverick

Offline bacter

  • Operator
  • Tireless poster
  • *****
  • Posts: 682
    • View Profile
Re: Need macro help
« Reply #1 on: November 17, 2009, 07:54:30 AM »
Hi maverick

The basic problem to get a log about links clicked in your site is not how to write a macro. First, we have to understand, what a link is in our template. Also if we build in the template lists of links apparently the same way as the list of files, there is one basic difference: A reference to a file on our server creates a request to our server, and a link creates a request to the site where the link points to. Once the user has our page with links, we can shutdown our server and the user can still use the links, as we are not involved in serving the pages of the link.

So, to get some log, we must do two things at the same time when a link is clicked on our page:
a) create a 'non-destructive' request to our server. Non destructive means here, it should not load a new page from the server, as this would replace aour page and no further link could be done, nor should it initiata a download of a file like if the user would have clicked to a file for download. With this request we should pass some information to our server that indicates what link was pressed.
b) once the request to our server is done, execute the wanted link.

I found a workaround to achieve both things:

1) place somwhere in your template a small image, with a label (id) named 'fake'.
Code: [Select]
<img id="fake" src ="/dummy.jpg">add this dummy.jpg to your root folder.

2) place the following javascript in your header:
Code: [Select]
<script type="text/javascript">
function fakelink(url)
{
document.getElementById("fake").src="/dummy.jpg?s="+url;
window.open(url);
        }
</script>
This script, when invoked, reloads the source of our img placed in the template. Adding the urlvar with '?s=....' we pass the url of the link: This moves the desired information to our server, at the same time it avoids that the img gets loaded from the caché (at least for different links, using the same link more than one time could be lost , but it's ok so if we want only to know that a user has used the link. If we would register every time the user uses the link in one session, we could add some time-information in the urlvar)

Te request to reload this img gives now a log entry like the following:
17/11/2009 8:24:16 192.168.1.3:1632 {lan} Fully downloaded - 6,30 KB @ 1802 KB/s - /dummy.jpg?s=http://www.rejetto.com/wiki/index.php/HFS:_Template_symbols
if the url we link to is http://www.rejetto.com/wiki/index.php/HFS:_Template_symbols

Once the img is reloaded, a new window or tab with the page of the site we want to link is opened !

3) To invoke this script, we have to change in the [link] section of our template the anchor code to the following:
Code: [Select]
<a href="%encoded-folder%" onclick="fakelink('%item-url%')">%new%<img align=top src="/~img_link">%item-name%</a>
4) The link works as expected, and you have information of a completed download in your server and in your log. It's up to you what you do with that info in the log. Maybe knowing a download of 'dummy.jpg' means that a link was used is enough, otherwise use a event script on the 'on download completed' event to create a log entry as you like it. ;)

--------------------
PD: Did some test how it works with different browsers:
Firefox + Opera: As expected, new tab is opened and log shows 'requested get' + 'fully downloaded with full urlvar info.
Chrome: Opens new tab, but only logs  'requested get' - no download done
Safari: Opens new window, but no request for reloading img is passed to hfs :-(
IE 8: (surprisingly !!) Opens new window, and log shows 'requested get' + 'fully downloaded with full urlvar info.

« Last Edit: November 17, 2009, 10:18:19 AM by bacter »
your computer has no brain - use your own !

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Re: Need macro help
« Reply #2 on: November 17, 2009, 11:27:25 AM »
Thanks bacter.   :)

I tried it with Opera and it works as you indicated.  Here is an example of the log results received ...

04:48:12 AM Guest@127.0.0.1:1948 Fully downloaded - 799 B @ 26 KB/s - /dummy.jpg?s=http://www.rejetto.com/hfs/?f=dl

However in this example, if I didn't know what that url is actually for, that log entry with the urlvar info doesn't help me.  The link name for this example is "Get Latest Stable HFS Direct From Author" and my goal was to try and get this info into the log. 

Also, if at all possible I didn't want to use javascript and thought it could be done with a macro.

Maybe what I am trying to do is impossible.   ???
maverick

Offline bacter

  • Operator
  • Tireless poster
  • *****
  • Posts: 682
    • View Profile
Re: Need macro help
« Reply #3 on: November 17, 2009, 11:53:51 AM »
Quote
Also, if at all possible I didn't want to use javascript and thought it could be done with a macro.

Maybe what I am trying to do is impossible.


To work with all links, you have to initiate the request to hfs AND to the liked site from the browser - and there are no macros!  :)

To get the text in your log, change the script , and where it is called in link add as a second parameter the %item-name% (perhaps you have to encode the spaces to %20 to get it working - not tested.)


To have the name of the link, you chan change the script adding a second parameter, to have there urls and name

Code: [Select]
function fakelink(url,[color=green]lname[/color])
{
document.getElementById("fake").src="/root/default.jpg?s="+[color=green]lname[/color];
window.open(url);
}

There exists another way to call webpages form a link only with macros and without javascript using the load macro, but this does not work for all websites, as they check the UA in the header, and hfs is not recognized like a browser and many sites then don't serve the request.

Anyway, if you use only links that work when loaded from hfs, there may be another way to get it. I will try and post later.
your computer has no brain - use your own !

Offline bacter

  • Operator
  • Tireless poster
  • *****
  • Posts: 682
    • View Profile
Re: Need macro help
« Reply #4 on: November 17, 2009, 01:04:32 PM »
Hi Maverick

found a simple solution with macros using the refresh meta! ;D ;D ;D ;D ;D

Add a section [extlink] to your template:

Code: [Select]
[extlink]
{.set|tt|{.urlvar|site.}.}
{.set|tt|{.replace|(que)|?|{.replace|(and)|&amp;|{.^tt.}.}.}.}
{.add to log|Link:{.urlvar|sname.}.}
<html><head>
<meta http-equiv="Refresh" content="1 url={.^tt.}">
</head><body /></html>

Replace the link section the anchor code with this:
Code: [Select]
<a href="/~extlink?sname=%item-name%&site={.replace|?|(que)|{.replace|&|(and)|%item-url%.}.}">%new%<img align=top src="/~img_link">%item-name%</a-->
All the replaces of ampersand (&) and question (?) are done to not loose part of links that include urlvars.

This works as follows:
With all the links you link to the hfs section 'extlink', where the url is recovered, a log entry written, then the minimal html page with the metatag is build. The metatag refreshs tha page with the url within one second.

This works even better than the load thing i  wanted to do, as there are no restrictions by UA.
« Last Edit: November 17, 2009, 01:51:41 PM by bacter »
your computer has no brain - use your own !

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Re: Need macro help
« Reply #5 on: November 17, 2009, 02:07:18 PM »
Bacter you're the man!  I knew you could do it.  Much better.  ;D

- I added <a  target="_blank" href="/~extlink? ....
- I removed {.add to log|%date% %time% %ip% %user% ... as that info was already in the log for that line
- I removed <a href="/~extlink?sname .... </a--> as I think it is a typo and not necessary
 
I tried 4 different links.  3 worked and 1 didn't.  The page kept refreshing itself but never loaded.  Don't know why?  Maybe you can notice something.  Here's the info.

- Link name - Wiki - Rejetto's Full Help Guide
- Link - http://www.rejetto.com/wiki/index.php/Main_Page
- After the template with the new additions to it is loaded, the above link changes to the following, and the page keeps refreshing itself but doesn't load ...

http://127.0.0.1/~extlink?sname=Wiki%20-%20Rejetto's%20Full%20Help%20Guide&site=http://www.rejetto.com/wiki/index.php/Main_Page

Edit - I found the problem with that link.  It doesn't like the apostrophe in Rejetto's.  Removed it an all is fine.   ;D ;D

« Last Edit: November 18, 2009, 01:30:35 PM by maverick »
maverick

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Re: Need macro help
« Reply #6 on: November 17, 2009, 06:28:05 PM »
The above works fine if the links are located within the filelist.

Any ideas how to get the same results when an external link is located outside the filelist but still within the webpage in other locations?
maverick

Offline bacter

  • Operator
  • Tireless poster
  • *****
  • Posts: 682
    • View Profile
Re: Need macro help
« Reply #7 on: November 17, 2009, 07:24:32 PM »
if you mean with 'an external link ... within the webpage in other location' something like for example a link to your blog built in the webdesign then just do the same thing as in the list section, but simplified.

Supose you have a link tou your blog 'mavericksays' at blogspot com.
Then you use now the following link somwhere in your template.

<a href="http://mavericksays.blogspot.com">Mi blog</a>
      
To use your 'logging system, just replace it by:

<a target="_blank" href="/~extlink?sname=Mi blog&site=http://mavericksays.blogspot.com">Mi blog</a>

or a link to hfs homepage would be:
<a target="_blank" href="/~extlink?sname=HTTP File server&site=http://www.rejetto.com/hfs/">HTTP File Server</a>


 :)
your computer has no brain - use your own !

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12953
    • View Profile
Re: Need macro help
« Reply #8 on: November 18, 2009, 02:50:40 PM »
bacter the great :)
i want to contribute to this matter by suggesting another version that needs no template editing.
you just have to put it in the diff template.
if you want it for the whole server, right click on the root/home > properties > diff template
and past this

Code: [Select]
[+]
<script>
// we run only when the DOM is ready
var exOnLoad = window.onload;
window.onload = function() {
// explore all links
var es = document.getElementsByTagName('a');
var n = es.length;
for (var i=0; i<n; i++) {
// we are interested only in external links
var a = es[i];
if (a.getAttribute('href').indexOf('://') < 0) continue;
// we listen for clicks
var exClick = a.onclick;
a.onclick = function() {
// we make a call to the server, to log the link's name, by pretending we load an image
var img = document.createElement('img');
                                                var txt = this.text || this.innerText;
img.src = "/~loghelper?link="+txt+"&time="+(new Date().getTime());
img.style.display = 'none';
var body = document.getElementsByTagName('body')[0];
body.appendChild(img);
body.removeChild(img);
if (exClick) return exClick();
}
}
if (exOnLoad) return exOnLoad();
}
</script>

[loghelper|no log]
{.add to log|CLICKED LINK: {.?link.}.}

Tested with FF3 and IE8.

This also works for the whole page, not only the file list.

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Re: Need macro help
« Reply #9 on: November 18, 2009, 04:00:47 PM »
Thanks rejetto, but it doesn't work for me.

My login page doesn't open so I can't go further.  The login page is loaded from the [login-link] section of the template.  HFS log shows the Requested GET / but that's it. 

Yes, I have js enabled.  Is there possibly some HFS setting that I need turned on that I might not be using right now for this to work?

maverick

Offline r][m

  • Tireless poster
  • ****
  • Posts: 347
    • View Profile
Re: Need macro help
« Reply #10 on: November 18, 2009, 06:32:23 PM »
Thank you Bacter.
The refresh meta method works here, even though my links page
is not in my tpl. It's in the public website section outside the secure file server.
I did notice that when I access a second link in quick succession sometimes
the browser freezes. Could just be the browser or system though.
 
Thank you Rejetto, as well, but while the diff  tpl method didn't cause any problem
it didn't work here either?

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12953
    • View Profile
Re: Need macro help
« Reply #11 on: November 18, 2009, 07:57:53 PM »
stupid of me!
i met the same problem, and found it's because of a bug in HFS.
i fixed it, for next release, and my testing was finally successful, but forgot about this little detail. ;)
i will publish it soon

Offline r][m

  • Tireless poster
  • ****
  • Posts: 347
    • View Profile
Re: Need macro help
« Reply #12 on: November 21, 2009, 06:22:06 AM »
i met the same problem, and found it's because of a bug in HFS.

I tried the diff tpl method with 249. Must be doing something wrong, it didn't work.

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12953
    • View Profile
Re: Need macro help
« Reply #13 on: November 30, 2009, 02:58:50 PM »
i tried with a clean hfs and all went fine.
please ensure you refresh the page, you may be using a cached copy.
anyone else?

Offline maverick

  • Tireless poster
  • ****
  • Posts: 1052
  • Computer Solutions
    • View Profile
Re: Need macro help
« Reply #14 on: November 30, 2009, 05:33:57 PM »
anyone else?

Just tried it.  Doesn't work for me either.  No log entries of any kind were produced when a link is used. 

To be honest, I prefer bacter's method of using macros with the refresh meta. 
maverick