├── screenshot.png ├── icons ├── icon-32.png ├── icon-48.png └── icon-64.png ├── web-ext-artifacts ├── domainswitcher-1.0.xpi └── domainswitcher-1.1.xpi ├── README.md ├── manifest.json ├── choose_domain.html ├── domainswitcher.js └── choose_domain.js /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/screenshot.png -------------------------------------------------------------------------------- /icons/icon-32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/icons/icon-32.png -------------------------------------------------------------------------------- /icons/icon-48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/icons/icon-48.png -------------------------------------------------------------------------------- /icons/icon-64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/icons/icon-64.png -------------------------------------------------------------------------------- /web-ext-artifacts/domainswitcher-1.0.xpi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/web-ext-artifacts/domainswitcher-1.0.xpi -------------------------------------------------------------------------------- /web-ext-artifacts/domainswitcher-1.1.xpi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/peterbe/domainswitcher/master/web-ext-artifacts/domainswitcher-1.1.xpi -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Domainswitcher 2 | 3 | **Caveat: Only works for `*.mozilla.org`, `*.allizom.org` and `*.mozilla.com` domains at the moment.** See [issue #1](https://github.com/peterbe/domainswitcher/issues/1). 4 | 5 | A WebExtension to switch the current domain (and protocol) and redirect there. 6 | 7 | This is useful if you've loaded, for example, a URL on your production site 8 | and what to load the exact same URL but change the start of the URL 9 | to `http://localhost:5000` instead for example. 10 | 11 | Every time you do a domainswitch, the destination is remembered so next time 12 | you visit that first URL, it remembers what you preference might be. 13 | 14 |  15 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | "manifest_version": 2, 4 | "name": "Domainswitcher", 5 | "version": "1.1", 6 | "homepage_url": "https://github.com/peterbe/domainswitcher", 7 | 8 | "description": "Change domain, only.", 9 | "icons": { 10 | "48": "icons/icon-48.png", 11 | "64": "icons/icon-64.png" 12 | }, 13 | 14 | "applications": { 15 | "gecko": { 16 | "id": "domainswitcher@peterbe.com", 17 | "strict_min_version": "45.0" 18 | } 19 | }, 20 | 21 | "permissions": [ 22 | "tabs" 23 | ], 24 | 25 | "page_action": { 26 | "default_icon": "icons/icon-32.png", 27 | "default_title": "Domainswitcher", 28 | "default_popup": "choose_domain.html" 29 | }, 30 | 31 | "background": { 32 | "scripts": ["domainswitcher.js"] 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /choose_domain.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 | 13 | 14 | 15 | 16 | 26 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /domainswitcher.js: -------------------------------------------------------------------------------- 1 | var urlparse = function(url) { 2 | var parser = document.createElement('a') 3 | parser.href = url 4 | return parser 5 | } 6 | 7 | var matchedDomain = function(url) { 8 | var domain = urlparse(url).host 9 | // XXX make this config. Not everyone works on mozilla websites. 10 | if (/(allizom|mozilla)\.(org|com)$/.test(domain)) { 11 | return true 12 | } 13 | return false 14 | } 15 | 16 | chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { 17 | if (!changeInfo.url) { 18 | return 19 | } 20 | if (!matchedDomain(changeInfo.url)) { 21 | return 22 | } 23 | chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 24 | chrome.pageAction.show(tabId) 25 | }) 26 | }) 27 | 28 | // chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 29 | // console.log('Current tab to start', tabs[0].url) 30 | // }) 31 | 32 | // Happens when you click on a tab that was already loaded but not focused. 33 | // chrome.tabs.onActivated.addListener(function (activeInfo) { 34 | // // console.log("Activated tab", activeInfo) 35 | // // reset(activeInfo.tabId) 36 | // }) 37 | 38 | 39 | chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 40 | reset(tabs[0].id) 41 | }) 42 | 43 | function reset(tabId) { 44 | chrome.pageAction.hide(tabId) 45 | } 46 | -------------------------------------------------------------------------------- /choose_domain.js: -------------------------------------------------------------------------------- 1 | var urlparse = function(url) { 2 | var parser = document.createElement('a') 3 | parser.href = url 4 | return parser 5 | } 6 | 7 | var submit = function(destination) { 8 | if (!destination.trim().length) { 9 | return 10 | } 11 | animateDots() 12 | chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 13 | chrome.pageAction.hide(tabs[0].id) 14 | 15 | var origin = urlparse(tabs[0].url) 16 | var goto = urlparse(destination) 17 | var newDestination = goto.protocol + 18 | '//' + 19 | goto.host + 20 | origin.pathname + 21 | origin.search + 22 | origin.hash 23 | 24 | chrome.tabs.update({url: newDestination}) 25 | }) 26 | } 27 | 28 | 29 | var animateDots = function() { 30 | var el = document.querySelector('#loading') 31 | var dots = 0 32 | setInterval(function() { 33 | el.textContent = Array(dots++ % 5 + 1).join('.') 34 | }, 400) 35 | } 36 | 37 | 38 | document.addEventListener("submit", function(e) { 39 | e.preventDefault() 40 | var destination = document.querySelector('#domain').value 41 | 42 | chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 43 | var key = makeKey(tabs[0].url) 44 | var memory = load() 45 | 46 | // if it's already something and is an array with length, ... 47 | if (memory[key] && memory[key].length) { 48 | // ...then add it to the top. 49 | var index = memory[key].indexOf(destination) 50 | if (index > -1) { 51 | // ...but remove it if it was already there. 52 | memory[key].splice(index, 1) 53 | } 54 | memory[key].unshift(destination) 55 | } else { 56 | memory[key] = [destination] 57 | } 58 | save(memory) 59 | submit(destination) 60 | }) 61 | 62 | }) 63 | 64 | var load = function() { 65 | return JSON.parse(localStorage.getItem('domainswitcher') || '{}') 66 | } 67 | 68 | var save = function(stuff) { 69 | localStorage.setItem('domainswitcher', JSON.stringify(stuff)) 70 | } 71 | 72 | var makeKey = function(url) { 73 | var parser = document.createElement('a') 74 | parser.href = url 75 | return parser.protocol + parser.hostname 76 | } 77 | 78 | chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { 79 | // console.log('Current tab:', tabs[0].url) 80 | var memory = load() 81 | var key = makeKey(tabs[0].url) 82 | if (memory[key]) { 83 | document.querySelector('#domain').value = memory[key][0] 84 | 85 | var alternatives = document.querySelector('#alternatives') 86 | if (memory[key].length > 1) { 87 | memory[key].forEach(function(alternative, notfirst) { 88 | if (notfirst) { 89 | var button = document.createElement('button') 90 | button.classList.add('alternative') 91 | button.textContent = alternative 92 | button.addEventListener('click', function(event) { 93 | event.preventDefault() 94 | submit(alternative) 95 | }) 96 | alternatives.appendChild(button) 97 | } 98 | }) 99 | alternatives.style.display = 'block' 100 | } else { 101 | alternatives.style.display = 'none' 102 | } 103 | } 104 | 105 | }) 106 | --------------------------------------------------------------------------------