Skip to content

Commit

Permalink
Fixing #45 issue where tsi indicator is missing when main chart move …
Browse files Browse the repository at this point in the history
…to different pane
  • Loading branch information
AzrizHaziq committed Aug 21, 2022
1 parent 365b750 commit 3c163b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/extension/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function observeNodeChanges(
nodeToObserve: Element,
cb: () => unknown,
options: Partial<MutationObserverInit> = { childList: true, subtree: true }
): MutationObserver {
): () => void {
// eslint-disable-next-line prefer-const
let observer: MutationObserver

Expand All @@ -83,7 +83,7 @@ export function observeNodeChanges(

observer.observe(nodeToObserve, options)

return observer
return () => observer.disconnect()
}

export function waitForElm(selector: string): Promise<Element> {
Expand Down
25 changes: 20 additions & 5 deletions packages/extension/src/page/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ import {
observeNodeChanges,
} from '../helper'

waitForElm('[data-name="legend-series-item"]').then(setStockListInMap).then(mainScript)
// eslint-disable-next-line @typescript-eslint/no-empty-function
let obs = () => {}
const selectors = {
mainChartStockNameEl: '[data-name="legend-series-item"] [data-name="legend-source-title"]',
mainChartStockSeriesEl: '[data-name="legend-series-item"]',
mainChartEl:
'body > div.js-rootresizer__contents.layout-with-border-radius > div.layout__area--center > div.chart-container.single-visible.top-full-width-chart.active > div.chart-container-border',
}

waitForElm(selectors.mainChartStockSeriesEl)
.then(setStockListInMap)
.then(() => waitForElm(selectors.mainChartEl))
.then((rootChartElement) => {
obs()
observeNodeChanges(rootChartElement, mainScript)
})

function mainScript() {
// have to target dom like below since this is the most top parent
const symbolNode = document.querySelector('[data-name="legend-series-item"]')
observeNodeChanges(symbolNode, chartScript)
const symbolNode = document.querySelector(selectors.mainChartStockSeriesEl)
obs = observeNodeChanges(symbolNode, chartScript)
}

async function chartScript(): Promise<void> {
const { parentElement } = document.querySelector('[data-name="legend-source-title"]')
const { parentElement } = document.querySelector(selectors.mainChartStockNameEl)

// getting cssInJS hash like titleWrapper-1WIwNaDF (1WIwNaDF)
// getting cssInJS hash like titleWrapper-1WIwNaDF -> 1WIwNaDF
const cssInJsHash = parentElement.className
.split(' ')
.find((i) => i.startsWith('titleWrapper-'))
Expand Down
6 changes: 4 additions & 2 deletions packages/extension/src/page/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
observeNodeChanges,
} from '../helper'

let obs: MutationObserver
// eslint-disable-next-line @typescript-eslint/no-empty-function
let obs = () => {}

const largeResoSelector = '.tv-symbol-header .tv-symbol-header__second-line .tv-symbol-header__exchange'
const smallResoSelector = '.tv-symbol-header.tv-symbol-header--mobile .tv-symbol-header__first-line'

Expand All @@ -34,7 +36,7 @@ function symbolScript() {
} else {
// have to disconnect current observer so that it doesn't create loop
// because below we create icon and insert into div which trigger again observer.
obs.disconnect()
obs()

const icon1 = createIcon()
icon1.style.position = 'relative'
Expand Down

0 comments on commit 3c163b4

Please sign in to comment.