From 004219dcc80cbb5819f2c9231d00476974e0f9d4 Mon Sep 17 00:00:00 2001 From: Jomic Date: Sun, 16 Jun 2024 00:25:13 +0800 Subject: [PATCH] fix setting lost by replacing storage.local with sync --- CHANGELOG.md | 3 + _locales/en/messages.json | 2 +- js/builtin.js | 202 +++++++++++++++++++------------------- js/syncloc.js | 19 ++++ manifest.json | 3 +- 5 files changed, 126 insertions(+), 103 deletions(-) create mode 100644 js/syncloc.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 0835f51..dc7447c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.5.1 +- 修复改成跨浏览器同步数据后会丢失配置 + # 2.5.0 - 添加语言本地化(简体中文、繁体中文、英文) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 4128c79..2fa44e7 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -6,7 +6,7 @@ "message": "Easy Switch" }, "description": { - "message": "Easily switching between websites, retaining search content for continued searching. You can switch using buttons or shortcuts. Supports custom search websites. Ported from a discontinued Chrome extension with the same name." + "message": "Easily switching between websites, retaining search content for continued searching. You can switch using buttons or shortcuts." }, "noswitch": { "message": "No switch" diff --git a/js/builtin.js b/js/builtin.js index f24cf82..4a21de9 100644 --- a/js/builtin.js +++ b/js/builtin.js @@ -1,134 +1,134 @@ const Baidu = new SearchSite({ - name: 'Baidu', - icon: 'icon/baidu.png', - tip: '百度一下', - home: 'https://www.baidu.com/', - searchUrl: 'https://www.baidu.com/s?wd=%s', - matches: ['//[\\w]*\\.baidu\\.com'], - exclude: ['//(video|v|baike)\\.baidu\\.com'], - types: [{ - type: 'news', - home: 'http://news.baidu.com/', - searchUrl: 'http://news.baidu.com/ns?word=%s', - }] + name: 'Baidu', + icon: 'icon/baidu.png', + tip: '百度一下', + home: 'https://www.baidu.com/', + searchUrl: 'https://www.baidu.com/s?wd=%s', + matches: ['//[\\w]*\\.baidu\\.com'], + exclude: ['//(video|v|baike)\\.baidu\\.com'], + types: [{ + type: 'news', + home: 'http://news.baidu.com/', + searchUrl: 'http://news.baidu.com/ns?word=%s', + }] }); const Bing = new SearchSite({ - name: 'Bing', - icon: 'icon/bing.png', - home: 'https://cn.bing.com/', - matches: ['//www\\.bing\\.com', '//cn\\.bing\\.com'], - searchUrl: 'https://cn.bing.com/search?q=%s', - types: [{ - type: 'news', - home: 'https://cn.bing.com/news', - searchUrl: 'https://cn.bing.com/news/search?q=%s', - }] + name: 'Bing', + icon: 'icon/bing.png', + home: 'https://cn.bing.com/', + matches: ['//www\\.bing\\.com', '//cn\\.bing\\.com'], + searchUrl: 'https://cn.bing.com/search?q=%s', + types: [{ + type: 'news', + home: 'https://cn.bing.com/news', + searchUrl: 'https://cn.bing.com/news/search?q=%s', + }] }); const Google = new SearchSite({ - name: 'Google', - icon: 'icon/google.png', - home: 'https://www.google.com/', - searchUrl: 'https://www.google.com/search?newwindow=1&hl=zh-CN&q=%s', - matches: ['//[\\w]*\\.google\\.'], - exclude: ['chrome\\.google\\.com'], - types: [{ - type: 'news', - home: 'https://news.google.com/nwshp?hl=zh-CN', - searchUrl: 'https://www.google.com/search?q=%s&newwindow=1&tbm=nws&hl=zh-CN', - typeRegexp: '(news\\.google\\.com|\\.google\\.com.*?\\Wtbm=nws)', - }] + name: 'Google', + icon: 'icon/google.png', + home: 'https://www.google.com/', + searchUrl: 'https://www.google.com/search?newwindow=1&hl=zh-CN&q=%s', + matches: ['//[\\w]*\\.google\\.'], + exclude: ['chrome\\.google\\.com'], + types: [{ + type: 'news', + home: 'https://news.google.com/nwshp?hl=zh-CN', + searchUrl: 'https://www.google.com/search?q=%s&newwindow=1&tbm=nws&hl=zh-CN', + typeRegexp: '(news\\.google\\.com|\\.google\\.com.*?\\Wtbm=nws)', + }] }); const Youku = new SearchSite({ - name: 'Youku', - icon: 'icon/youku.png', - home: 'http://www.youku.com/', - searchUrl: 'http://www.soku.com/v?keyword=%s', - matches: ['www\\.soku\\.com'] + name: 'Youku', + icon: 'icon/youku.png', + home: 'http://www.youku.com/', + searchUrl: 'http://www.soku.com/v?keyword=%s', + matches: ['www\\.soku\\.com'] }); const Douyin = new SearchSite({ - name: 'Douyin', - icon: 'icon/douyin.png', - home: 'https://www.douyin.com/', - searchUrl: 'https://www.douyin.com/search/%s', - matches: ['www\\.douyin\\.com'] + name: 'Douyin', + icon: 'icon/douyin.png', + home: 'https://www.douyin.com/', + searchUrl: 'https://www.douyin.com/search/%s', + matches: ['www\\.douyin\\.com'] }); const BaiduVideo = new SearchSite({ - name: 'BaiduVideo', - icon: 'icon/haokan.png', - home: 'http://v.baidu.com/', - matches: ['//(video|v)\\.baidu\\.com'], - searchUrl: 'http://v.baidu.com/v?word=%s&ie=utf-8' + name: 'BaiduVideo', + icon: 'icon/haokan.png', + home: 'http://v.baidu.com/', + matches: ['//(video|v)\\.baidu\\.com'], + searchUrl: 'http://v.baidu.com/v?word=%s&ie=utf-8' }); const BaiduBaike = new SearchSite({ - name: 'BaiduBaike', - icon: 'icon/baike.png', - home: 'http://baike.baidu.com/', - searchUrl: 'http://baike.baidu.com/search?word=%s&pn=0&rn=0&enc=utf8', + name: 'BaiduBaike', + icon: 'icon/baike.png', + home: 'http://baike.baidu.com/', + searchUrl: 'http://baike.baidu.com/search?word=%s&pn=0&rn=0&enc=utf8', }); const Wiki = new SearchSite({ - name: 'WikiCN', - icon: 'icon/wiki.png', - home: 'https://zh.wikipedia.org/', - searchUrl: 'https://zh.wikipedia.org/zh-cn/%s', - matches: ['//[\\w]*\\.wikipedia\\.org'], - q: 'wiki' + name: 'WikiCN', + icon: 'icon/wiki.png', + home: 'https://zh.wikipedia.org/', + searchUrl: 'https://zh.wikipedia.org/zh-cn/%s', + matches: ['//[\\w]*\\.wikipedia\\.org'], + q: 'wiki' }); const Wikiwand = new SearchSite({ - name: 'Wikiwand', - icon: 'icon/wikiwand.png', - home: 'https://www.wikiwand.com/', - searchUrl: 'https://www.wikiwand.com/zh-cn/%s', - matches: ['//[\\w]*\\.wikiwand\\.com'], - q: 'wiki' + name: 'Wikiwand', + icon: 'icon/wikiwand.png', + home: 'https://www.wikiwand.com/', + searchUrl: 'https://www.wikiwand.com/zh-cn/%s', + matches: ['//[\\w]*\\.wikiwand\\.com'], + q: 'wiki' }); const Bilibili = new SearchSite({ - name: 'Bilibili', - icon: 'icon/bilibili.png', - home: 'https://www.bilibili.com/', - searchUrl: 'https://search.bilibili.com/all?keyword=%s', - matches: ['//search\\.bilibili\\.com'] + name: 'Bilibili', + icon: 'icon/bilibili.png', + home: 'https://www.bilibili.com/', + searchUrl: 'https://search.bilibili.com/all?keyword=%s', + matches: ['//search\\.bilibili\\.com'] }); const Zhihu = new SearchSite({ - name: 'Zhihu', - icon: 'icon/zhihu.png', - home: 'https://www.zhihu.com/', - searchUrl: 'https://www.zhihu.com/search?q=%s', - matches: ['//[\\w]*\\.zhihu\\.com'] + name: 'Zhihu', + icon: 'icon/zhihu.png', + home: 'https://www.zhihu.com/', + searchUrl: 'https://www.zhihu.com/search?q=%s', + matches: ['//[\\w]*\\.zhihu\\.com'] }); const Taobao = new SearchSite({ - name: 'Taobao', - icon: 'icon/taobao.png', - home: 'https://www.taobao.com', - searchUrl: 'https://s.taobao.com/search?q=%s', - matches: ['//[\\w]*\\.taobao\\.com'] + name: 'Taobao', + icon: 'icon/taobao.png', + home: 'https://www.taobao.com', + searchUrl: 'https://s.taobao.com/search?q=%s', + matches: ['//[\\w]*\\.taobao\\.com'] }); const Jingdong = new SearchSite({ - name: 'Jingdong', - icon: 'icon/jd.png', - home: 'https://www.jd.com/', - searchUrl: 'https://search.jd.com/Search?keyword=%s&enc=utf-8', - matches: ['//[\\w]*\\.jd\\.com'] + name: 'Jingdong', + icon: 'icon/jd.png', + home: 'https://www.jd.com/', + searchUrl: 'https://search.jd.com/Search?keyword=%s&enc=utf-8', + matches: ['//[\\w]*\\.jd\\.com'] }); const Github = new SearchSite({ - name: 'Github', - icon: 'icon/github.png', - home: 'https://www.github.com/', - searchUrl: 'https://github.com/search?q=%s', - matches: ['//[\\w\\.]*github\\.com.*search'] + name: 'Github', + icon: 'icon/github.png', + home: 'https://www.github.com/', + searchUrl: 'https://github.com/search?q=%s', + matches: ['//[\\w\\.]*github\\.com.*search'] }); const Douban = new SearchSite({ - name: 'Douban', - icon: 'icon/douban.png', - home: 'https://www.douban.com/', - searchUrl: 'https://www.douban.com/search?q=%s', - matches: ['//[\\w]*\\.douban\\.com'] + name: 'Douban', + icon: 'icon/douban.png', + home: 'https://www.douban.com/', + searchUrl: 'https://www.douban.com/search?q=%s', + matches: ['//[\\w]*\\.douban\\.com'] }); const Smzdm = new SearchSite({ - name: 'Smzdm', - icon: 'icon/smzdm.png', - home: 'https://www.smzdm.com/', - searchUrl: 'https://search.smzdm.com/?s=%s', - matches: ['//[\\w]*\\.smzdm\\.com'], - q: 's' + name: 'Smzdm', + icon: 'icon/smzdm.png', + home: 'https://www.smzdm.com/', + searchUrl: 'https://search.smzdm.com/?s=%s', + matches: ['//[\\w]*\\.smzdm\\.com'], + q: 's' }); const sysSites = [Baidu, Google, Bing, Taobao, Jingdong, Zhihu, Wiki, BaiduBaike, Bilibili, Douyin, Github, Smzdm, Douban, Youku, Wikiwand]; \ No newline at end of file diff --git a/js/syncloc.js b/js/syncloc.js new file mode 100644 index 0000000..3fec7d4 --- /dev/null +++ b/js/syncloc.js @@ -0,0 +1,19 @@ +// migrate the content from chrome.storage.local to chrome.storage.sync +chrome.runtime.onInstalled.addListener(function (details) { + if (details.reason === 'install' || details.reason === 'update') { + chrome.storage.sync.get(null, function (syncData) { + if (Object.keys(syncData).length === 0) { + chrome.storage.local.get(null, function (localData) { + chrome.storage.sync.set(localData, function () { + if (chrome.runtime.lastError) { + console.error("Failed to migrate data to chrome.storage.sync:", chrome.runtime.lastError); + } else { + console.log("Data migrated to chrome.storage.sync successfully."); + chrome.storage.local.clear(); + } + }); + }); + } + }); + } +}); \ No newline at end of file diff --git a/manifest.json b/manifest.json index a0e76cd..fb2cf20 100644 --- a/manifest.json +++ b/manifest.json @@ -2,6 +2,7 @@ "background": { "persistent": false, "scripts": [ + "js/syncloc.js", "js/oop.js", "js/url.js", "js/kw.js", @@ -34,5 +35,5 @@ "*://*/*" ], "short_name": "__MSG_extName__", - "version": "2.5.0" + "version": "2.5.1" } \ No newline at end of file