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

index.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. // Wait for the DOM to load before doing anything
  27. jQuery(document).ready(function() {
  28. // Check if there's a mirror for the current URL. If so, display the message
  29. // about it and the button to redirect to a random mirror (if there's more than
  30. // one).
  31. getCurrentTabUrl(function(url) {
  32. chrome.storage.local.get("sites", function(sites){
  33. sites = sites.sites
  34. // Grab the domain part of the URL
  35. domain = url.match(/:\/\/(www\.)?([^\/]+)\//).slice(-1)[0]
  36. if(domain in sites) {
  37. proxies = sites[domain]
  38. // Offer the user to redirect them to a mirror
  39. $("#mirror").css("display", "block")
  40. $("#nomirror").css("display", "none")
  41. $("#mirror button").on("click", function() {
  42. takeMeTo(proxies[Math.floor(Math.random()*proxies.length)])
  43. })
  44. }
  45. else {
  46. // Tell the user there's no mirror available
  47. $("#mirror").css("display", "none")
  48. $("#nomirror").css("display", "block")
  49. }
  50. })
  51. })
  52. })