-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add: tab monitor * Add: view source element * refactor
- Loading branch information
Showing
7 changed files
with
165 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// observee the latest active tab | ||
import browser from "webextension-polyfill"; | ||
import EventLite from "event-lite"; | ||
|
||
class TabMonitor extends EventLite { | ||
constructor() { | ||
super(); | ||
this._tabId = null; | ||
this._tab = null; | ||
this._onActivated = this._onActivated.bind(this); | ||
this._onUpdated = this._onUpdated.bind(this); | ||
// FIXME: make it work with multiple windows | ||
// NOTE: in Chrome MV2, these requires the "tabs" permission | ||
browser.tabs.onActivated.addListener(this._onActivated); | ||
try { | ||
browser.tabs.onUpdated.addListener(this._onUpdated, {properties: ["url"]}); | ||
} catch (err) { | ||
if (/filters/.test(err.message)) { | ||
// Chrome doesn't support filters | ||
browser.tabs.onUpdated.addListener(this._onUpdated); | ||
} | ||
} | ||
} | ||
|
||
_onActivated({tabId}) { | ||
this._tabId = tabId; | ||
browser.tabs.get(tabId) | ||
.then(tab => { | ||
this._onUpdated(tabId, null, tab); | ||
}); | ||
} | ||
|
||
_onUpdated(tabId, changeInfo, tab) { | ||
if (!this._tabId || tabId !== this._tabId) { | ||
return; | ||
} | ||
if (!tab.url) { | ||
if (tab.finalUrl) { | ||
tab.url = tab.finalUrl; | ||
} else { | ||
console.warn(`Tab ${tabId} has no url`); | ||
return; | ||
} | ||
} | ||
this._tab = tab; | ||
this.emit("change"); | ||
} | ||
|
||
getTab() { | ||
return this._tab; | ||
} | ||
|
||
destroy() { | ||
browser.tabs.onActivated.removeListener(this._onActivated); | ||
browser.tabs.onUpdated.removeListener(this._onUpdated); | ||
} | ||
|
||
isExtensionPage() { | ||
if (!this._tab) { | ||
return false; | ||
} | ||
return this._tab.url.startsWith(browser.runtime.getURL("")); | ||
} | ||
|
||
} | ||
|
||
export const tabMonitor = new TabMonitor(); |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"contextMenus", | ||
"activeTab", | ||
"notifications", | ||
"tabs", | ||
"<all_urls>" | ||
], | ||
"optional_permissions": [ | ||
|