Javascript / Chrome - 如何從webkit檢查器複製對象作爲代碼

本文翻譯自:Javascript / Chrome - How to copy an object from the webkit inspector as code

I am doing a console.log statement in my javascript in order to log a javascript object. 我在我的javascript中做一個console.log語句,以便記錄一個javascript對象。 I'm wondering if there's a way, once that's done - to copy that object as javascript code. 我想知道是否有辦法,一旦完成 - 將該對象複製爲javascript代碼。 What I'm trying to do is convert an object that was created using ajax to parse an xml feed into a static javascript object so that a file can run locally, without a server. 我要做的是轉換使用ajax創建的對象將xml提要解析爲靜態javascript對象,以便文件可以在本地運行,而無需服務器。 I've included a screenshot of the object in the chrome inspector window so you can see what I'm trying to do. 我在chrome檢查器窗口中包含了對象的屏幕截圖,以便您可以看到我正在嘗試做的事情。 在此輸入圖像描述


#1樓

參考:https://stackoom.com/question/hEtZ/Javascript-Chrome-如何從webkit檢查器複製對象作爲代碼


#2樓

Try JSON.stringify() . 試試JSON.stringify() Copy the resulting string. 複製結果字符串。 Does not work with objects containing circular references. 不適用於包含循環引用的對象。


#3樓

You can copy an object to your clip board using copy(JSON.stringify(Object_Name)); 您可以使用copy(JSON.stringify(Object_Name))將對象複製到剪貼板; in the console. 在控制檯中。

Eg:- Copy & Paste the below code in your console and press ENTER. 例如: - 在控制檯中複製並粘貼以下代碼,然後按ENTER。 Now, try to paste(CTRL+V for Windows or CMD+V for mac) it some where else and you will get {"name":"Daniel","age":25} 現在,嘗試粘貼(Windows的CTRL + V或Mac的CMD + V)以及其他地方,您將獲得{“name”:“Daniel”,“age”:25}

var profile = {
    name: "Daniel",
    age: 25
};

copy(JSON.stringify(profile));

#4樓

You can now accomplish this in Chrome by right clicking on the object and selecting "Store as Global Variable": http://www.youtube.com/watch?v=qALFiTlVWdg 您現在可以通過右鍵單擊對象並選擇“存儲爲全局變量”在Chrome中完成此操作: http//www.youtube.com/watch?v = qALFiTlVWdg

在此輸入圖像描述


#5樓

  1. Right-click an object in Chrome's console and select Store as Global Variable from the context menu. 右鍵單擊Chrome控制檯中的對象,然後從上下文菜單中選擇“ Store as Global Variable ”。 It will return something like temp1 as the variable name. 它將返回類似temp1的變量名稱。

  2. Chrome also has a copy() method, so copy(temp1) in the console should copy that object to your clipboard. Chrome還有一個copy()方法,因此控制檯中的copy(temp1)應該將該對象複製到剪貼板。

在Chrome DevTools中複製Javascript對象

Note on Recursive Objects: If you're trying to copy a recursive object, you will get [object Object] . 關於遞歸對象的注意事項:如果您正在嘗試複製遞歸對象,您將獲得[object Object] This is to be expected. 這是可以預料的。


#6樓

If you've sent the object over a request you can copy it from the Chrome -> Network tab. 如果您通過請求發送了對象,則可以從Chrome - >網絡標籤中複製該對象。

Request Payload - > View Source 請求有效負載 - >查看源

在此輸入圖像描述

在此輸入圖像描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章