-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new bookmarklet to display posting (publish) date of a product …
…listing on Mercado Livre
- Loading branch information
1 parent
2cc5a83
commit ee07c01
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
/** | ||
* refs: | ||
* - | ||
* limitations: | ||
* - | ||
*/ | ||
|
||
// @twing-include {% include 'building_blocks/shared/partials/utils.js' %} | ||
|
||
const BLOCK_NAME = "toggle-mercadolivre-posting-date"; | ||
|
||
const removeItself = () => { | ||
let e = document.querySelector("#" + BLOCK_NAME); | ||
e.parentNode.removeChild(e); | ||
e = null; | ||
window.blockFn[BLOCK_NAME] = null; | ||
delete window.blockFn[BLOCK_NAME]; | ||
}; | ||
|
||
if (document.querySelector("#" + BLOCK_NAME)) { | ||
blockFn[BLOCK_NAME].removeItself(); | ||
} else { | ||
|
||
if (!window.blockFn) { | ||
window.blockFn = {}; | ||
} | ||
window.blockFn[BLOCK_NAME] = {}; | ||
window.blockFn[BLOCK_NAME].removeItself = removeItself; | ||
|
||
let mainPostElement = document.querySelector("#ui-pdp-main-container"); | ||
|
||
let preloadedData = window[ "__PRELOADED_STATE__"]; | ||
let postingDateValue = nestedValue(preloadedData, 'initialState.components.track.gtm_event.startTime'); | ||
|
||
if (mainPostElement && postingDateValue) { | ||
let postingDateElement = document.createElement('span'); | ||
postingDateElement.id = BLOCK_NAME; | ||
fill(postingDateElement.style, | ||
['position', 'fontSize', 'fontFamily', 'marginLeft'], | ||
['absolute', '11px', 'sans-serif', '5px'] | ||
); | ||
|
||
let formattedDateValue = new Intl.DateTimeFormat('pt-BR', { dateStyle: 'full', timeStyle: 'long', timeZone: 'America/Sao_Paulo' }).format(new Date(postingDateValue)); | ||
postingDateElement.textContent = `Postado em: ${formattedDateValue}`; | ||
|
||
mainPostElement.insertAdjacentElement('beforebegin', postingDateElement); | ||
} | ||
} |