{"id":10006875,"date":"2022-11-19T14:35:08","date_gmt":"2022-11-19T06:35:08","guid":{"rendered":"https:\/\/ityet.com\/?p=10006875"},"modified":"2022-11-19T14:35:08","modified_gmt":"2022-11-19T06:35:08","slug":"chrome-extension-v3-%e5%bc%80%e5%8f%91%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006875","title":{"rendered":"Chrome Extension v3 \u5f00\u53d1\u6307\u5357"},"content":{"rendered":"<p>\u6269\u5c55\u7a0b\u5e8f\u662f\u57fa\u4e8e\u4e8b\u4ef6\u7684\u7a0b\u5e8f\uff0c\u7528\u4e8e\u4fee\u6539\u6216\u589e\u5f3a Chrome \u6d4f\u89c8\u4f53\u9a8c\uff0c\u5982\u679c\u6b64\u65f6\u4f60\u60f3\u6784\u5efa\u4e00\u4e2a Chrome \u6269\u5c55\u7a0b\u5e8f\uff0c\u5e76\u52aa\u529b\u5bfb\u627e\u4e00\u7bc7\u6db5\u76d6 Chrome \u6269\u5c55\u7a0b\u5e8f\u7684\u6574\u4e2a\u6784\u601d\u3001\u6784\u5efa\u548c\u542f\u52a8\u8fc7\u7a0b\u7684\u6587\u7ae0\uff0c\u8fd9\u91cc\u6709\u4e00\u4e2a\u7efc\u5408\u6307\u5357\uff0c\u53ef\u5e2e\u52a9\u60a8\u5b8c\u6210\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>\u672c\u6587\u5206\u51e0\u4e2a\u90e8\u5206\u8bb2\u89e3 Chrome \u6269\u5c55\u5f00\u53d1\u7684\u5168\u6d41\u7a0b\u4ee5\u53ca\u9ad8\u7ea7\u4f7f\u7528\u6280\u5de7\uff0c<strong>\u9002\u7528\u7528\u6700\u65b0\u7684 <code>v3<\/code> \u7248\u672c<\/strong>\uff0c\u5e0c\u671b\u8fd9\u53ef\u4ee5\u8282\u7701\u4f60\u5b66\u4e60\u6d4f\u89c8\u5668\u63d2\u4ef6\u5f00\u53d1\u5165\u95e8\u548c\u63d0\u5347\u7684\u65f6\u95f4\uff0c<a href=\"https:\/\/link.segmentfault.com\/?enc=1VYg7AEUjd1OfGf5oNZYsA%3D%3D.aH4Be%2BoBKt6FgwEKerZsbdkH5C7bpr6xyAaX5inwv578cAdL1hjPlJAjjOa4g3cF\" rel=\"nofollow\">\u8be6\u7ec6\u5185\u5bb9\u8bf7\u53c2\u9605\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u662fV3\uff1f<\/h2>\n<ol>\n<li>\u66f4\u9ad8\u7684\u5b89\u5168\u6027\u3001\u9690\u79c1\u6027\u548c\u6027\u80fd<\/li>\n<li>\u652f\u6301 service workers \u548c promises<\/li>\n<li>\u83b7\u5f97\u66f4\u5feb\u901f\u7684\u4ee3\u7801\u5ba1\u6838<\/li>\n<li>Chrome \u7f51\u4e0a\u5e94\u7528\u5e97\u4e0d\u518d\u63a5\u53d7 Manifest V2 \u6269\u5c55<\/li>\n<\/ol>\n<h2>\u4e3a\u4ec0\u4e48\u4f7f\u7528 Chrome \u6269\u5c55\u7a0b\u5e8f\uff1f<\/h2>\n<ol>\n<li>\n<p><strong>\u5f00\u53d1\u95e8\u69db\u4f4e<\/strong><\/p>\n<p>\u51e0\u4e4e\u652f\u6301\u4efb\u610f\u7684\u6280\u672f\u6808\uff0c\u5bf9\u4e8e\u524d\u7aef\u5165\u95e8\u7528\u6237\u5982\u679c\u60f3\u5f00\u53d1\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u4e8e\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u7684\u63d2\u4ef6\u529f\u80fd\uff0c\u4f8b\u5982\u9ad8\u4eae\u5173\u952e\u5b57\u3001\u589e\u52a0\u9ed1\u591c\u6a21\u5f0f\u7b49\u4f1a\u6bd4\u5f00\u53d1\u4f20\u7edf\u7c7b\u4f3c\u529f\u80fd\u7684\u7f51\u7ad9\u548c\u79fb\u52a8\u5e94\u7528\u6210\u672c\u4f4e\u7684\u591a\uff0c\u5373\u4f7f\u4f60\u4e0d\u4f1a\u4f7f\u7528 <code>React<\/code> \u6216\u8005 <code>Vue<\/code> \u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>Jquery<\/code> \u53c8\u6216\u8005\u662f\u539f\u751f <code>Javascript<\/code> \u53bb\u5b9e\u73b0\u5b83\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8986\u76d6\u8303\u56f4\u5e7f<\/strong><\/p>\n<p>Chrome \u5728\u5e02\u573a\u4efd\u989d\u4e0a\u4ee5\u5f88\u5927\u7684\u4f18\u52bf\u51fb\u8d25\u4e86\u5176\u4ed6\u6d4f\u89c8\u5668\uff0c\u56e0\u6b64\uff0c\u4f18\u5148\u5f00\u53d1 Chrome \u6269\u5c55\u662f\u83b7\u53d6\u4e0b\u8f7d\u91cf\u548c\u6d41\u91cf\u6700\u597d\u7684\u5165\u53e3\u3002\u63d2\u4ef6\u90e8\u7f72\u540e\uff0c\u6240\u6709 Chrome \u7528\u6237\u90fd\u53ef\u4ee5\u5728<a href=\"https:\/\/link.segmentfault.com\/?enc=wFbpyH7KQPO1S7gl01oTgQ%3D%3D.QrkRnPthsUFhps1I24M%2FtN%2B8jXeki0vtqHsCKj65%2F04HlUTV%2FORqi5sq4zGEpr9XupQ79wFTzReWaSdIW%2Bp6cIRyU6FvrVMDI7CyXJQKs%2Bs%3D\" rel=\"nofollow\">Chrome \u7f51\u4e0a\u5e94\u7528\u5e97<\/a>\u4e0b\u8f7d\u4f60\u7684\u6269\u5c55\u7a0b\u5e8f\u3002<\/p>\n<\/li>\n<li>\n<p><strong>u can do whtever u want<\/strong><\/p>\n<p>\u5728\u4f60\u7684\u6269\u5c55\u7a0b\u5e8f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4e0d\u7528\u4e3a\u6d4f\u89c8\u5668\u7684\u540c\u6e90\u7b56\u7565\u62c5\u5fe7\uff0c\u4f60\u53ef\u4ee5\u5728\u4f60\u60f3\u8981\u7684\u4efb\u610f\u7f51\u7ad9\u4e2d&#8221;\u4fb5\u5165&#8221;\u4f60\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u50cf <code>React devTools<\/code> \u90a3\u6837\u589e\u5f3a\u4f60\u81ea\u5df1\u7684\u8c03\u8bd5\u5668\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u5728\u4f60\u7684\u6269\u5c55\u7a0b\u5e8f\u4e2d\u7ba1\u7406\u522b\u4eba\u7684\u6269\u5c55\u7a0b\u5e8f\uff08\u5982\u9632\u9493\u9c7c\uff0c\u9632\u6c89\u8ff7\u7b49\uff09\u3002<\/p>\n<\/li>\n<\/ol>\n<h2>Chrome Extension \u67b6\u6784\u7ec4\u6210<\/h2>\n<h3>manifest.json<\/h3>\n<pre><code class=\"json\">{\n   &quot;name&quot;: &quot;__MSG_extName__&quot;, \/\/ \u56fd\u9645\u5316\u8bed\u6cd5\uff0c\u6216\u9ed8\u8ba4\u53bb\u6839\u76ee\u5f55\u4e0b\u627e_locales.en(\u5bf9\u5e94\u7684\u8bed\u8a00\u5305).message.extName\n   &quot;version&quot;: &quot;1.0.0&quot;,\n   &quot;description&quot;: &quot;__MSG_extDescription__&quot;, \/\/ \u540cname\n   &quot;icons&quot;: {\n    '16': 'src\/assets\/icons\/icon16.png',\n    '32': 'src\/assets\/icons\/icon32.png',\n    '48': 'src\/assets\/icons\/icon48.png',\n    '128': 'src\/assets\/icons\/icon128.png'\n    },\n&quot;background&quot;: {\n      &quot;persistent&quot;: false, \/\/ \u4fdd\u6301\u540e\u53f0\u811a\u672c\u6301\u7eed\u6d3b\u52a8\u7684\u552f\u4e00\u60c5\u51b5\u662f\u6269\u5c55\u4f7f\u7528chrome.webRequest API \u6765\u963b\u6b62\u6216\u4fee\u6539\u7f51\u7edc\u8bf7\u6c42\u3002webRequest API \u4e0e\u975e\u6301\u4e45\u6027\u540e\u53f0\u9875\u9762\u4e0d\u517c\u5bb9\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c&quot;persistent&quot;\u8bbe\u7f6e\u4e3a true\u3002\n      &quot;scripts&quot;: [ &quot;background.js&quot; ]\n    },\n   \n   &quot;content_scripts&quot;: [ {\n      &quot;js&quot;: [ 'src\/content\/ethers\/address.tsx' ],\n      &quot;matches&quot;: [ &quot;*:\/\/etherscan.io\/address\/*&quot;, &quot;*:\/\/*.bscscan.com\/address\/*&quot; ]\n   } ],\n&quot;web_accessible_resources&quot;: [\n    {\n      &quot;matches&quot;: ['&lt;all_urls&gt;'],\n      &quot;resources&quot;: ['src\/assets\/images\/*.png']\n    }\n  ],\n  &quot;action&quot;: {\n    &quot;default_popup&quot;: 'src\/popup\/popup.html',\n    &quot;default_icon&quot;: {\n      '16': 'src\/assets\/icons\/icon16.png',\n      '32': 'src\/assets\/icons\/icon32.png',\n      '48': 'src\/assets\/icons\/icon48.png',\n      '128': 'src\/assets\/icons\/icon128.png'\n    }\n  },\n  &quot;permissions&quot;: ['storage', 'webNavigation', 'webRequest'], \/\/ \u6ca1\u6709\u7528\u5230\u7684\u6743\u9650\u4e0d\u8981\u6dfb\u52a0\uff0c\u5426\u5219\u5ba1\u6838\u8fc7\u4e0d\u4e86\n  &quot;host_permissions&quot;: [\n    '*:\/\/explorer.btc.com\/*',\n    '*:\/\/etherscan.io\/*',\n    '*:\/\/cn.etherscan.com\/*',\n    '*:\/\/polygonscan.com\/*',\n    '*:\/\/*.bscscan.com\/*',\n    '*:\/\/snowtrace.io\/*',\n    '*:\/\/optimistic.etherscan.io\/*',\n    '*:\/\/arbiscan.io\/*',\n    '*:\/\/ftmscan.com\/*',\n    '*:\/\/cronoscan.com\/*',\n    '*:\/\/*.moonscan.io\/*',\n    'https:\/\/*.blocksec.com\/*', \/\/ \u81ea\u5df1\u7684\u4e1a\u52a1\u8bf7\u6c42api\u57df\u540d\uff0c\u8fd9\u6837\u624d\u4e0d\u4f1a\u8de8\u57df\n    'https:\/\/explorer.api.btc.com\/*' \/\/ \u4f7f\u7528webRequest\u76d1\u542c\u8bf7\u6c42\u4fe1\u606f\uff0c\u88ab\u76d1\u542c\u7684\u57df\u540d\u4e5f\u8981\u914d\u7f6e\uff0c\u5426\u5219\u4e0d\u751f\u6548\n  ] \n}<\/code><\/pre>\n<p>\u914d\u7f6e\u6e05\u5355\u6709\u51e0\u4e2a\u6ce8\u610f\u70b9\uff1a<\/p>\n<ol>\n<li>\u4ee5\u6700\u5c0f\u6743\u9650\u7ea6\u675f\u63d2\u4ef6\uff0c<code>permissions<\/code> \u5b57\u6bb5\u5982\u679c\u5199\u4e86\u4f60\u5e94\u7528\u4e2d\u6ca1\u7528\u5230\uff08\u6216\u8005\u4e0d\u5fc5\u8981\uff09\u7684\u6743\u9650\uff0c\u5728\u63d0\u4ea4\u5ba1\u6838\u65f6\u4f1a\u88ab\u62d2\u7edd\u3002\u6ce8\u610f <code>tabs<\/code> \u7edd\u5927\u90e8\u5206API\u662f\u4e0d\u9700\u8981\u7533\u8bf7\u8fd9\u4e2a <code>tabs<\/code> \u6743\u9650\u7684\u3002<\/li>\n<li>\u5982\u679c\u6d89\u53ca\u8de8\u57df\u8bf7\u6c42\uff0c\u9700\u8981\u5728 <code>host_permissions<\/code> \u91cc\u9762\u914d\u7f6e\u57df\u540d\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u7528 <code>&lt;all_urls&gt;<\/code> \uff0c\u66f4\u597d\u7684\u6ce8\u91cd\u7528\u6237\u9690\u79c1\u7684\u505a\u6cd5\u662f\u5b9e\u4e8b\u6c42\u662f\uff0c\u7528\u5230\u54ea\u4e9b\u5217\u54ea\u4e9b\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4f1a\u5b58\u5728\u7528\u6237\u7684\u786c\u76d8\u4e0a\uff0c\u8fd9\u4f1a\u5f15\u8d77\u7528\u6237\u62c5\u5fe7\u3002<\/li>\n<li>\u5982\u679c\u6d89\u53ca <code>webRequest<\/code> \u62e6\u622a\u8bf7\u6c42\uff0c\u6bd4\u5982\u76d1\u542c\u7528\u6237\u7ffb\u9875\u4e86\uff08\u5176\u5b9e\u5c31\u662f\u76d1\u542c <code>getMore<\/code> \u7684\u63a5\u53e3\u8bf7\u6c42\u5b8c\u6210\u4e86\uff09\uff0c\u9700\u8981\u628a\u4f60\u8981\u76d1\u542c\u7684\u8bf7\u6c42\u57df\u540d\u914d\u7f6e\u5728 <code>permissions<\/code>\u3002<\/li>\n<li><code>background<\/code> \u4e2d\u4fdd\u6301\u540e\u53f0\u811a\u672c\u6301\u7eed\u6d3b\u52a8\u7684\u552f\u4e00\u60c5\u51b5\u662f\u6269\u5c55\u4f7f\u7528 <code>chrome.webRequest<\/code> API \u6765\u963b\u6b62\u6216\u4fee\u6539\u7f51\u7edc\u8bf7\u6c42\u3002<code>webRequest<\/code> API \u4e0e\u975e\u6301\u4e45\u6027\u540e\u53f0\u9875\u9762\u4e0d\u517c\u5bb9\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>&quot;persistent&quot;<\/code>\u8bbe\u7f6e\u4e3a <code>true<\/code>\u3002<\/li>\n<li>\u65e0\u8bba\u662f <code>matches<\/code> \u3001<code>resources<\/code> \u8fd8\u662f <code>host_permissions<\/code> \u90fd\u53ef\u4ee5\u7528\u901a\u914d\u7b26\u63cf\u8ff0\u3002<\/li>\n<li>\u63d2\u4ef6\u4e2d\u7528\u5230\u7684\u9759\u6001\u8d44\u6e90\u90fd\u9700\u8981\u5728 <code>web_accessible_resources<\/code> \u4e2d\u914d\u7f6e\u3002<\/li>\n<li><code>content_scripts<\/code> \u4e2d\u7684\u8d44\u6e90\u662f\u6309\u7167\u4f60\u5217\u8868\u4e2d\u7684\u987a\u5e8f\u52a0\u8f7d\u5230\u9875\u9762\u4e2d\u7684\uff0c\u8bf7\u6ce8\u610f\u4f9d\u8d56\u5173\u7cfb\u7684\u5148\u540e\u987a\u5e8f\u3002<\/li>\n<\/ol>\n<h3>background.js<\/h3>\n<p>\u540e\u53f0\u811a\u672c\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u5904\u4e8e\u4f11\u7720\u72b6\u6001\uff0c\u5e76\u4e14\u5305\u542b\u4ec5\u5728\u67d0\u4e9b\u6d4f\u89c8\u5668\u4e8b\u4ef6\u53d1\u751f\u65f6\u624d\u89e6\u53d1\u811a\u672c\u7684\u4fa6\u542c\u5668\u3002 \u540e\u53f0\u811a\u672c\u4f1a\u8d2f\u7a7f\u4f60\u63d2\u4ef6\u5e94\u7528\u7684\u5168\u751f\u547d\u5468\u671f\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u4e00\u822c\u7528\u4e8e\u76d1\u542c <code>Popup<\/code> \u6216\u8005 <code>content script<\/code> \u7684\u4e8b\u4ef6\uff0c\u4f8b\u5982\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002<\/p>\n<pre><code class=\"typescript\">import { chromeEvent } from '@common\/event'\nimport { reloadCurrentTab, isNil } from '@common\/utils'\nimport commonApi from '@common\/api'\nimport { REFRESH } from '@common\/constants'\n\n\/** refresh current page (usually user change the settings) *\/\nchromeEvent.on(REFRESH, () =&gt; {\n  reloadCurrentTab()\n})\n\nchromeEvent.on('custom-event-name', async params =&gt; {\n  try {\n    const { success, data, msg } = await commonApi.getXxx(params)\n    return {\n      success: success,\n      data: data,\n      message: msg\n    }\n  } catch (error) {\n    \/** external exception *\/\n    return { success: false, data: error, message: 'error' }\n  }\n})\n\nchrome.webRequest.onCompleted.addListener(\n  async details =&gt; {\n    const { url, tabId } = details\n        \/\/ do something\n  },\n  { urls: [] }\n)\n<\/code><\/pre>\n<h3>popup<\/h3>\n<p>\u7528\u6237\u754c\u9762\uff0c\u70b9\u51fb\u6d4f\u89c8\u5668\u6269\u5c55\u56fe\u6807\u540e\u5c55\u793a\u7684UI\u5143\u7d20\u3002<\/p>\n<p>&lt;img src=&quot;https:\/\/picgo-cloudimg.oss-cn-hangzhou.aliyuncs.com\/img\/Xnip2022-11-19_09-14-32.jpg&quot; style=&quot;zoom:30%;&quot; \/&gt;<\/p>\n<p>\u5f00\u53d1popup\u4e0e\u4f60\u5f00\u53d1\u4e00\u4e2a\u6b63\u5e38\u7684webapp\u65f6\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u552f\u4e00\u9700\u8981\u6ce8\u610f\u7684\u662f\u6253\u5305\u7684\u65f6\u5019\u9700\u8981\u628apopup.html\u914d\u7f6e\u5230entry\u548coutput\u4ee5\u6b63\u5e38\u8bbf\u95ee\u5230\u8fd9\u4e2a\u9875\u9762\uff0c\u5f00\u53d1\u65f6\u76ee\u5f55\u7ed3\u6784\u7c7b\u4f3c\u4ee5\u4e0b\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851132\" alt=\"image-20221119092623788\" title=\"image-20221119092623788\" loading=\"lazy\"><\/p>\n<h3>content script<\/h3>\n<p>\u5185\u5bb9\u811a\u672c\u8bfb\u53d6\u548c\u4fee\u6539\u7f51\u9875\u3002\u5b83\u4eec\u662f\u7528 <code>Javascript<\/code> \u7f16\u5199\u7684\uff0c\u5e76\u5728\u7f51\u9875\u52a0\u8f7d\u65f6\u6267\u884c\u3002\u4f8b\u5982\uff0cMetaDock \u7684\u5185\u5bb9\u811a\u672c\u90e8\u5206\u529f\u80fd\u7528\u4e8e\u66ff\u6362*scan\u9875\u9762\u4e2d\u7684address\u663e\u793a\u6807\u7b7e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>&lt;img src=&quot;https:\/\/picgo-cloudimg.oss-cn-hangzhou.aliyuncs.com\/img\/202211190931705.png&quot; alt=&quot;image-20221119093113672&quot; style=&quot;zoom:50%;&quot; \/&gt;<\/p>\n<p><code>&quot;run_at&quot;<\/code> \u7528\u4e8e\u914d\u7f6e\u811a\u672c\u6267\u884c\u65f6\u673a\uff0c\u9ed8\u8ba4\u662f <code>document_idle<\/code>\uff0c\u6b64\u65f6 DOM \u5df2\u7ecf\u6302\u8f7d\u5b8c\u6210\u3002\u53e6\u5916\u4e24\u4e2a\u53ef\u914d\u7f6e\u9009\u9879\u503c\u662f <code>document_start<\/code> \uff08dom\u6302\u8f7d\u524d\uff09\u548c <code>document_end<\/code> \uff08dom\u6302\u8f7d\u5b8c\u6210\u540e\u7acb\u9a6c\u6267\u884c\uff0c\u6b64\u65f6\u5176\u4ed6\u56fe\u50cf\u548c\u6846\u67b6\u7b49\u5b50\u8d44\u6e90\u53ef\u80fd\u5e76\u6ca1\u6709\u52a0\u8f7d\u5b8c\u6210\uff09\u3002<\/p>\n<p><code>&quot;all_frames&quot;  <\/code> \u5b57\u6bb5\u5141\u8bb8\u6269\u5c55\u6307\u5b9a\u662f\u5426\u5e94\u5c06 JavaScript \u548c CSS \u6587\u4ef6\u6ce8\u5165\u5230\u7b26\u5408\u6307\u5b9a URL \u8981\u6c42\u7684\u6240\u6709\u6846\u67b6\u4e2d\uff0c\u8fd8\u662f\u4ec5\u6ce8\u5165\u5230\u9009\u9879\u5361\u4e2d\u6700\u9876\u5c42\u7684\u6846\u67b6\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u6709\u5185\u5bb9\u811a\u672c\u4e0e\u5bbf\u4e3b\u9875\u9762\u7684\u901a\u4fe1\u9700\u6c42\u8bf7\u4f7f\u7528 <a href=\"https:\/\/link.segmentfault.com\/?enc=wiSxQ8CFxNHKuH5lxRHYDQ%3D%3D.Gu78Oj3sUy5wmYt9T1ZYR0r6sf4y5%2FwE68l8hF7zyLzo0gBzlWuOxvgMPPRZBxvkqh57KcJr6vmStvPde54nGg%3D%3D\" rel=\"nofollow\"><code>window.postMessage<\/code><\/a>\u3002<\/p>\n<pre><code class=\"javascript\">const port = chrome.runtime.connect();\n\nwindow.addEventListener(&quot;message&quot;, (event) =&gt; {\n  \/\/ We only accept messages from ourselves\n  if (event.source != window) {\n    return;\n  }\n\n  if (event.data.type &amp;&amp; (event.data.type == &quot;FROM_PAGE&quot;)) {\n    console.log(&quot;Content script received: &quot; + event.data.text);\n    port.postMessage(event.data.text);\n  }\n}, false);<\/code><\/pre>\n<pre><code class=\"javascript\">document.getElementById(&quot;theButton&quot;).addEventListener(&quot;click&quot;, () =&gt; {\n  window.postMessage({ type: &quot;FROM_PAGE&quot;, text: &quot;Hello from the webpage!&quot; }, &quot;*&quot;);\n}, false);<\/code><\/pre>\n<h3>options<\/h3>\n<p>\u914d\u7f6e\u9875\u9762\uff0c\u5728\u6e05\u5355\u4e2d\u914d\u7f6e\u6b64\u9879\u4f1a\u5728\u63d2\u4ef6\u90ae\u4ef6\u83dc\u5355\u4e2d\u591a\u4e00\u4e2a\u9009\u9879\u7684 item\uff0coptions \u9875\u9762\u4e0e popup \u5f00\u53d1\u6a21\u5f0f\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u4e0d\u591a\u505a\u4ecb\u7ecd\u3002<\/p>\n<pre><code class=\"json\">{\n  &quot;name&quot;: &quot;My extension&quot;,\n  ...\n  &quot;options_ui&quot;: {\n    &quot;page&quot;: &quot;options.html&quot;,\n    &quot;open_in_tab&quot;: false\n  },\n  ...\n}<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851133\" alt=\"\u5d4c\u5165\u5f0f\u9009\u9879\" title=\"\u5d4c\u5165\u5f0f\u9009\u9879\" loading=\"lazy\"><\/p>\n<h3>\u5176\u4ed6\u4e1a\u52a1\u9875\u9762<\/h3>\n<p>\u63d2\u4ef6\u53ef\u4ee5\u6709\u81ea\u5df1\u57df\u4e0b\u7684\u9875\u9762\uff0c\u5f00\u53d1\u8fd9\u4e9b\u9875\u9762\u8ddf\u5f00\u53d1\u591a\u9875\u5e94\u7528\u7684\u6d41\u7a0b\u4e00\u81f4\uff0c\u6bd4\u5982\u4f60\u8981\u5728\u63d2\u4ef6\u4e2d\u65b0\u589e\u4e00\u4e2a\u9690\u79c1\u7b56\u7565\u9875\u9762\uff0c\u76ee\u5f55\u7ed3\u6784\u5e94\u8be5\u7c7b\u4f3c\u4ee5\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851134\" alt=\"image-20221119095351484\" title=\"image-20221119095351484\" loading=\"lazy\"><\/p>\n<p>\u914d\u7f6e <code>entry<\/code> \u548c <code>output<\/code>\uff0c\u4ee5\u4e0b\u4ee5 <code>vite<\/code> \u7684 <code>rollupOptions<\/code> \u4e3a\u4f8b\u3002<\/p>\n<pre><code class=\"json\">rollupOptions: {\n  input: {\n    policy: 'src\/pages\/PrivacyPolicy\/index.html'\n  }\n}<\/code><\/pre>\n<p>\u901a\u8fc7 <code>chrome-extension:\/\/fkhgpeojcbhixxxxxliepkpcgcoo\/src\/pages\/PrivacyPolicy\/index.html<\/code> \u6253\u5f00\u6b64\u9875\u9762\u3002<\/p>\n<p>\u4e0b\u56fe\u6458\u81ea<a href=\"https:\/\/link.segmentfault.com\/?enc=18lIhY40xsKkxMVzcKL7jA%3D%3D.3h0eS2CJIvKhwoAOTlwuQx1hD1UlTKq0rfi0g1eObg6U96pZqMWQSZ82qs5wU732fQ%2F4ID%2BWrN9rbXy%2Bng5PMAUWn4c%2FaWg8%2Flgxy1O8JAU%3D\" rel=\"nofollow\">Google \u7684\u6307\u5357<\/a>\uff0c\u8bf4\u660e\u4e86\u5404\u79cd\u6587\u4ef6\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u662f\u6587\u4ef6\u4e4b\u95f4\u901a\u4fe1\u7684\u5173\u952e\u65b9\u6cd5\uff0c\u7528\u4e8e\u534f\u8c03\u6574\u4e2a\u6269\u5c55\u7684\u529f\u80fd\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851135\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>Chrome API<\/h2>\n<h3>Chrome.runtime<\/h3>\n<ol>\n<li><strong>chrome.runtime.sendMessage<\/strong>\uff1a\u5b83\u5141\u8bb8\u60a8\u5411\u4e8b\u4ef6\u4fa6\u542c\u5668\u53d1\u9001\u4e00\u6761\u6d88\u606f\uff0c\u5141\u8bb8\u4e0d\u540c\u811a\u672c\u4e4b\u95f4\u7684\u4ea4\u4e92\uff08\u65e0\u6cd5\u53d1\u9001\u5230\u5185\u5bb9\u811a\u672c\uff09<\/li>\n<li><strong>chrome.runtime.onMessage.addListener<\/strong>\uff1a\u76d1\u542c\u5e76\u5728\u4ece\u6269\u5c55\u8fdb\u7a0b\/\u53e6\u4e00\u4e2a\u811a\u672c\u63a5\u6536\u5230\u6d88\u606f\u65f6\u89e6\u53d1<\/li>\n<li><strong>chrome.runtime.getURL<\/strong>\uff1a\u83b7\u53d6\u63d2\u4ef6\u7684\u8d44\u6e90\u8def\u5f84\uff0c\u4e00\u822c\u8def\u5f84\u5f80\u5f80\u662f\u4ee5 <code>chrome-extension:\/\/fkhgpeojcbhixxxxxliepkpcgcoo<\/code> \u5f00\u5934\uff0c<code>fkhgpeojcbhixxxxxliepkpcgcoo<\/code> \u662f\u63d2\u4ef6ID\uff0c\u8fd9\u4e2a\u4e00\u822c\u4e0d\u4f1a\u53d8\uff0c\u5728\u5e94\u7528\u4e2d\u4f60\u4e0d\u7528\u53bb\u7ef4\u62a4\u8fd9\u4e2aID\u5373\u53ef\u83b7\u5f97\u8d44\u6e90\u7684\u5b8c\u6574\u8def\u5f84\u3002\u4e00\u822c\u83b7\u53d6\u56fe\u7247\u7b49\u8d44\u6e90\u65f6\u53ef\u4ee5\u7528<code>chrome.runtime.getURL('\/src\/assets\/images\/logo.png')<\/code><\/li>\n<li><strong>chrome.runtime.openOptionsPage<\/strong>\uff1a\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u63d0\u4f9b\u9009\u9879\u9875\u9762\u6765\u81ea\u5b9a\u4e49\u6269\u5c55\u7684\u884c\u4e3a\u3002\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u53f3\u952e\u5355\u51fb\u5de5\u5177\u680f\u4e2d\u7684\u6269\u5c55\u56fe\u6807\u7136\u540e\u9009\u62e9\u9009\u9879\u6216\u5bfc\u822a\u5230\u6269\u5c55\u7ba1\u7406\u9875\u9762<code>chrome:\/\/extensions<\/code>\uff0c\u627e\u5230\u6240\u9700\u7684\u6269\u5c55\uff0c\u5355\u51fb<strong>\u8be6\u7ec6\u4fe1\u606f<\/strong>\uff0c\u7136\u540e\u9009\u62e9\u9009\u9879\u94fe\u63a5\u6765\u67e5\u770b\u6269\u5c55\u7684\u9009\u9879\u3002<\/li>\n<\/ol>\n<h3>Chrome.tabs<\/h3>\n<p>\u5982\u679c\u60a8\u7684\u6269\u5c55\u7a0b\u5e8f\u4e0e\u6d4f\u89c8\u5668\u9009\u9879\u5361\u6709\u5173\uff0c\u5219\u60a8\u9700\u8981\u6b64 API\u3002<\/p>\n<ol>\n<li><strong>chrome.tabs.get<\/strong>\uff1a\u83b7\u53d6\u6709\u5173\u4efb\u4f55\u6307\u5b9a\u9009\u9879\u5361\u7684\u8be6\u7ec6\u4fe1\u606f\uff08\u4f8b\u5982 URL\u3001\u6807\u9898\u3001ID\u3001\u662f\u5426\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\uff09\u3002\u5982\u679c\u60a8\u53ea\u60f3\u5728\u7528\u6237\u8bbf\u95ee\u67d0\u4e9b\u7f51\u7ad9\u65f6\u89e6\u53d1\u64cd\u4f5c\uff08\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u6269\u5c55\u7a0b\u5e8f\u662f\u7279\u5b9a\u4e8e\u7f51\u7ad9\u7684\uff09\uff0c\u8fd9\u5c06\u5f88\u6709\u7528\u3002<\/li>\n<li><strong>chrome.tabs.getCurrent<\/strong> : \u83b7\u53d6\u5f53\u524d\u6807\u7b7e\u7684\u8be6\u7ec6\u4fe1\u606f<\/li>\n<li><strong>chrome.tabs.sendMessage<\/strong>\uff1a\u5c06\u6d88\u606f\u53d1\u9001\u5230\u6307\u5b9a\u9009\u9879\u5361\u7684\u5185\u5bb9\u811a\u672c\uff0c\u5e76\u5728\u53d1\u9001\u56de\u54cd\u5e94\u65f6\u8fd0\u884c\u53ef\u9009\u7684\u56de\u8c03<\/li>\n<li><strong>chrome.tabs.create\uff1a<\/strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u6807\u7b7e\u9875\uff08\u4f60\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a URL\uff09<\/li>\n<li><strong>chrome.tabs.reload<\/strong>\uff1a\u5237\u65b0\u9009\u9879\u5361\u9875\u9762<\/li>\n<li><strong>chrome.tabs.query<\/strong>\uff1a\u83b7\u53d6\u9009\u9879\u5361\u76f8\u5173<\/li>\n<\/ol>\n<h3>chrome.webRequest<\/h3>\n<p>\u4f7f\u7528<code>chrome.webRequest<\/code>API \u89c2\u5bdf\u548c\u5206\u6790\u6d41\u91cf\u5e76\u62e6\u622a\u3001\u963b\u6b62\u6216\u4fee\u6539\u8fd0\u884c\u4e2d\u7684\u8bf7\u6c42\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851136\" alt=\"\u4ecewebrequest API\u770b\u4e00\u4e2aweb\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\" title=\"\u4ecewebrequest API\u770b\u4e00\u4e2aweb\u8bf7\u6c42\u7684\u751f\u547d\u5468\u671f\" loading=\"lazy\"><\/p>\n<p>\u751f\u547d\u5468\u671f\u4e2d\u6bcf\u4e2a\u52fe\u5b50\u90fd\u80fd\u88ab\u76d1\u542c\u5230\u3002\u66f4\u591a\u7ec6\u8282\u8bf7<a href=\"https:\/\/link.segmentfault.com\/?enc=skIuYKGl3Js1JCkaDxiS8g%3D%3D.hoxyY0UFKBer%2Bf99F0ixWlBU6I3qn7dAnYtu5ncoXnTg%2FzlRcfLxFNxMQojaLx%2FjH4J7aEbu8Z5YNMAqNSOqlF4uSyCAjpuBgZputNs%2BzdQ%3D\" rel=\"nofollow\">\u53c2\u8003\u6587\u6863<\/a>\u3002<\/p>\n<h2>\u5f00\u53d1\u8c03\u8bd5<\/h2>\n<p>\u63d2\u4ef6\u4e2d\u7684\u9875\u9762\u5982 <code>popup<\/code>\u3001<code>options<\/code> \uff0c\u8fd8\u6709\u540e\u53f0\u811a\u672c\u7b49\u8c03\u8bd5\u5de5\u5177\u662f\u8ddf\u4f60\u5f53\u524d\u6d4f\u89c8\u5668\u7684\u8c03\u8bd5\u5de5\u5177\uff08F12\uff09\u72ec\u7acb\u7684\uff0c\u5982\u679c\u9700\u8981\u8c03\u8bd5\u5143\u7d20\u548c\u7f51\u7edc\u8bf7\u6c42\u7b49\u63a7\u5236\u53f0\u4fe1\u606f\uff0c\u8bf7\u5728 <code>popup<\/code> \u9762\u677f\u4e0a\u53f3\u952e\u68c0\u67e5\uff0c\u6ce8\u610f\uff1a\u6bcf\u91cd\u65b0\u5f00\u4e00\u4e2a<code> tab<\/code> \u90fd\u9700\u8981\u91cd\u65b0\u6267\u884c\u4e0a\u8ff0\u6b65\u9aa4\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851137\" alt=\"image-20221119101926148\" title=\"image-20221119101926148\" loading=\"lazy\"><\/p>\n<p>\u5173\u4e8e <code>content script<\/code> \u70ed\u66f4\u65b0\u8c03\u8bd5\u95ee\u9898\u6574\u7406\u4e86\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6848\uff0c\u90fd\u6709\u5c1d\u8bd5\uff0c\u63a8\u8350\u7b2c\u4e8c\u79cd\u65b9\u6848\u3002<\/p>\n<ol>\n<li>\n<p><code>webpack<\/code> \u7248\u672c\u89e3\u51b3\u65b9\u6848\uff08\u590d\u6742\u63d2\u4ef6\u5f00\u53d1\u8fc7\u7a0b\u4e0d\u7a33\u5b9a\uff0c\u5ef6\u8fdf\u9ad8\uff09<\/p>\n<ul>\n<li>\u914d\u7f6e webpack server\uff0c\u5c06 bundle \u5199\u5230\u78c1\u76d8\u3002<\/li>\n<li>\u901a\u8fc7 webpack plugin \u66b4\u9732 compiler \u5bf9\u8c61\u3002<\/li>\n<li>\u4e3a webpack server \u589e\u52a0\u4e2d\u95f4\u4ef6\uff0c\u62e6\u622a reload \u8bf7\u6c42\uff0c\u8f6c\u5316\u4e3a SSE\uff0ccompiler \u6ce8\u518c\u7f16\u8bd1\u5b8c\u6210\u7684\u94a9\u5b50\uff0c\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u901a\u8fc7 SSE \u53d1\u9001\u6d88\u606f\u3002<\/li>\n<li>chrome extension \u542f\u52a8\u540e\uff0cbackground \u4e0e webpack server \u5efa\u7acb\u8fde\u63a5\uff0c\u76d1\u542c reload \u65b9\u6cd5\uff0c\u6536\u5230 server \u7684\u901a\u77e5\u540e\uff0c\u6267\u884c chrome \u672c\u8eab\u7684 reload \u65b9\u6cd5\uff0c\u5b8c\u6210\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<\/li>\n<li><code>CRXJS Vite<\/code> \u63d2\u4ef6\uff08\u63a8\u8350\uff09<\/li>\n<\/ol>\n<h2>CRXJS Vite \u63d2\u4ef6\u4f7f\u7528\u6280\u5de7<\/h2>\n<h3>\u989d\u5916\u7684 HTML \u9875\u9762<\/h3>\n<p>\u6269\u5c55\u7a0b\u5e8f\u5305\u542b\u60a8\u65e0\u6cd5\u5728\u6e05\u5355\u4e2d\u58f0\u660e\u7684\u7f51\u9875\u662f\u5f88\u5e38\u89c1\u7684\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u5728\u7528\u6237\u767b\u5f55\u540e\u66f4\u6539\u5f39\u51fa\u7a97\u53e3\u6216\u5728\u7528\u6237\u5b89\u88c5\u6269\u5c55\u7a0b\u5e8f\u65f6\u6253\u5f00\u6b22\u8fce\u9875\u9762\u3002\u6b64\u5916\uff0c\u50cf React Developer Tools \u8fd9\u6837\u7684\u5f00\u53d1\u5de5\u5177\u6269\u5c55\u4e0d\u4f1a\u5728\u6e05\u5355\u4e2d\u58f0\u660e\u5b83\u4eec\u7684\u68c0\u67e5\u5668\u9762\u677f\u3002<\/p>\n<p>\u7ed9\u5b9a\u4ee5\u4e0b\u6587\u4ef6\u7ed3\u6784\u548c\u6e05\u5355\uff0c<code>index.html<\/code>\u5e76\u5c06<code>src\/panel.html<\/code>\u5728\u5f00\u53d1\u671f\u95f4\u53ef\u7528\uff0c\u4f46\u5728\u751f\u4ea7\u6784\u5efa\u4e2d\u4e0d\u53ef\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5728<code>vite.config.ts<\/code><\/p>\n<pre><code>.\n\u251c\u2500\u2500 vite.config.ts\n\u251c\u2500\u2500 manifest.json\n\u251c\u2500\u2500 index.html\n\u2514\u2500\u2500 src\/\n    \u251c\u2500\u2500 devtools.html\n    \u2514\u2500\u2500 panel.html<\/code><\/pre>\n<pre><code class=\"json\">\/\/ manifest.json\n{\n  &quot;manifest_version&quot;: 3,\n  &quot;version&quot;: &quot;1.0.0&quot;,\n  &quot;name&quot;: &quot;example&quot;,\n  &quot;devtools_page&quot;: &quot;src\/devtools.html&quot;\n}<\/code><\/pre>\n<pre><code class=\"javascript\">\/\/ vite.config.js\nimport { resolve } from 'path';\nimport { defineConfig } from 'vite';\nimport { crx } from '@crxjs\/vite-plugin';\nimport manifest from '.\/manifest.json';\n\nexport default defineConfig({\n  build: {\n    rollupOptions: {\n      \/\/ add any html pages here\n      input: {\n        \/\/ output file at '\/index.html'\n        welcome: resolve(__dirname, 'index.html'),\n        \/\/ output file at '\/src\/panel.html'\n        panel: resolve(__dirname, 'src\/panel.html'),\n      },\n    },\n  },\n  plugins: [crx({ manifest })],  \n});<\/code><\/pre>\n<h3>\u4f7f\u7528\u52a8\u6001\u6e05\u5355\u6587\u4ef6<\/h3>\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u4f55\u5c06 <code>manifest.json<\/code> \u4e2d\u7684\u7248\u672c\u53f7\u8ddf <code>package.json<\/code> \u4e2d\u7684\u7248\u672c\u7edf\u4e00\uff1f\ud83e\udd14<\/p>\n<p>Vite \u63d2\u4ef6\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>defineManifest<\/code>\u4e0e Vite \u529f\u80fd\u7c7b\u4f3c\u7684<code>defineConfig<\/code>\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e86 <code>IntelliSense<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u6784\u5efa\u65f6\u6269\u5c55\u4f60\u7684\u6e05\u5355\u3002<\/p>\n<pre><code class=\"typescript\">\/\/ manifest.config.ts\n\nimport { defineManifest } from '@crxjs\/vite-plugin'\nimport { version } from '.\/package.json'\n\nconst names = {\n  build: 'My Extension',\n  serve: '[INTERNAL] My Extension'\n}\n\n\/\/ import to `vite.config.ts`\nexport default defineManifest((config, env) =&gt; ({\n  manifest_version: 3,\n  name: names[env.command],\n  version,\n}))<\/code><\/pre>\n<h3>\u56fe\u6807\u548c\u516c\u5171\u8d44\u6e90<\/h3>\n<p>\u4f60\u53ef\u4ee5\u53c2\u8003\u6e05\u5355\u4e2d\u7684\u516c\u5171\u6587\u4ef6\u3002\u5982\u679c <code>CRXJS<\/code> \u5728\u5176\u4e2d\u6ca1\u6709\u627e\u5230\u5339\u914d\u7684\u6587\u4ef6\uff0c\u5b83\u5c06\u67e5\u627e\u76f8\u5bf9\u4e8e<a href=\"https:\/\/link.segmentfault.com\/?enc=lhMd%2Fj0yG%2FX6XWGM5lFkVg%3D%3D.RXFNLoAAoTb0CEcss0Ice5i7K66tKNlKvMVhZU6ZY24%3D\" rel=\"nofollow\">Vite \u9879\u76ee\u6839\u76ee\u5f55<\/a>\u7684\u6587\u4ef6\u5e76\u5c06\u8d44\u4ea7\u6dfb\u52a0\u5230\u8f93\u51fa\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u9759\u6001\u8d44\u6e90\u7edf\u4e00\u653e\u7f6e\u5728 <code>public<\/code> \u76ee\u5f55\u4e2d\u7ba1\u7406\u3002<\/p>\n<h3>Web Accessible Resources<\/h3>\n<p>\u4f60\u6bcf\u6b21\u4f7f\u7528\u4e00\u4e2a\u56fe\u7247\u90fd\u8981\u624b\u52a8\u53bb\u66f4\u65b0\u5230\u6e05\u5355\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u80fd\u4f1a\u8ba9\u6211\u4eec\u89c9\u5f97\u6bd4\u8f83\u7e41\u7410\u3002\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u6784\u5efa\u5de5\u5177\uff0c\u90a3\u4e48\u4e3a\u4ec0\u4e48\u8981\u505a\u4e0d\u5fc5\u8981\u7684\u624b\u52a8\u5de5\u4f5c\u5462\uff1f\u5f53\u4f60\u5c06\u56fe\u50cf\u5bfc\u5165\u5185\u5bb9\u811a\u672c\u65f6\uff0c<code>CRXJS<\/code> \u4f1a\u81ea\u52a8\u66f4\u65b0\u6e05\u5355\u3002\u2728<\/p>\n<pre><code class=\"javascript\">import logoPath from '.\/logo.png'\n\nconst logo = document.createElement('img')\nlogo.src = chrome.runtime.getURL(logo)<\/code><\/pre>\n<h3>\u52a8\u6001\u5185\u5bb9\u811a\u672c<\/h3>\n<blockquote><p>\u4eb2\u6d4b\u8def\u5f84\u89e3\u6790\u6ca1\u95ee\u9898\uff0c\u4f46\u662f\u6211\u53bb <code>executeScript<\/code> \u6b21\u8def\u5f84\u7684\u811a\u672c\u65f6\u6ca1\u6709\u53cd\u5e94\uff0c\u4e5f\u8bb8\u662f\u7248\u672cbug\uff0c\u4e5f\u8bb8\u662f\u6211\u4f7f\u7528\u6709\u8bef\uff0c\u8bf7\u81ea\u884c\u5224\u65ad<\/p><\/blockquote>\n<p>\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e2a\u573a\u666f\uff1a\u6bd4\u5982\u4f60\u5728 <code>background.js<\/code> \u76d1\u542c\u67d0\u4e2a\u7f51\u7edc\u8bf7\u6c42\uff0c\u547d\u4e2d\u540e\u60f3\u6267\u884c\u4e00\u904d\u4f60\u7684\u67d0\u4e2a <code>content script<\/code>\uff0c\u8fd9\u65f6\u4f60\u9700\u8981\u5728 <code>executeScript<\/code> \u4e2d\u5199\u811a\u672c\u7684\u8def\u5f84\u624d\u80fd\u6b63\u786e\u6267\u884c\uff0c\u4f46\u662f\u4f60\u5e76\u4e0d\u77e5\u9053\u6253\u5305\u540e\u7684\u811a\u672c\u8def\u5f84\uff08\u6253\u5305\u540e\u7684\u8def\u5f84\u4f60\u5e76\u4e0d\u5173\u5fc3\u6216\u8005\u6587\u4ef6\u540d\u662f\u7ecf\u8fc7\u54c8\u5e0c\u5904\u7406\u7684\uff09\uff0c\u8fd9\u65f6\u5019\u52a8\u6001\u5185\u5bb9\u811a\u672c\u5c31\u6d3e\u4e0a\u7528\u573a\u4e86\u3002<\/p>\n<p><code>CRXJS<\/code> \u4f7f\u7528\u552f\u4e00\u7684\u5bfc\u5165\u67e5\u8be2\u6765\u6307\u5b9a\u5bfc\u5165\u6307\u5411\u5185\u5bb9\u811a\u672c\u3002\u5f53\u5bfc\u5165\u540d\u79f0\u4ee5\u67e5\u8be2\u7ed3\u5c3e\u65f6<code>?script<\/code>\uff0c\u9ed8\u8ba4\u5bfc\u51fa\u662f\u5185\u5bb9\u811a\u672c\u7684\u8f93\u51fa\u6587\u4ef6\u540d\u3002<\/p>\n<pre><code class=\"typescript\">import scriptPath from '.\/content-script?script'\n\nchrome.action.onClicked.addListener((tab) =&gt; {  \n  chrome.scripting.executeScript({\n    target: { tabId: tab.id },\n    files: [scriptPath]\n  });\n});<\/code><\/pre>\n<h2>\u4f7f\u7528\u6280\u5de7<\/h2>\n<ol>\n<li>\u63a8\u8350\u4f7f\u7528 <code>fetch<\/code> \u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528<a href=\"https:\/\/link.segmentfault.com\/?enc=PlIX7r1ofDhf3y7EPVkEGQ%3D%3D.af1%2BKDCZq%2FF8s%2FZwy3VeO75CFjOy7yunbxIBJKSgbza8V1VJ%2B5t8Wdo2KZER6QD2\" rel=\"nofollow\">ky<\/a>\u5305\u51cf\u5c11\u8d1f\u62c5\uff0c\u53e6\u5916 <code>axios<\/code> \u6211\u8bb0\u5f97\u5728\u63d2\u4ef6\u5f00\u53d1\u4e2d\u6709\u989d\u5916\u7684\u9700\u8981\u914d\u7f6e\u6216\u8005\u5751\u3002<\/li>\n<li>\u6240\u6709\u65f6\u95f4\u76d1\u542c\u5305\u62ec\u8bf7\u6c42\u63a8\u8350\u5728 <code>background.js<\/code> \u4e2d\u7edf\u4e00\u5904\u7406\uff0c\u4f7f\u7528\u6d88\u606f\u901a\u4fe1\u4f20\u9012\u7ed3\u679c\u5230\u5176\u4ed6\u811a\u672c\/\u9875\u9762\u4e2d\u3002<\/li>\n<li>\n<p>\u6d88\u606f\u4f20\u9012\u5982\u679c\u662f\u5176\u4ed6 -&gt; <code>background.js<\/code> \u53ef\u4ee5\u4f7f\u7528 <code>chrome-extension-core<\/code> \u7684 <code>Event<\/code> \u8fdb\u884c\u4f20\u9012\uff0c\u53cb\u597d\u7684\u652f\u6301\u4e86 <code>typescript<\/code>\uff0c\u4f7f\u7528\u65b9\u5f0f\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"typescript\">\/\/ common\/event.js\n\/\/ \u5728\u5168\u5c40\u7684event\u6587\u4ef6\u4e2d\u7ba1\u7406\u6240\u6709\u7684\u4e8b\u4ef6\u5b9a\u4e49\uff0c\u5305\u62ec\u53c2\u6570\u7ea6\u675f\nimport { Event } from 'chrome-extension-core'\n\nimport { SCOPE } from '@common\/constants'\nimport type { PostXXXParams } from '@common\/api\/types'\nimport type { REFRESH, GET_XXX } from '@common\/constants\/event'\n\ntype EventInfo = {\n  [REFRESH]: boolean\n  [GET_XXX]: PostXXXParams\n}\n\nexport const chromeEvent = new Event&lt;EventInfo&gt;(SCOPE)<\/code><\/pre>\n<pre><code class=\"typescript\">\/\/ content script\nconst res = await chromeEvent.emit&lt;typeof GET_XXXX, Response&gt;(\n  GET_XXX,\n  {\n    name: 'ghostwang'\n  }\n)<\/code><\/pre>\n<pre><code class=\"typescript\">\/\/ background.js\n\nchromeEvent.on(GET_XXX, async params =&gt; {\n  try {\n    const { success, data, msg } = await commonApi.getXxx(params)\n    return {\n      success: success,\n      data,\n      message: msg\n    }\n  } catch (error) {\n    return { success: false, data: error, message: 'error' }\n  }\n})<\/code><\/pre>\n<\/li>\n<li>\n<p><code>background.js<\/code> \u4e3b\u52a8\u7ed9\u5176\u4ed6\u53d1\u6d88\u606f\uff0c\u6ce8\u610f\u5fc5\u987b\u8981\u5904\u7406\u5f02\u6b65\uff0c\u4e0d\u7136\u4f1a\u5728\u63d2\u4ef6\u9762\u677f\u62a5\u9519\uff08\u867d\u7136\u8fd9\u4e2a\u9519\u8bef\u53ef\u80fd\u4e0d\u5f71\u54cd\u4f60\u7684\u529f\u80fd\uff09<\/p>\n<pre><code class=\"typescript\">\/\/ \u540e\u53f0\u811a\u672c\u53d1\u9001\u903b\u8f91\nchrome.tabs.sendMessage(tabId, EXECUTE_XXX_SCRIPT, function () {\n  \/** \u6ce8\u610f\uff1a\u4ee5\u4e0b\u4ee3\u7801\u4e0d\u8981\u5220 *\/\n  if (!chrome.runtime.lastError) {\n    \/\/ \u5982\u679c\u4f60\u6709\u4efb\u4f55\u56de\u5e94\n  }\n})\n\n\/\/ \u5185\u5bb9\u811a\u672c\u76d1\u542c\u903b\u8f91\nchrome.runtime.onMessage.addListener((message, _sender, sendResponse) =&gt; {\n  if (message === EXECUTE_XXX_SCRIPT) {\n    run()\n    sendResponse()\n  }\n})<\/code><\/pre>\n<\/li>\n<li>\n<p>\u6301\u4e45\u5316\u5b58\u50a8\u53ef\u4ee5\u5c01\u88c5\u81ea\u5b9a\u4e49\u7684 <code>hooks<\/code> \u5e76\u5b9e\u73b0 \u76d1\u542c\u903b\u8f91\u3002<\/p>\n<pre><code class=\"typescript\">import { useCallback, useEffect, useState } from 'react'\n\nimport { store, defaultValue, type StorageInfo } from '@src\/store'\n\n\/**\n * \u6301\u4e45\u5316\u5b58\u50a8store\n *\/\nexport default function useStore&lt;Key extends keyof StorageInfo&gt;(\n  key: Key\n): [StorageInfo[Key], (newValue: StorageInfo[Key]) =&gt; Promise&lt;void&gt;] {\n  const [value, setValue] = useState&lt;StorageInfo[Key]&gt;(defaultValue[key])\n  useEffect(() =&gt; {\n    const getStore = async () =&gt; {\n      const currentStoreValue = await store.get(key)\n      setValue(currentStoreValue)\n    }\n    const storeWatcher = (\n      data: Record&lt;keyof StorageInfo, chrome.storage.StorageChange&gt;\n    ) =&gt; {\n      if (data[key]) {\n        const changedData = data[key]\n        setValue(changedData.newValue)\n      }\n    }\n    store.addWatcher(storeWatcher)\n    getStore()\n    return () =&gt; {\n      store.removeWatcher(storeWatcher)\n    }\n  }, [key])\n\n  const setStore = useCallback(\n    (newValue: StorageInfo[Key]) =&gt; {\n      return store.set(key, newValue)\n    },\n    [key]\n  )\n\n  return [value, setStore]\n}<\/code><\/pre>\n<pre><code class=\"typescript\">import type { WatcherCallback } from 'chrome-extension-core'\nimport { useEffect } from 'react'\n\nimport type { StorageInfo } from '@src\/store'\nimport { store } from '@src\/store'\n\nexport default function useStoreWatcher(\n  callback: WatcherCallback&lt;StorageInfo&gt;,\n  deps?: (keyof StorageInfo)[]\n) {\n  useEffect(() =&gt; {\n    const storeWatcher = (\n      data: Record&lt;keyof StorageInfo, chrome.storage.StorageChange&gt;\n    ) =&gt; {\n      const dataKeys = Object.keys(data)\n      if (!deps || deps.some(val =&gt; dataKeys.includes(val))) {\n        callback(data)\n      }\n    }\n    store.addWatcher(storeWatcher)\n    return () =&gt; {\n      store.removeWatcher(storeWatcher)\n    }\n  }, [callback, deps])\n}<\/code><\/pre>\n<\/li>\n<li>\u5185\u5bb9\u811a\u672c\u52a0\u8f7d\u5b57\u4f53\u7b49\u8d44\u6e90\u6bd4\u8f83\u8017\u65f6\uff0c\u56e0\u4e3a\u672c\u8eab\u6267\u884c\u65f6\u673a\u53ef\u80fd\u5c31\u662f\u5728\u5bbf\u4e3b\u7f51\u9875\u8d44\u6e90\u52a0\u8f7d\u540e\uff0c\u6240\u4ee5\u5c3d\u91cf\u907f\u514d\u7528 <code>iconfont<\/code> \uff0c\u4f7f\u7528\u5c0f\u56fe\u7247\u66ff\u4ee3\u4f1a\u66f4\u5feb\u3002<\/li>\n<li>\n<p>\u63d2\u4ef6\u7684\u672c\u5730\u5316\u65e0\u6cd5\u63d0\u4f9b\u52a8\u6001\u5207\u6362\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u5982\u679c\u8981\u505a\u56fd\u9645\u5316\uff0c\u8fd8\u5f97\u5199\u4e24\u5957\uff0c\u63d2\u4ef6\u652f\u6301\u7684\u56fd\u9645\u5316\u529f\u80fd\u9002\u7528\u4e8e\u63d2\u4ef6\u5e02\u573a\u5c55\u793a\u4f60\u7684\u63d2\u4ef6\u5e94\u7528\u7684\u540d\u79f0\u548c\u63cf\u8ff0\u4fe1\u606f\uff0c<code>popup<\/code> \u7b49\u9875\u9762\u9700\u8981\u901a\u8fc7 <code>i18next<\/code> \u5b9e\u73b0\u52a8\u6001\u5207\u6362\u8bed\u8a00\uff0c\u6240\u4ee5\uff0c\u4f60\u7684\u76ee\u5f55\u7ed3\u6784\u5e94\u8be5\u7c7b\u4f3c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042851138\" alt=\"image-20221119113155558\" title=\"image-20221119113155558\" loading=\"lazy\"><\/p>\n<\/li>\n<\/ol>\n<p>Chrome \u5185\u7f6e\u7684\u56fd\u9645\u5316\u76ee\u5f55\u5fc5\u987b\u5728\u6839\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u914d\u5408 <code>vite<\/code> \u7684 <code>vite-plugin-files-copy<\/code> \u63d2\u4ef6\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"javascript\">CopyPlugin({\n  patterns: [\n    {\n      from: '.\/src\/_locales\/chrome',\n      to: 'dist\/_locales'\n    }\n  ]\n})<\/code><\/pre>\n<h2>\u5199\u5728\u6700\u540e<\/h2>\n<p>\u63a8\u8350\u4e00\u4e2a\u4e13\u95e8\u4e3a Web3 \u7528\u6237\u670d\u52a1\u7684 Chorme \u6d4f\u89c8\u5668\u63d2\u4ef6 \u2014\u2014 <a href=\"https:\/\/link.segmentfault.com\/?enc=SVRXJfrUq6iIjXnePtD1mA%3D%3D.beddwUHQHiv3Ir%2Fv%2FxuaOIKF6BPwytsj%2Fk0XXlQRiOnzIuTn1J690lzi8pYXU3LiR4bfooN8G6FmugWn0lJMXxvDRwnmULlnMb1geRaZJlUgqZdqo0MPN%2BLK7cq1cH3a\" rel=\"nofollow\"><strong>MetaDock<\/strong><\/a><\/p>\n<p>\u4ea7\u54c1\u529f\u80fd\uff1a<br \/>\u5b89\u88c5\u63d2\u4ef6\u540e\uff0c\u5728\u5927\u5bb6\u8bbf\u95ee <code>Etherscan<\/code>\u3001<code>BscScan<\/code>\u3001<code>BTC.com<\/code> \u7b49\u533a\u5757\u94fe\u6d4f\u89c8\u5668\u65f6\uff0c\u63d0\u4f9b\u591a\u9879\u589e\u5f3a\u529f\u80fd\uff0c\u76f4\u63a5\u5c55\u793a\u5728\u9875\u9762\u4e2d\u3002\u76ee\u524d\u5df2\u5b9e\u73b0\u5730\u5740\u6807\u7b7e\u3001\u98ce\u9669\u8bc4\u5206\uff0c\u8d44\u91d1\u6d41\u5411\u56fe\uff0c\u5408\u7ea6\u4ee3\u7801\u4e0b\u8f7d\uff0c\u5feb\u901f\u5728 <a href=\"https:\/\/link.segmentfault.com\/?enc=7a7QMdHvEy61XjLE4ixqEA%3D%3D.jGauxcYmx7WGpj45colKCJ4hv%2FRsO2y1sY0FKAyflJs%3D\" rel=\"nofollow\">Phalcon<\/a> \u4e2d\u6253\u5f00\u4ea4\u6613\uff0c\u8df3\u8f6c\u5230\u591a\u6b3e\u533a\u5757\u94fe\u6d4f\u89c8\u5668\u7b49\u529f\u80fd\u3002<\/p>\n<p>\u9690\u79c1\u4fdd\u62a4\uff1a<br \/><code>MetaDock<\/code> \u6709\u4e25\u683c\u9690\u79c1\u4fdd\u62a4\uff0c\u53ea\u4f1a\u5728\u6307\u5b9a\u7684\u533a\u5757\u94fe\u6d4f\u89c8\u5668\u7f51\u7ad9\u4e0a\u8fd0\u884c\uff08\u53ef\u4ee5\u5728\u914d\u7f6e\u4e2d\u5173\u95ed\uff09\uff0c\u4e0d\u4f1a\u5728\u5176\u4ed6\u7f51\u7ad9\u4e0a\u8fd0\u884c\uff0c\u4e0d\u4f1a\u4e0a\u4f20\u81ea\u5b9a\u4e49\u4fe1\u606f\u3002\u8bf7\u5927\u5bb6\u653e\u5fc3\u4f7f\u7528 \ud83d\ude42<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2NA3\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042851130\">https:\/\/segmentfault.com\/a\/1190000042851130<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6269\u5c55\u7a0b\u5e8f\u662f\u57fa\u4e8e\u4e8b\u4ef6\u7684\u7a0b\u5e8f\uff0c\u7528\u4e8e\u4fee\u6539\u6216\u589e\u5f3a Chrome \u6d4f\u89c8\u4f53\u9a8c\uff0c\u5982\u679c\u6b64\u65f6\u4f60\u60f3\u6784\u5efa\u4e00\u4e2a Chrome \u6269\u5c55\u7a0b\u5e8f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006875","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006875"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006875\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}