Find access to blocked websites https://rsf.org/collateral-freedom

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Get the current URL.
  3. *
  4. * @param {function(string)} callback - called when the URL of the current tab
  5. * is found.
  6. */
  7. function getCurrentTabUrl(callback) {
  8. var queryInfo = {
  9. active: true,
  10. currentWindow: true
  11. };
  12. chrome.tabs.query(queryInfo, function(tabs) {
  13. var tab = tabs[0];
  14. var url = tab.url;
  15. console.assert(typeof url == 'string', 'tab.url should be a string');
  16. callback(url);
  17. });
  18. }
  19. // Redirect to mirror url
  20. function takeMeTo(url) {
  21. updateProperties = {
  22. url: "https://" + url
  23. }
  24. chrome.tabs.update(updateProperties=updateProperties)
  25. }
  26. // Check if there's a mirror for the current URL. If so, display the message
  27. // about it and the button to redirect to a random mirror (if there's more than
  28. // one).
  29. getCurrentTabUrl(function(url) {
  30. chrome.storage.local.get("sites", function(sites){
  31. sites = sites.sites
  32. // Grab the domain part of the URL
  33. domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
  34. if(domain in sites) {
  35. proxies = sites[domain]
  36. // Offer the user to redirect them to a mirror
  37. $("#mirror").css("display", "block")
  38. $("#nomirror").css("display", "none")
  39. $("#mirror button").on("click", function() {
  40. takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
  41. })
  42. }
  43. else {
  44. // Tell the user there's no mirror available
  45. $("#mirror").css("display", "none")
  46. $("#nomirror").css("display", "block")
  47. }
  48. })
  49. })