1

I can create an object URL like this:

let pixel = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let data = atob(pixel);
let buffer = new ArrayBuffer(data.length);
let array = new Uint8Array(buffer);
for (var i = 0;i<data.length;i++){
    array[i]=data.charCodeAt(i);
}
let blob = new Blob([buffer], {type: 'image/jpeg'});
let url = URL.createObjectURL(blob);

And I can revoke an object URL like this:

URL.revokeObjectURL(url);

But what I would like to do is automatically revoke the URL after one use.

Is there a way to check if an object URL has been accessed?

Thank you in advance.

EDIT:

In this case, I can't just trigger a function when I use the URL. Instead, I would like to be able to check if it has been used even OUTSIDE of the page. An example would be if someone typed the URL into the address bar, this can't call javascript AFAIK.

7
  • If you don't have an API to do this, you could use localstorage to track this I guess Commented May 12, 2021 at 16:07
  • Could you explain how I would be able to use localStorage? Commented May 12, 2021 at 16:10
  • 1
    after one use what exactly do you call a "use"? Commented May 12, 2021 at 16:16
  • @Guerric P, I mean once the URL has been used to fetch the resource. For example, if it was included in an <img/> tag, I would like to know when the image has been fetched by way of the URL. Commented May 12, 2021 at 16:19
  • 1
    As I've mentioned in the other comment thread under my answer, this is not possible purely on the client-side. You'll need a server for this. Even with that, typing a URL into an address bar may not trigger a request for that url. /cc @GuerricP Commented May 12, 2021 at 16:26

1 Answer 1

0

If you don't have an API to do this, you could use localStorage to track this.

You could have 2 methods,

  • addUrlStatus - which takes a url as input and checks whether it has been accessed in the past.

  • updateUrlStatus - which sets the status of a url to true in localStorage once the user has used the url (not sure what action defines this in your code).

So you could do something like,


let pixel = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
let data = atob(pixel);
let buffer = new ArrayBuffer(data.length);
let array = new Uint8Array(buffer);
for (var i = 0;i<data.length;i++){
    array[i]=data.charCodeAt(i);
}
let blob = new Blob([buffer], {type: 'image/jpeg'});
let url = URL.createObjectURL(blob);
addUrlStatus(url);


function addUrlStatus(url) {
  var _url = localStorage.getItem(url);
  if(!_url) {
    localStorage.setItem(url, false); // false indicates it hasn't been used yet.
    return;
  }
}


// this function needs to be called when the user has used the url.
function updateUrlStatus(url) {
  localStorage.setItem(url, true);
  URL.revokeObjectURL(url);
}

Sign up to request clarification or add additional context in comments.

3 Comments

thank you for the quick response! This isn't what I need it for though. I would like to be able to check if the URL has been used WITHOUT calling a function. For example, if a random user typed the URL into the address bar, I would like to be able to tell if they reached the resource.
That's not possible purely on the client-side.
Okay, I guessed this was the case, but all the same thank you for your detailed answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.