Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 2.82 KB

usage.md

File metadata and controls

70 lines (52 loc) · 2.82 KB

Usage

Getting an SVG

To get an image for the current URL (for example is image is being requested by www.example.com), simply get the image as you normally would:

<img src="https://hitcounter.pythonanywhere.com/count/tag.svg" alt="Hits">

In this example, a hit would be added to the websites count on the server. To stop this from occurring but still get the SVG file, use:

<img src="https://hitcounter.pythonanywhere.com/nocount/tag.svg" alt="Hits">

Getting the Count Raw

If you don't want the SVG file but still want the count to use in something else, you can do a GET request to /count or as before, /nocount to not add a count. For Example:

let xmlHttp = new XMLHttpRequest();
xmlHttp.withCredentials = true;
xmlHttp.open('GET', 'https://hitcounter.pythonanywhere.com/count', false);
xmlHttp.send(null);
count = xmlHttp.responseText;

Using Ajax

let targetUrl = window.location.href;
$.ajax('https://hitcounter.pythonanywhere.com/count',{
    data: { url: targetUrl },
    xhrFields: { withCredentials: true }
}).then(count => console.log('Count:' + count));

Do not use data: {url: encodeURIComponent(targetUrl)} as Ajax will encode the string (url) for you. Doing this will encode the url twice which will then only be decoded on the server once (this can lead to broken tags in the future).

Getting a Count For a Site That Isn't Me

There may be circumstances that the referrer may not be sent or you may want to request an SVG or count for another site. To do this, set url to the URL you want to get (make sure to encoded the value).

For example, getting an SVG:

<img src="https://hitcounter.pythonanywhere.com/nocount/tag.svg?url=www.example.com" alt="Hits">

And if you want to get the count:

let targetUrl = 'www.example.com';
let query = '?url=' + encodeURIComponent(targetUrl);
let xmlHttp = new XMLHttpRequest();
xmlHttp.withCredentials = true;
xmlHttp.open('GET', 'https://hitcounter.pythonanywhere.com/nocount' + query, false);
xmlHttp.send(null);
count = xmlHttp.responseText;

There are also some situations where a client will not send the Referer in the header. This is a simple solution to the server not being able to find where the request came from.

Generating Links With A Tool Hosted By The Server

Going to the location / on the server, you will be served with an HTML page that contains a tool to create the image tag or markdown element and search up a websites count.

Interface