diff --git a/generate-manifest.js b/generate-manifest.js index 6026c7f..b4614cf 100644 --- a/generate-manifest.js +++ b/generate-manifest.js @@ -95,7 +95,9 @@ var manifest = { ], "js": [ // "assets/js/lib/jquery.min.js", - // "assets/js/content.js", + + // 切换开关刷新页面 + "scripts/content.js", // Double S 快捷搜索 页面注入js "scripts/advanced-search/content.js" diff --git a/html/assets/js/popup.js b/html/assets/js/popup.js index a9c9ec0..71ef773 100644 --- a/html/assets/js/popup.js +++ b/html/assets/js/popup.js @@ -1,4 +1,4 @@ -$(function() { +$(function () { /** * Direct Url */ @@ -21,7 +21,7 @@ $(function() { const btnExpandFulltext = document.querySelector("#btnExpandFulltext"); // 页面加载时,更新界面开关状态 chrome.storage.sync.get("State_ExpandFulltext", ({ State_ExpandFulltext }) => { - btnExpandFulltext.checked = !State_ExpandFulltext; + btnExpandFulltext.checked = !State_ExpandFulltext; }); // 点击开关时存储按钮状态并刷新页面 @@ -36,12 +36,12 @@ $(function() { */ const btnGoogleAds = document.querySelector("#btnAdsBlock"); // 页面加载时,更新界面开关状态 - chrome.storage.sync.get('State_AdsBlock', function(budget) { + chrome.storage.sync.get('State_AdsBlock', function (budget) { btnGoogleAds.checked = !budget.State_AdsBlock; }); // 点击开关时存储按钮状态并刷新页面 - $("#btnAdsBlock").click(function() { + $("#btnAdsBlock").click(function () { chrome.storage.sync.set({ 'State_AdsBlock': !btnGoogleAds.checked }); chrome.extension.getBackgroundPage().updateAdsBlockStatus(!btnGoogleAds.checked); refreshPage('Ads Block'); @@ -53,12 +53,12 @@ $(function() { */ const btnSSSearch = document.querySelector("#btnSSSearch"); // 页面加载时,更新界面开关状态 - chrome.storage.sync.get('State_SSSearch', function(budget) { - btnSSSearch.checked = !budget.State_SSSearch; + chrome.storage.sync.get('State_SSSearch', function (budget) { + btnSSSearch.checked = !budget.State_SSSearch; }); // 点击开关时存储按钮状态并刷新页面 - $("#btnSSSearch").click(function() { + $("#btnSSSearch").click(function () { chrome.storage.sync.set({ 'State_SSSearch': !btnSSSearch.checked }); // refreshPage('SS Search'); }) @@ -72,25 +72,28 @@ $(function() { * @param {} url * @returns */ - function isBrowserSettingPage({url, action, showSorryInfo = true }) { - var protocol, isSettingPage = true; - if(/^chrome:\/\/.*$/.test(url)) { - protocol = "chrome://" - } else if(/^edge:\/\/.*$/.test(url)) { - protocol = "edge://" - } else { - isSettingPage = false; - } - if(showSorryInfo && isSettingPage) { - alert(`十分抱歉,由于浏览器限制,“${protocol}”开头的网站不支持${action}`); - } - return isSettingPage; + function isBrowserSettingPage({ url, action, showSorryInfo = true }) { + var protocol, isSettingPage = true; + if (/^chrome:\/\/.*$/.test(url)) { + protocol = "chrome://" + } else if (/^edge:\/\/.*$/.test(url)) { + protocol = "edge://" + } else { + isSettingPage = false; + } + if (showSorryInfo && isSettingPage) { + alert(`十分抱歉,由于浏览器限制,“${protocol}”开头的网站不支持${action}`); + } + return isSettingPage; } /** * 改变开关自动刷新页面 + * + * 向网页发送一条消息,由注入的脚本接收,完成页面刷新操作 */ function refreshPage(messageInfo) { + console.log("refreshPage", messageInfo) chrome.tabs.query({ active: true, currentWindow: true diff --git a/manifest.json b/manifest.json index 33c97e8..567ed5c 100644 --- a/manifest.json +++ b/manifest.json @@ -1 +1 @@ -{"manifest_version":3,"name":"小墨助手","version":"1.0.0","action":{"default_icon":{"16":"resources/image/logo.png","24":"resources/image/logo.png","32":"resources/image/logo.png"},"default_title":"点击打开小墨助手 配置页面","default_popup":"html/popup.html"},"description":"便捷,易用的浏览器小助手","icons":{"16":"resources/image/logo.png","32":"resources/image/logo.png","48":"resources/image/logo.png","128":"resources/image/logo.png"},"background":{"service_worker":"background.js"},"chrome_url_overrides":{},"commands":{"_execute_action":{"suggested_key":{"default":"Alt+Comma"}},"toggle-feature-foo":{"suggested_key":{"default":"Ctrl+Shift+U","mac":"Command+Shift+U"},"description":"Toggle feature foo","global":true}},"content_scripts":[{"matches":["","*://*/*"],"js":["scripts/advanced-search/content.js"],"css":[],"run_at":"document_start"},{"matches":["*://fanyi.qq.com/*","*://baike.baidu.com/*","*://fanyi.youdao.com/*","*://www.cnki.net/*","*://qikan.cqvip.com/*","*://lib.cqvip.com/*"],"js":["scripts/advanced-search/content-helper.js"],"run_at":"document_start"},{"matches":["*://c.pc.qq.com/middlem.html*","*://docs.qq.com/scenario/link.html*","*://www.tianyancha.com/security*","*://jump.bdimg.com/safecheck/index*","*://jump2.bdimg.com/safecheck/index*","*://www.chinaz.com/go.shtml*","*://www.douban.com/link2/*","*://link.csdn.net/*","*://link.zhihu.com/*","*://link.juejin.cn/*","*://links.jianshu.com/go*","*://www.jianshu.com/go-wild*"],"css":["html/assets/lib/myukitoast/myukitoast.css"],"js":["html/assets/lib/jquery-3.6.0.min.js","html/assets/lib/myukitoast/myukitoast.js","scripts/direct-url/RedirectRule.js","scripts/direct-url/url.js"],"run_at":"document_start"}],"content_security_policy":{},"cross_origin_embedder_policy":{},"cross_origin_opener_policy":{},"event_rules":[],"file_browser_handlers":[],"file_system_provider_capabilities":{},"host_permissions":[],"incognito":"spanning","input_components":[],"minimum_chrome_version":"107","omnibox":{"keyword":"ss"},"optional_host_permissions":[],"optional_permissions":[],"options_page":"html/options.html","options_ui":{},"permissions":["commands","bookmarks","contextMenus","storage","webNavigation"],"requirements":{},"sandbox":{},"storage":{},"tts_engine":{},"version_name":"1.0 beta","web_accessible_resources":[]} \ No newline at end of file +{"manifest_version":3,"name":"小墨助手","version":"1.0.0","action":{"default_icon":{"16":"resources/image/logo.png","24":"resources/image/logo.png","32":"resources/image/logo.png"},"default_title":"点击打开小墨助手 配置页面","default_popup":"html/popup.html"},"description":"便捷,易用的浏览器小助手","icons":{"16":"resources/image/logo.png","32":"resources/image/logo.png","48":"resources/image/logo.png","128":"resources/image/logo.png"},"background":{"service_worker":"background.js"},"chrome_url_overrides":{},"commands":{"_execute_action":{"suggested_key":{"default":"Alt+Comma"}},"toggle-feature-foo":{"suggested_key":{"default":"Ctrl+Shift+U","mac":"Command+Shift+U"},"description":"Toggle feature foo","global":true}},"content_scripts":[{"matches":["","*://*/*"],"js":["scripts/content.js","scripts/advanced-search/content.js"],"css":[],"run_at":"document_start"},{"matches":["*://fanyi.qq.com/*","*://baike.baidu.com/*","*://fanyi.youdao.com/*","*://www.cnki.net/*","*://qikan.cqvip.com/*","*://lib.cqvip.com/*"],"js":["scripts/advanced-search/content-helper.js"],"run_at":"document_start"},{"matches":["*://c.pc.qq.com/middlem.html*","*://docs.qq.com/scenario/link.html*","*://www.tianyancha.com/security*","*://jump.bdimg.com/safecheck/index*","*://jump2.bdimg.com/safecheck/index*","*://www.chinaz.com/go.shtml*","*://www.douban.com/link2/*","*://link.csdn.net/*","*://link.zhihu.com/*","*://link.juejin.cn/*","*://links.jianshu.com/go*","*://www.jianshu.com/go-wild*"],"css":["html/assets/lib/myukitoast/myukitoast.css"],"js":["html/assets/lib/jquery-3.6.0.min.js","html/assets/lib/myukitoast/myukitoast.js","scripts/direct-url/RedirectRule.js","scripts/direct-url/url.js"],"run_at":"document_start"}],"content_security_policy":{},"cross_origin_embedder_policy":{},"cross_origin_opener_policy":{},"event_rules":[],"file_browser_handlers":[],"file_system_provider_capabilities":{},"host_permissions":[],"incognito":"spanning","input_components":[],"minimum_chrome_version":"107","omnibox":{"keyword":"ss"},"optional_host_permissions":[],"optional_permissions":[],"options_page":"html/options.html","options_ui":{},"permissions":["commands","bookmarks","contextMenus","storage","webNavigation"],"requirements":{},"sandbox":{},"storage":{},"tts_engine":{},"version_name":"1.0 beta","web_accessible_resources":[]} \ No newline at end of file diff --git a/scripts/content.js b/scripts/content.js new file mode 100644 index 0000000..14ebd6a --- /dev/null +++ b/scripts/content.js @@ -0,0 +1,14 @@ +console.log("[小墨助手]", "插件已启用") + +// 每次改变开关状态时刷新页面使功能及时生效 +chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { + console.log(`进入 scripts/content.js 的 onMessage Listener`) + + if (request.action === 'refreshPage') { + location.reload(); + sendResponse('Reload page because of ' + request.info); + } + + console.log(`离开 scripts/content.js 的 onMessage Listener`) + return true; +})