Skip to content

Commit

Permalink
fix change action icon bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Benbinbin committed May 9, 2023
1 parent a5ecaf7 commit e9e18c6
Showing 1 changed file with 23 additions and 58 deletions.
81 changes: 23 additions & 58 deletions src/background/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { useCheckBookmarkState } from '@/composables/checkBookmarkState'
import { useChangeActionIcon } from '@/composables/changeActionIcon'


/**
* show page after install extension
*/
// open the introduction page when the extension is installed
chrome.runtime.onInstalled.addListener(({reason}) => {
if (reason === 'install') {
Expand All @@ -10,25 +14,11 @@ chrome.runtime.onInstalled.addListener(({reason}) => {
}
});

// watching the active tab change
chrome.tabs.onActivated.addListener(async (activeInfo) => {
const activeTabID = activeInfo.tabId;
const tab = await chrome.tabs.get(activeTabID);

const url = tab.url || tab.pendingUrl;

let bookmarkState = false;
if (url) {
const result = await useCheckBookmarkState(url);
if(result) bookmarkState = true
}

// change action icon
await useChangeActionIcon(bookmarkState);
});

// watching the window focus change
chrome.windows.onFocusChanged.addListener(async (windowId) => {
/**
* change action icon
*/
const checkActiveWindowTabState = async () => {
// get the active tab of current focus window
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
Expand All @@ -46,56 +36,31 @@ chrome.windows.onFocusChanged.addListener(async (windowId) => {

// change action icon
await useChangeActionIcon(bookmarkState);
}

// watching the active tab change
chrome.tabs.onActivated.addListener(async (activeInfo) => {
await checkActiveWindowTabState();
});

// watching the window focus change
chrome.windows.onFocusChanged.addListener(async (windowId) => {
await checkActiveWindowTabState();
});

// watching tab update
// watching tab (url) update
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (!changeInfo.url) return;

// when tab update url check the bookmark state
const { url } = changeInfo;

let bookmarkState = false
const result = await useCheckBookmarkState(url);
if(result) bookmarkState = true

// change action icon
await useChangeActionIcon(bookmarkState);
await checkActiveWindowTabState();
});

// watching bookmark create
chrome.bookmarks.onCreated.addListener(async (id, bookmarkNode) => {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});

if (!tab) return;

const url = tab.url || tab.pendingUrl;

let bookmarkState = false
const result = await useCheckBookmarkState(url);
if(result) bookmarkState = true

// change action icon
await useChangeActionIcon(bookmarkState);
await checkActiveWindowTabState();
});

// watching bookmark delete
chrome.bookmarks.onRemoved.addListener(async (id, bookmarkNode) => {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});

const url = tab.url || tab.pendingUrl;

let bookmarkState = false
const result = await useCheckBookmarkState(url);
if (result) bookmarkState = true


// change action icon
await useChangeActionIcon(bookmarkState);
await checkActiveWindowTabState();
});

0 comments on commit e9e18c6

Please sign in to comment.