Skip to content

Instantly share code, notes, and snippets.

@tommai78101
Created January 11, 2019 16:18
Show Gist options
  • Save tommai78101/b8e520b21fe3116d32a2b6020dd873fc to your computer and use it in GitHub Desktop.
Save tommai78101/b8e520b21fe3116d32a2b6020dd873fc to your computer and use it in GitHub Desktop.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// This helps to determine what browser platform this code is being run on. It is detected at runtime.
const BrowserType = function() {
return {
NONE: -1,
Chrome: 0,
Firefox: 1,
Edge: 2,
Get: function() {
//Isolated namespace checking when we can't even access the background script.
var result = BrowserType.NONE;
if (typeof browser === "undefined") {
result = BrowserType.Chrome;
return result;
}
let supportsPromises = false;
try {
supportsPromises = browser.runtime.getPlatformInfo() instanceof Promise;
}
catch (e) {
supportsPromises = false;
}
if (!supportsPromises)
result = BrowserType.Edge;
else
result = BrowserType.Firefox;
return result;
}
};
}();
window.namespace = (BrowserType.Get() === BrowserType.Chrome ? chrome : browser);
--------------------------------------
// Arbitrary callback functions scattered throughout the project.
function foo(myFlag, myCondition, myValues) {
//...
}
function bar(myValues) {
//...
}
// These arbitrary callback functions are to be wrapped by a wrapper function, given below, to do tasks. Code is wrapped like so:
wrapper_legacy(foo);
wrapper_new(bar);
wrapper_legacy(bar);
wrapper_new(foo);
// etc...
--------------------------------------
// Old code looks like this. We can't modify this code too drastically.
function wrapper_legacy(callback) {
//A global namespace for Firefox and Edge
if (BrowserType.Get() === BrowserType.Firefox) {
window.namespace.tabs.sendMessage({}).then(callback);
}
//A global namespace for Chrome
else if ((BrowserType.Get() === BrowserType.Chrome) || (BrowserType.Get() === BrowserType.Edge)) {
window.namespace.tabs.sendMessage({}, callback);
}
}
//Intended new code
function wrapper_new(callback) {
//Major change - We need to add "chrome.runtime.lastError" specifically for Chrome platform.
//A global namespace for Firefox and Edge
if (BrowserType.Get() === BrowserType.Firefox) {
window.namespace.tabs.sendMessage({}).then(callback);
}
//A global namespace for Chrome
else if ((BrowserType.Get() === BrowserType.Chrome) || (BrowserType.Get() === BrowserType.Edge)) {
// This is not going to work, because this code needs to be inside the "callback" function called by "tabs.sendMessage", in order to get the runtime error message.
if (BrowserType.Get() === BrowserType.Chrome)
if (chrome.runtime.lastError)
console.log(chrome.runtime.lastError.message);
window.namespace.tabs.sendMessage({}, callback);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment