1

I have written a Chrome Extension to Clear TYPO3 CMS Caches right out of the browser's address bar.

Now I would like to add an optional feature, to clear the caches automatically on page refresh . That means, when the user presses F5, CTRL+R, clicks the reload icon in toolbar or clicks in context menu to "Reload", first my ajax script should be executed (to clear the CMS cache) and after it, the page may get reloaded.

Is there a possibility to do it with the Chrome API?

I tried first the window.onbeforeupdate event in content script - and this is triggered always when the page is reloading, but also if I close the window or switch to another website. And furthermore I can't tell the browser to wait for my ajax request here.

What works is to check in document.onkeydown event the keys 116 (F5) and 82 (R) with control key and use event.preventDefault(). But this will not cover the two other possible cases to refresh the cache.

Thanks!

3
  • Why don't you accept onbeforeupdate as a solution? IMHO, it does not matter that the cache will be cleared as well if the window is closed, or a user moved to another site. Also it seems you have no need to wait ajax response. Do you mean the browser don't offer you enough time even to send ajax request? Did you consider background long polling or websockets connetions as live flags for keeping cache? As soon as connection is dropped, cache can be cleared up. Commented Nov 20, 2012 at 12:44
  • Hm, I tried onbeforeupdate but for any reasons it has not been fired, when refreshing the page. No I would have enough time to start the ajax request, but it makes no sense to reload the page and clear the cache simulatiously. The reload must happen after clearing the cache. Commented Nov 20, 2012 at 13:46
  • Oh, I don't think it's possible to suspend page refresh until a server allows it to happen, that is after the cache was actually cleared (if I understand you correctly). At least, this could be doable in 2 steps, not in one (as implies refresh on its own). Commented Nov 20, 2012 at 14:02

1 Answer 1

1

I've found a solution. It does not really match my requirements, but it works (even better).

Chrome provides a webRequest API which allows you to modify or block all http requests.

chrome.webRequest.onBeforeRequest.addListener( function (details) { if (!severalConditionsToCheckIfIWantToDoTheMagic) { return; } $.ajax(url, { async:false, complete:function () { return {redirectUrl:details.url}; } }); }, {urls:["<all_urls>"]}, ["blocking"] ); 

In this example first I check if I want to do the ajax request. If yes, the next step is no do the ajax request. It is important, that the request is not asynchronous. On complete I return the original url of the request, which is basically just a reload.

The reason why it does not really match my requirements, is that it will be triggered always, on any request, not just on reload. That means in my conditions I have to check against:

  • Other websites
  • Other request types (loading css, js or images are also request), I just need the type 'main_frame'
  • Some variables belonging to my extension

Now, the script will be also triggered when I click on a link which is on the website - but for my case this is fine.

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

5 Comments

can theis prevent the user from refreshing the chrome extension itself?
If you mean with "refreshing the chrome extension", to prevent the user to uninstall/reinstall the extension: No this is impossible. You could prevent links to Google, once the extension is installed. But extension management and webRequest API are two different things.
No I meant refreshing the page when the page is loaded at chrome-extension://xyzid
Oh. Good requestion. I don't know. You need to try it. But I guess it is not possible, because it is not a real web request. Not even a http request, according to the protocol in URL. Also if they would allow this, you can also prevent to open settings or whatever.
Yeah i think youre right, i tried it and couldnt get it to work, but may try again later

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.