Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Related to http://stackoverflow.com/questions/15479683/all-chrome-tabs-present-the-same-error
- var tabsToReload = [],interval; //TabsToReload holds the ids of the tabs
- chrome.contextMenus.create({type:"checkbox",id:"reloadEntry",title:"Reload every 2 Minutes"}) //Add an Checkbox ContextMenu Entry with type "checkbox" to toggle reloading
- chrome.contextMenus.onClicked.addListener(function(info,tab) { //Add An Click EventListener
- if(info.menuItemId == "reloadEntry" && info.wasChecked != info.checked) { //If the Menu Item which was clicked is the reloadEntry either remove or add the tab
- info.checked ? addTab(tab.id) : removeTab(tab.id);
- }
- });
- chrome.tabs.onActivated.addListener(function(info) { //Add a Listener when another Tab gets the focus
- var checked = !~tabsToReload.indexOf(info.tabId)
- chrome.contextMenus.update("reloadEntry", {title:"Reload Tab Every 2 Minutes",type:"checkbox",checked:false}) //Update the menu entry to represent the AutoReload state
- });
- chrome.tabs.onRemoved.addListener(function(tabId) { //Listen if a tab gets closed and call the removeTab functionb
- removeTab(tabId);
- });
- function addTab (tabId) { //adds a Tab to the ReloadList
- var index = tabsToReload.indexOf(tabId); //gets the indexOf of the tabId in the toReload Array
- if (!~index) { //uses a Bitwise NOT and a Logical NOT to get a boolean value, representing if the Tab is in the list
- console.log("Added Tab " + tabId);
- tabsToReload.push(tabId)
- if (tabsToReload.length === 1) { //If there weren't any tabs before, start the Interval
- reloadTabs();
- interval = window.setInterval(reloadTabs,120000);
- }
- }
- }
- function removeTab (tabId) { //Removes a tab from the List
- var index = tabsToReload.indexOf(tabId);
- if (!!~index) { //see above
- tabsToReload.splice(index,1); //splioes the tabs id of the array
- if (tabsToReload.length === 0) { //If no more tabs are left
- window.clearInterval(interval); //clear the interval
- }
- console.log("Removed Tab " + tabId);
- }
- }
- function reloadTabs() {
- console.log("Reloading tabs " + tabsToReload);
- for (var i = 0,j;j = tabsToReload[i];i++) { //iterate over the list of tabs
- (function (j) { //use a closure to capture j
- chrome.tabs.get(j, function (tab) { //get the tab
- tab ? chrome.tabs.reload(j) : removeTab(j); //if the tab exists reload it , otherwise remove it from the list (shouldn't happen)
- })
- })(j);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement