-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.dicthub.plugin | ||
|
||
import org.dicthub.model.PluginInfo | ||
import org.dicthub.model.UserPreference | ||
import org.w3c.dom.get | ||
import kotlin.browser.localStorage | ||
import kotlin.js.Date | ||
import kotlin.js.Promise | ||
|
||
|
||
data class PluginUpdateResult( | ||
val newPlugins: List<PluginInfo>, | ||
val upgradablePlugins: List<PluginInfo> | ||
) | ||
|
||
|
||
class PluginUpdateChecker(private val pluginIndex: PluginIndex, private val userPreference: UserPreference) { | ||
|
||
private val LAST_CHECK_TIME = "lastCheckTime" | ||
private val LAST_AVAILABLE_PLUGINS = "lastAvailablePlugins" | ||
|
||
fun getLastCheckTime(): Long { | ||
return localStorage[LAST_CHECK_TIME]?.toLong() ?: 0 | ||
} | ||
|
||
fun check(): Promise<PluginUpdateResult> { | ||
|
||
return Promise { resolve, reject -> | ||
pluginIndex.load().then { latestPluginInfo -> | ||
|
||
val lastPluginIds = localStorage.getItem(LAST_AVAILABLE_PLUGINS)?.split(",")?.toSet() | ||
|
||
val newPlugins = latestPluginInfo.filterNot { lastPluginIds?.contains(it.id) ?: true } | ||
|
||
val enabledPluginVersions = userPreference.enabledPlugins.map { it.id to it.version }.toMap() | ||
val upgradablePlugins = latestPluginInfo.filter { enabledPluginVersions.containsKey(it.id) && enabledPluginVersions.get(it.id) != it.version} | ||
|
||
val latestPluginIdStr = latestPluginInfo.map { it.id }.joinToString(",") | ||
localStorage.setItem(LAST_AVAILABLE_PLUGINS, latestPluginIdStr) | ||
localStorage.setItem(LAST_CHECK_TIME, Date().getTime().toString()) | ||
|
||
resolve(PluginUpdateResult(newPlugins = newPlugins, upgradablePlugins = upgradablePlugins)) | ||
}.catch(reject) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ | |
"storage", | ||
"activeTab", | ||
"tabs", | ||
"notifications", | ||
"contextMenus", | ||
"\u003Call_urls>" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ | |
"storage", | ||
"activeTab", | ||
"tabs", | ||
"notifications", | ||
"contextMenus", | ||
"\u003Call_urls>" | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,37 @@ | ||
|
||
var MENU_ID = "WordhubQuery"; | ||
var MENU_ID = "DicthubQuery"; | ||
|
||
chrome.runtime.onInstalled.addListener(function (details) { | ||
|
||
if (details.reason == 'install') { | ||
chrome.tabs.create({'url': chrome.extension.getURL('welcome.html')}, function(tab) {}); | ||
chrome.tabs.create({ 'url': chrome.extension.getURL('welcome.html') }, function (tab) { }); | ||
} else if (details.reason == 'update') { | ||
chrome.tabs.create({'url': 'https://dicthub.org/docs/getting-started/release/'}, function(tab) {}); | ||
chrome.tabs.create({ 'url': 'https://dicthub.org/docs/getting-started/release/' }, function (tab) { }); | ||
} | ||
|
||
chrome.contextMenus.create({ | ||
"id": MENU_ID, | ||
"title": "DictHub Query", | ||
"contexts": ["selection"] | ||
}); | ||
chrome.contextMenus.onClicked.addListener(function(info, tab) { | ||
chrome.contextMenus.onClicked.addListener(function (info, tab) { | ||
if (info.menuItemId === MENU_ID) { | ||
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) { | ||
chrome.tabs.sendMessage(tabs[0].id, {"action": "showContextMenuContainer"}); | ||
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { | ||
chrome.tabs.sendMessage(tabs[0].id, { "action": "showContextMenuContainer" }); | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
function handleNotification(message) { | ||
if (message.cmd == "plugin-notification") { | ||
chrome.notifications.create("dicthub-notification", message.payload, function (id) { | ||
chrome.notifications.onClicked.addListener(function(id) { | ||
chrome.tabs.create({ 'url': chrome.runtime.getURL('options.html') }, function (tab) { }) | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
chrome.runtime.onMessage.addListener(handleNotification); |