代码之家  ›  专栏  ›  技术社区  ›  duXing

Chrome开发工具的代码段保存在哪个文件?

  •  13
  • duXing  · 技术社区  · 13 年前

    据我所知,个人数据总是保存在配置文件路径中,可以在chrome://version.

    我在Chrome开发工具中添加了许多片段,并想备份它们。

    但我找不到在该路径下保存的数据片段文件。

    有人知道吗?请告诉我。非常感谢!

    5 回复  |  直到 13 年前
        1
  •  15
  •   Paul Irish    10 年前

    从Chrome47(大约)开始,片段不再通过localStorage提供。此外,natchiketa的答案依赖于localStorage,而且不再有效。

    以下是当前的技术:

    获取片段

    InspectorFrontendHost.getPreferences(_ => console.log(JSON.parse(_.scriptSnippets)))
    

    设置代码段

    InspectorFrontendHost.setPreference("scriptSnippets", JSON.stringify(yourSnippets))
    

    这项技术是从 github thread 讨论这一变化。

        2
  •  8
  •   shmuel friedman Marinos An    5 年前

    最新铬的工作溶液(90.0):

    在我的 ubuntu18 , chromium 代码段保存在以下位置: ~/.config/chromium/Default/Preferences 作为一个单行json文件。

    以上和 Bookmarks 是我通常备份的文件。

    如果解析Preferences json文件,您可以在以下json路径中找到片段:

    ROOT.devtools.preferences.scriptSnippets
    

    该属性的值需要再次解析为json。 然后,您将获得所有片段的{name,content}数组。

    注意:我正在使用 jq-1.6 对于以下命令

    要备份 所有的狙击手 一个json文件:

    jq .devtools.preferences.scriptSnippets ~/.config/chromium/Default/Preferences \
    | jq '. | fromjson' > /tmp/backup.json
    
    

    到 备份所有片段 进入 单独的.js文件

    # Tested with jq-1.6
    # The script converts json entries into lines of format 
    # `filename[TAB]content-in-base64` and then 
    # for each line creates the corresponding file 
    # with the content decoded.
    
    # Better be in a safe directory!!
    mkdir /tmp/snippets-backup
    cd /tmp/snippets-backup
    
    jq .devtools.preferences.scriptSnippets ~/.config/chromium/Default/Preferences \
    | jq '. | fromjson | .[]| [ .name, @base64 "\(.content)" ] | @tsv' -r \
    | xargs -I{} /bin/bash -c 'file=$(echo "{}"|cut -f1); fileContent=$(echo "{}"|cut -f2); echo "$fileContent" | base64 -d > "${file}.js"'
    
    

    关于jq

    jq 是一个很好的命令行工具,用于查询和处理JSON文件。你可以从 stedolan.github.io/jq/ .

        3
  •  3
  •   natchiketa    10 年前

    编辑(2016年1月) 警告 :我只能从最近的否决票中推测,这已经行不通了。我一有机会就会更新。

    TL;博士

    • 代码段存储为sqlite文件。
    • 您可以将它们提取为 [{id: n, name: 'foo.js', content: "The script"}]
    • 操作系统X的说明如下

    在OSX上,假设您安装了sqlite命令行客户端(我已经安装了一个,我不记得明确安装过),您会发现它是这样的:

    #如果你不知道什么是Chrome配置文件,你可能会使用配置文件1,所以: cd~/Library/Application\Support/Google/Chrome/Profile\1/本地\存储

    在这个目录中,应该有一个名为 chrome-devtools_devtools_0.localstorage 如果是这样,您应该能够做到:

    sqlite3 chrome-devtools_devtools_0.localstorage
    

    …并且您应该在sqlite shell中,它的开头是这样的:

    SQLite version 3.7.13 2012-07-17 17:46:21
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite>
    

    你可以 .tables 要查看此文件中的表:

    sqlite> .tables
    ItemTable
    

    然后,您可以执行此操作以查看该表中的列名:

    PRAGMA table_info(ItemTable);
    

    由于localStorage只是一个键/值存储,因此此表非常简单:

    0|key|TEXT|0||0
    1|value|BLOB|1||0
    

    更具启发性的是看到关键名称:

    select key from ItemTable;
    

    你应该看到这样的东西:

    fileSystemMapping
    showAdvancedHeapSnapshotProperties
    lastDockState
    resourcesLargeRows
    networkResourceTypeFilters
    pauseOnCaughtException
    showInheritedComputedStyleProperties
    elementsPanelSplitViewState
    filterBar-networkPanel-toggled
    networkLogColumnsVisibility
    networkPanelSplitViewState
    sourcesPanelDebuggerSidebarSplitViewState
    pauseOnExceptionEnabled
    watchExpressions
    breakpoints
    consoleHistory
    domBreakpoints
    Inspector.drawerSplitViewState
    InspectorView.splitViewState
    WebInspector.Drawer.lastSelectedView
    currentDockState
    editorInDrawerSplitViewState
    experiments
    inspectorVersion
    lastActivePanel
    sourcesPanelSplitViewState
    sourcesPanelNavigatorSplitViewState
    scriptSnippets_lastIdentifier
    previouslyViewedFiles
    revision-history
    revision-history|Script snippet #1|20267.2|1410876616909
    scriptSnippets
    

    片段在 scriptsSnippets 。当我开始这样做的时候,我没有任何片段,所以我没有看到这个密钥。在创建我的第一个片段时,密钥出现了。所以接下来我做了:

    select * from ItemTable where key = 'scriptSnippets';
    

    瞧!

    scriptSnippets|[{"id":"1","name":"SnipFoo.js","content":"/**\n * @license\n * Lo-Dash 2.4.1 (Custom Build) <http://lodash.com/>\n * Build: `lodash modern -o ./dist/lodash.js`\n * Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>\n * Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>\n * Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors\n * Available under MIT license <http://lodash.com/license>\n */\n;(function() {\n\n  /** Used as a safe reference for `undefined` in pre ES5 environments */\n  var undefined;\n\n  /** Used to pool arrays and objects used internally */\n  var arrayPool = [],\n      objectPool = [];\n\n  /** Used to generate unique IDs */\n  var idCounter = 0;\n\n  /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */\n  var keyPrefix = +new Date + '';\n\n  /** Used as the size when optimizations are enabled for large arrays */\n  var largeArraySize = 75;\n\n  /** Used as the max size of the `arrayPool` and `objectPool` */\n  var maxPoolSize = 40;\n\n  /** Used to detect and test whitespace */\n  var whitespace = (\n    // whitespace\n    ' \\t\\x0B\\f\\xA0\\ufeff' +\n\n    // line terminators\n    '\\n\\r\\u2028\\u2029' +\n\n    // unicode category \"Zs\" space separators\n    '\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\
    

    …等等。请注意,您将看到多个版本的代码片段。我刚粘贴 lodash.js 在我上面提到的那个新片段中,我已经有两次了。

    那么,如何真正将脚本从中提取出来呢?好吧,一旦你得到了 value 字段,这只是一个JSON数组,其中有一个片段 id , name 和 content 。因此,创建一个SQL脚本以传递给 sqlite3 ,从命令行,如下所示:

    echo "select value from ItemTable where key = 'scriptSnippets';" > showSnippets.txt
    

    然后像这样输入脚本:

    sqlite3 chrome-devtools_devtools_0.localstorage < showSnippets.txt
    

    如果你这样做,看起来会稍微漂亮一点:

    sqlite3 chrome-devtools_devtools_0.localstorage < showSnippets.txt | python -m json.tool
    
        4
  •  1
  •   YinPeng.Wei    6 年前

    雨衣

    /Users/yourUserName/Library/Application Support/Google/Chrome/Default/Preferences
    

    Chrome将您的片段保存在首选项中。

        5
  •  0
  •   Community Mohan Dere    9 年前

    编辑 :这个答案是 过时的 ,片段不再保存在本地存储中。

    请参阅 paulirish's answer .

    对于旧版本的chrome,这可能会起作用。 它存储在C:\Users\Webdev\AppData\Local\Google\Chrome\User Data\Default\Local Storage\Chrome-devtools_0.localstorage中。 但我认为你无法从文件路径中查看它,

    您可以通过执行以下操作来查看它。 打开devtools,然后前往chrome://inspect,在“OTHER”标题下,您应该会看到刚刚打开的devtools面板url,该url以chrome开头-devtools://dev. 单击检查链接进行检查。 应该打开一个新的devtools窗口,在resources选项卡下,转到localstorage,那里有 被称为scriptSnippets的键,这就是它的保存位置。只需复制并粘贴它,或者使用控制台面板预先修改输出即可。

    希望这能有所帮助