forked from dpacassi/view-page-source-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (41 loc) · 1.12 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
'use strict';
var browser = browser;
(function(browser) {
// Local variables.
var browser = browser;
// If browser is not defined, the plugin was loaded into Google Chrome.
if (typeof browser === 'undefined') {
browser = chrome;
}
/**
* Checks if the page source for a given url can be shown.
*/
function isValidPageSourceUrl(url) {
return !url.startsWith('view-source:');
}
/**
* Opens a new tab with the page source.
*/
browser.browserAction.onClicked.addListener(function(tab) {
var url = tab.url;
if (isValidPageSourceUrl(url)) {
browser.tabs.create({
url: 'view-source:' + url,
index: tab.index + 1
});
}
});
/**
* Enables or disables the app icon accordingly when changing the url.
*/
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (typeof browser.browserAction.disable !== 'undefined') {
var url = changeInfo.url || tab.url;
if (isValidPageSourceUrl(url)) {
browser.browserAction.enable(tabId);
} else {
browser.browserAction.disable(tabId);
}
}
});
})(browser);