Skip to content

Commit 7a62c2e

Browse files
authored
Merge pull request #39 from leor-gh/master
Add support for automatic persistent data migration when upgrading from MV2 to MV3
2 parents 0a54878 + 9426645 commit 7a62c2e

File tree

5 files changed

+62
-17
lines changed

5 files changed

+62
-17
lines changed

background.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,25 @@ chrome.webRequest.onAuthRequired.addListener(
6060
{urls: ["<all_urls>"]},
6161
['asyncBlocking'] );
6262

63-
chrome.runtime.onMessage.addListener(async function(msg, sender, res) {
63+
chrome.runtime.onMessage.addListener((msg, sender, res) => {
6464
if (msg.action != "authUpdate")
6565
return;
6666

67-
console.log("%s onMessage listener", new Date(Date.now()).toISOString());
68-
if (localStorage.proxySetting == undefined)
69-
await getLocalStorage();
67+
(async () => {
68+
console.log("%s receive authUpdate", new Date(Date.now()).toISOString());
69+
if (localStorage.proxySetting == undefined)
70+
await getLocalStorage();
7071

71-
var proxySetting = JSON.parse(localStorage.proxySetting);
72-
proxySetting['auth'] = msg.data;
73-
localStorage.proxySetting = JSON.stringify(proxySetting);
74-
chrome.storage.local.set(localStorage);
72+
var proxySetting = JSON.parse(localStorage.proxySetting);
73+
proxySetting['auth'] = msg.data;
74+
localStorage.proxySetting = JSON.stringify(proxySetting);
75+
await chrome.storage.local.set(localStorage);
76+
77+
console.log("%s sending authUpdate response", new Date(Date.now()).toISOString());
78+
res('done');
79+
})();
80+
81+
return true;
7582
});
7683

7784
var proxySetting = {
@@ -116,6 +123,23 @@ chrome.runtime.onInstalled.addListener(async details => {
116123
if (store.proxySetting == undefined) {
117124
localStorage.proxySetting = JSON.stringify(proxySetting);
118125
await chrome.storage.local.set(localStorage);
126+
127+
if (details.reason == "update") {
128+
chrome.runtime.onMessage.addListener((msg, sender, res) => {
129+
if (msg.action != "migrationDone")
130+
return;
131+
132+
console.log("%s data migration done", new Date(Date.now()).toISOString());
133+
chrome.offscreen.closeDocument();
134+
});
135+
136+
console.log("%s starting data migration", new Date(Date.now()).toISOString());
137+
chrome.offscreen.createDocument({
138+
url: 'migration.html',
139+
reasons: ['LOCAL_STORAGE'],
140+
justification: 'Migrate storage data for MV2 to MV3',
141+
});
142+
}
119143
}
120144
if(details.reason == "install") {
121145
gotoPage('options.html');

doc/migration

+7-8
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ During normal operation:
7373
- Service worker comes and goes, and credential information is
7474
lazy-loaded only when needed
7575

76-
After migration of installed MV2 extension to MV3, on the first run:
76+
During update migration of installed MV2 extension to MV3:
7777
- localStorage is populated with valid data
7878
- chrome.storage is empty, and the service worker populates the
7979
template proxySetting into chrome.storage, but this does not have
8080
valid proxy authorization credentials
81-
- proxy authorization will fail, and the browser presents a popup
82-
dialog to prompt user to enter proxy credentials
83-
- the option page still displays the correct credentials
84-
- this migration issue can be fixed by deleting and re-entering the
85-
credential information (both the username and password) in the
86-
options page to force the service worker receive and persist the
87-
correct data for its use
81+
- the onInstall handler sees a reason of "update", which means this
82+
is the MV2 to MV3 migration, and it kicks off the migration
83+
offscreen page
84+
- the offscreen page JS code checks localStorage to see if it has
85+
non-empty credentials, and sends a message to the service worker
86+
to update the credentials in chrome.storage

javascripts/migration.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(async () => {
2+
console.log("%s data migration. localStorage =",
3+
new Date(Date.now()).toISOString(),
4+
JSON.parse(JSON.stringify(localStorage)));
5+
6+
let resp;
7+
if (localStorage.proxySetting != undefined) {
8+
auth = JSON.parse(localStorage.proxySetting).auth;
9+
if (auth.user != '' || auth.pass != '') {
10+
resp = chrome.runtime.sendMessage({ action: 'authUpdate', data: auth });
11+
}
12+
}
13+
await resp;
14+
15+
chrome.runtime.sendMessage({ action: 'migrationDone' });
16+
})();

manifest.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"tabs",
1515
"storage",
1616
"webRequest",
17-
"webRequestAuthProvider"
17+
"webRequestAuthProvider",
18+
"offscreen"
1819
],
1920
"host_permissions": [
2021
"*://*/*"

migration.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<head>
3+
<script src="javascripts/migration.js"></script>
4+
</head>
5+
</html>

0 commit comments

Comments
 (0)