Skip to content
Merged
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
3 changes: 3 additions & 0 deletions src/page/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ function settingsStore() {
}
});
}
if (key === "active") {
browser.runtime.sendNativeMessage({name: "TOGGLE_EXTENSION", active: String(value)});
}
};
const updateSingleSetting = (key, value) => {
// update(settings => (settings[key] = value, settings));
Expand Down
2 changes: 2 additions & 0 deletions src/popup/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
async function toggleExtension() {
await settingsStorage.set({global_active: !active});
active = await settingsStorage.get("global_active");
// TODO: delete after migrating all related logic on the native
browser.runtime.sendNativeMessage({name: "TOGGLE_EXTENSION", active: String(active)});
}

function updateAll() {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ const _browser = {
response = result;
// response.error = "Something went wrong!";
// response.info = "No updates found";
} else if (name === "POPUP_TOGGLE_EXTENSION") {
} else if (name === "TOGGLE_EXTENSION") {
// response = {error: "Failed toggle extension"};
response = {success: true};
} else if (name === "POPUP_UPDATE_ALL" || name === "POPUP_UPDATE_SINGLE") {
Expand Down
10 changes: 1 addition & 9 deletions xcode/Safari-Extension/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1622,24 +1622,16 @@ func getPopupBadgeCount(_ url: String, _ subframeUrls: [String]) -> Int? {
}
let manifest = getManifest()
guard
var matches = getPopupMatches(url, subframeUrls),
let active = manifest.settings["active"],
let showCount = manifest.settings["showCount"]
var matches = getPopupMatches(url, subframeUrls)
else {
err("getPopupBadgeCount failed at (1)")
return nil
}
if showCount == "false" {
return 0
}
for pattern in manifest.blacklist {
if match(url, pattern) {
return 0
}
}
if active != "true" {
return 0
}
matches = matches.filter{!manifest.disabled.contains($0["filename"] as! String)}
return matches.count
}
Expand Down
30 changes: 17 additions & 13 deletions xcode/Safari-Extension/Resources/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,22 @@ function setClipboard(data, type = "text/plain") {
}

async function setBadgeCount() {
// only set badge on macOS
const platform = await getPlatform();
if (platform !== "macos") return;
const clearBadge = () => browser.browserAction.setBadgeText({text: ""});
// TODO: after the background script is modularized, import and use:
// settingsStorage.get(["global_active","toolbar_badge_count","global_exclude_match"])
const results = await browser.storage.local.get(["US_GLOBAL_ACTIVE", "US_TOOLBAR_BADGE_COUNT"]);
if (results?.US_GLOBAL_ACTIVE === false) return clearBadge();
if (results?.US_TOOLBAR_BADGE_COUNT === false) return clearBadge();

const currentTab = await browser.tabs.getCurrent();
// no active tabs exist (user closed all windows)
if (!currentTab) return;
if (!currentTab) return clearBadge();
const url = currentTab.url;
// if url doesn't exist, stop
if (!url) {
browser.browserAction.setBadgeText({text: ""});
return;
}
if (!url) return clearBadge();
// only check for http/s pages
if (!url.startsWith("http://") && !url.startsWith("https://")) {
browser.browserAction.setBadgeText({text: ""});
return;
}
if (!url.startsWith("http://") && !url.startsWith("https://")) return clearBadge();
// TODO: if url match in global exclude list, clear badge
const frameUrls = new Set();
const frames = await browser.webNavigation.getAllFrames({tabId: currentTab.id});
for (let i = 0; i < frames.length; i++) {
Expand All @@ -117,7 +116,12 @@ async function setBadgeCount() {
if (count > 0) {
browser.browserAction.setBadgeText({text: count.toString()});
} else {
browser.browserAction.setBadgeText({text: ""});
const _url = new URL(url);
if (_url.pathname.endsWith(".user.js")) {
browser.browserAction.setBadgeText({text: "JS"});
} else {
clearBadge();
}
}
});
}
Expand Down
30 changes: 12 additions & 18 deletions xcode/Safari-Extension/SafariWebExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,15 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
}
}
else if name == "POPUP_BADGE_COUNT" {
#if os(macOS)
if let url = message?["url"] as? String, let frameUrls = message?["frameUrls"] as? [String] {
if let matches = getPopupBadgeCount(url, frameUrls) {
response.userInfo = [SFExtensionMessageKey: ["count": matches]]
} else {
response.userInfo = [SFExtensionMessageKey: ["error": "failed to update badge count"]]
}
if let url = message?["url"] as? String, let frameUrls = message?["frameUrls"] as? [String] {
if let matches = getPopupBadgeCount(url, frameUrls) {
response.userInfo = [SFExtensionMessageKey: ["count": matches]]
} else {
inBoundError = true
response.userInfo = [SFExtensionMessageKey: ["error": "failed to update badge count"]]
}
#endif
} else {
inBoundError = true
}
}
else if name == "POPUP_INIT" {
if let initData = popupInit() {
Expand Down Expand Up @@ -124,21 +122,17 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
response.userInfo = [SFExtensionMessageKey: ["error": "failed to check for updates"]]
}
}
else if name == "POPUP_TOGGLE_EXTENSION" {
var manifest = getManifest()
if let active = manifest.settings["active"] {
if active == "true" {
manifest.settings["active"] = "false"
} else {
manifest.settings["active"] = "true"
}
else if name == "TOGGLE_EXTENSION" {
if let active = message?["active"] as? String, ["true", "false"].contains(active) {
var manifest = getManifest()
manifest.settings["active"] = active
if updateManifest(with: manifest) {
response.userInfo = [SFExtensionMessageKey: ["success": true]]
} else {
response.userInfo = [SFExtensionMessageKey: ["error": "failed to update injection state"]]
}
} else {
response.userInfo = [SFExtensionMessageKey: ["error": "failed to update injection state"]]
response.userInfo = [SFExtensionMessageKey: ["error": "missing or wrong message content"]]
}
}
else if name == "TOGGLE_ITEM" {
Expand Down