Coder Social home page Coder Social logo

Comments (11)

brentvollebregt avatar brentvollebregt commented on May 14, 2024 3

Looks like it's working - I also updated the REAMDE 👍

from hit-counter.

brentvollebregt avatar brentvollebregt commented on May 14, 2024

If you look at the request being made to the server to get the count, is there a cookie being sent?

from hit-counter.

Rdyx avatar Rdyx commented on May 14, 2024

Hmmm, I don't know (I don't think so because I guess if it did the count would not increase? ^^)

How can I check this? The website is fully static and hosted on github pages

from hit-counter.

brentvollebregt avatar brentvollebregt commented on May 14, 2024

If you go to DevTools (in Chrome) and look at the request headers, you can see all the cookies sent:
image

from hit-counter.

Rdyx avatar Rdyx commented on May 14, 2024

Oh, haha I'm feeling so dumb to forgot we can see it there :D

Ok so, if I link an image, there's a cookie but in fact, when I'm just using an ajax request, there's none

image

from hit-counter.

brentvollebregt avatar brentvollebregt commented on May 14, 2024

Take a look at this SO question: Why is jquery's .ajax() method not sending my session cookie?

It is stated that:

AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script.

I know this also happens in fetch, I have previously set credentials: 'same-origin' to get around this.

If you try something like:

function updateHitCounterText(url, elementId) {
  return $.ajax({
    url: 'https://hitcounter.pythonanywhere.com/count',
    data: { url: url },
    xhrFields: {
      withCredentials: true // <-- This guy should help
    },
  }).then(hitCount => {
    replaceTextInElementById(elementId, hitCount);
  });
};

does it work?

from hit-counter.

Rdyx avatar Rdyx commented on May 14, 2024

Encountering CORS origin conflicts, tried with fetch and credentials: 'include' (since same-origin is deprecated) but having the same problem. Problem is that response header is containing Access-Control-Allow-Origin with wildcard *, which is not allowed when credentials are used ^^'

Also gave a try with ajax and dataType: jsonp but returning error too since, obviously, returned data is not JSONp :)

Edit: this is the error message while using xhrField or credentials: 'include'
image

from hit-counter.

Rdyx avatar Rdyx commented on May 14, 2024

I found a workaround (kinda dirty, but working).

First, gratz for your very clear and straight forward doc about setting up project on python anywhere :)

Mounted my own setup so I'm not getting hitcounter busy especially with kinda useless requests.

Now, about the workaround, I make a request with image, which is hidden in DOM, then I make a request with nocount to get the count and show it wherever I want while not increasing the counter :)

I guess I could make it cleaner by editing code and adding the wanted url as Access-Control-Allow-Origin, but since this is purely for personnal purpose and I'm lazy, I'll deal with it 😂

Here is how the final code is looking:

/**
 * Creates a dynamic hidden hitcount image
 * @param string hitCountDomain
 * @param string url
 */
function createHitCounterImgElement(hitCountDomain, url) {
  let imgElement = document.createElement('img');
  imgElement.src = 'https://' + hitCountDomain + '.pythonanywhere.com/count/tag.svg?url=' + url;
  imgElement.classList.add('hidden');
  document.getElementById('body').appendChild(imgElement);
}

/**
 * Replace target element inner html
 * @param string elementId
 * @param string text
 */
function replaceTextInElementById(elementId, text) {
  document.getElementById(elementId).innerHTML = text;
}

/**
 * Make ajax request and update hits counter
 * @param string url
 * @param string elementId
 */
function updateHitCounterText(url, elementId, count = false) {
  const hitCountDomain = 'rdyx';

  count && createHitCounterImgElement(hitCountDomain, url);

  return $.ajax('https://' + hitCountDomain + '.pythonanywhere.com/nocount', { data: { url: url } }).then(
    (hitCount) => {
      replaceTextInElementById(elementId, hitCount);
    }
  );
}

Calling it with:

<script type="text/javascript">
  postUrl = window.origin + '{{ site.baseurl }}/{{ post.url | remove_first: '/' }}';
  updateHitCounterText(postUrl, '{{post.url}}-hits-count');  // Add true as last arg if you want to increase counter
</script>

from hit-counter.

brentvollebregt avatar brentvollebregt commented on May 14, 2024

I've been looking at this for a bit now and am a bit unsure on how to fix this.

In summary, we cannot set withCredentials: true (credentials mode 'include') on a fetch/ajax call if Access-Control-Allow-Origin or Access-Control-Allow-Headers are set to *. To allow any site to hit this website though, we need to set Access-Control-Allow-Origin: *.

I do remember using flask-cors' supports_credentials=True to do something similar recently - however, I imagine I set Access-Control-Allow-Origin to something that was not * which makes this useless.

from hit-counter.

Rdyx avatar Rdyx commented on May 14, 2024

Hehe yeah I get to the same statement, one workaround that may work (but I'm totally unsure if this is "clean" and I doubt it) would to get the origin from request (Host) and send it in the proper response header?

image

Else, I guess there is no real solution indeed, maybe the workaround I found earlier (using hidden image + nocount URL to get count and show it where I want) would be the best one. But doing so would double any counter request :)

from hit-counter.

brentvollebregt avatar brentvollebregt commented on May 14, 2024

Ah yes, the origin! Putting the origin in Access-Control-Allow-Origin should work and defaulting it to '*' if no origin is available. I'll look into this now, thank you.

from hit-counter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.