Advertisement
Guest User

Sample AutoReload Extension (background.js)

a guest
Mar 18th, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Related to http://stackoverflow.com/questions/15479683/all-chrome-tabs-present-the-same-error
  2.  
  3. var tabsToReload = [],interval; //TabsToReload holds the ids of the tabs
  4. chrome.contextMenus.create({type:"checkbox",id:"reloadEntry",title:"Reload every 2 Minutes"}) //Add an Checkbox ContextMenu Entry with type "checkbox" to toggle reloading
  5.  
  6. chrome.contextMenus.onClicked.addListener(function(info,tab) { //Add An Click EventListener
  7.     if(info.menuItemId == "reloadEntry" && info.wasChecked != info.checked) { //If the Menu Item which was clicked is the reloadEntry either remove or add the tab
  8.         info.checked ? addTab(tab.id) : removeTab(tab.id);
  9.     }
  10. });
  11.  
  12. chrome.tabs.onActivated.addListener(function(info) { //Add a Listener when another Tab gets the focus
  13.     var checked = !~tabsToReload.indexOf(info.tabId)
  14.     chrome.contextMenus.update("reloadEntry", {title:"Reload Tab Every 2 Minutes",type:"checkbox",checked:false}) //Update the menu entry to represent the AutoReload state
  15. });
  16.  
  17.  
  18. chrome.tabs.onRemoved.addListener(function(tabId) { //Listen if a tab gets closed and call the removeTab functionb
  19.     removeTab(tabId);
  20. });
  21.  
  22.  
  23. function addTab (tabId) { //adds a Tab to the ReloadList
  24.     var index = tabsToReload.indexOf(tabId); //gets the indexOf of the tabId in the toReload Array
  25.     if (!~index) { //uses a Bitwise NOT and a Logical NOT to get a boolean value, representing if the Tab is in the list
  26.         console.log("Added Tab " + tabId);
  27.         tabsToReload.push(tabId)
  28.         if (tabsToReload.length === 1) { //If there weren't any tabs before, start the Interval
  29.             reloadTabs();
  30.             interval = window.setInterval(reloadTabs,120000);
  31.         }
  32.        
  33.     }
  34. }
  35.  
  36. function removeTab (tabId) { //Removes a tab from the List
  37.     var index = tabsToReload.indexOf(tabId);
  38.     if (!!~index) { //see above
  39.         tabsToReload.splice(index,1); //splioes the tabs id of the array
  40.         if (tabsToReload.length === 0) { //If no more tabs are left
  41.             window.clearInterval(interval); //clear the interval
  42.         }
  43.         console.log("Removed Tab " + tabId);
  44.     }
  45. }
  46.  
  47. function reloadTabs() {
  48.     console.log("Reloading tabs " + tabsToReload);
  49.     for (var i = 0,j;j = tabsToReload[i];i++) { //iterate over the list of tabs
  50.         (function (j) { //use a closure to capture j
  51.             chrome.tabs.get(j, function (tab) { //get the tab
  52.                 tab ? chrome.tabs.reload(j) : removeTab(j); //if the tab exists reload it , otherwise remove it from the list (shouldn't happen)
  53.             })
  54.         })(j);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement