I am creating a Chrome extension, but I would like to retrieve HTML elements and save them to Chrome.storage.
For pages with only h1 tags, if you run JavaScript below,
>Value is set to <h1>Hello, world</h1>
>Value currently {key:[Prototype]]:Object}
The output is such that it is not reflected.
Please let me know.
const h1 = document.querySelector('h1')
chrome.storage.local.set({
"key"—h1,
}, function(){
console.log('Value is set to',h1)
})
chrome.storage.local.get(['key'], function(result){
console.log('Value currently is', result)
})
<body>
<h1>Hello, world</h1>
</body>
manifest.json
{
"manifest_version"—3,
"name": "Get Elements",
"description": "Get elements and save them to storage",
"version": "1.0",
"action": {
"default_popup": "popup.html"
},
"content_scripts":[
{
"matches": ["<all_urls>",
"js": [
"content-script.js"
],
"run_at": " document_end",
"all_frames"—true
}
],
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+H",
"mac": "MacCtrl+Shift+H"
},
"description": "Run extension."
}
},
"permissions": [
"activeTab",
"contextMenus",
"clipboardWrite",
"scripting",
"tabs",
"storage"
],
"options_page": "options.html"
}
When saving to storage, the object cannot be saved as it is, so serialization occurs (because the data can only be saved as a byte column).
In addition to the primitive values, the array, Date
and Regex
seem to be the expected serialization, but consider that other objects, including DOM, cannot basically be retained.
I don't know what behavior you're expecting, but if you want to keep it as HTML, would it be possible to extract HTML as text in Element.outerHTML?
© 2025 OneMinuteCode. All rights reserved.