Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion chrome/app/html/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</div>
<script type="text/javascript" src="../js/lib/beautify/1.7.4/beautify.min.js"></script>
<script type="text/javascript" src="../js/lib/beautify/1.7.4/beautify-html.min.js"></script>
<script type="text/javascript" src="../js/lib/browser-polyfill/0.2.1/browser-polyfill.min.js"></script>
<script type="text/javascript" src="../js/build/app.js"></script>

</body>
Expand Down
87 changes: 82 additions & 5 deletions chrome/app/js/background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
let appTabIds = {};
let orgVars = {};

Expand Down Expand Up @@ -40,20 +41,97 @@ browser.tabs.onRemoved.addListener((tabId, changeInfo, tab) => {
.forEach(key => appTabIds[key] = undefined)
}

})*/

let appTabNames = ['options_tab','app_tab']
let appTabIds = {}

function focusTab(tabId) {
return chrome.tabs.get(tabId).then((tab) => {
if (tab) chrome.tabs.update(tab.id, {
active: true
})
chrome.windows.getCurrent({}).then((currentWindow) => {
if (tab.windowId != currentWindow.id) {
chrome.windows.update(tab.windowId, {
focused: true
});
}
})
})}

chrome.tabs.onRemoved.addListener((closedTabId, changeInfo, tab) => {
appTabNames.forEach(async (name) => {
const tabId = await chrome.storage.sync.get(name)
console.log('closedTabId', closedTabId)
console.log('tabId', tabId)
if(closedTabId == tabId){
chrome.storage.sync.set({
[name]: undefined
})
}
})
})

browser.runtime.onMessage.addListener((request) => {
async function openOrFocusTab(url, name) {
const ids = await chrome.storage.sync.get(name)
console.log('ids', ids)
console.log('name', name)
if (ids[name]) {
focusTab(ids[name]).catch((err) => openTab(url, name))
} else {
openTab(url, name)
}
}

function openTab(url, name) {
chrome.tabs.create({
'url': url,
'selected': true
}, function(tab) {
console.log('tab', tab)
chrome.storage.sync.set({
[name]: tab.id
})
});
}

chrome.action.onClicked.addListener(
() => openOrFocusTab(chrome.runtime.getURL('html/options.html'), "options_tab"))

chrome.runtime.onMessage.addListener((request,sender,sendResponse) => {
console.log('request', request)
console.log('sender', sender)
switch (request.command) {
case "getToken":
chrome.storage.local.get('token').then(function({
token
}) {
sendResponse({
token
})
})
break
case "getShortcuts":
chrome.storage.sync.get('shortcuts').then(function({
shortcuts
}) {
sendResponse({
shortcuts
})
})
break
case "openTab":
browser.tabs.create({
chrome.tabs.create({
url: request.url
})
break
case "openOrFocusTab":
openOrFocusTab(request.url, request.name)
break
case "focusAppTab":
const appTabNames = Object.keys(appTabIds).filter(tabName => tabName.startsWith("app_") )
// const appTabNames = Object.keys(appTabIds).filter(tabName => tabName.startsWith("app_") )
const appTabNames = []
if(appTabNames.length > 0 && appTabIds[appTabNames[0]]){
focusTab(appTabIds[appTabNames[0]])
return true
Expand All @@ -74,7 +152,7 @@ browser.runtime.onMessage.addListener((request) => {
console.log(orgVars)
const tabId = appTabIds[ `app_${vars.oid}` ]
if(tabId){
browser.tabs.sendMessage(tabId, request)
chrome.tabs.sendMessage(tabId, request)
}
}
break;
Expand All @@ -84,4 +162,3 @@ browser.runtime.onMessage.addListener((request) => {
}
return true
});

1 change: 0 additions & 1 deletion chrome/app/js/build/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
1 change: 1 addition & 0 deletions chrome/app/js/build/app.js

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions chrome/app/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ function onload() {
$i('new_btn').addEventListener('click', (event) => {
shortcutsTable.appendChild(newShortcutLine())
})
browser.storage.local.get('token').then(function({
chrome.storage.local.get('token').then(function({
token
}) {
if (token) {
const tokenElement = document.querySelector('.token_val')
tokenElement.value = token
}
});
browser.storage.sync.get('shortcuts').then(function({
chrome.storage.sync.get('shortcuts').then(function({
shortcuts
}) {
const shortcutLines = buildShortcuts(shortcuts)
Expand Down Expand Up @@ -117,12 +117,10 @@ function save(event) {
const shortcuts = [...document.getElementById('shortcutsTable').children]
const tokenElement = document.querySelector('.token_val')
const token = tokenElement ? tokenElement.value : null;
if(token){
browser.storage.local.set({
chrome.storage.local.set({
'token': token
})
}
browser.storage.sync.set({
chrome.storage.sync.set({
'shortcuts': shortcuts.map(toSetting).filter(x => x)
})
}
Expand Down
Loading