diff --git a/README.md b/README.md
index 6b24f91..be5d73e 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,8 @@
Module to enhance ability of source replacement for website
+Note: Not using this in production environment
+
## Installation
```
@@ -15,16 +17,14 @@ yarn add source-replacement
### Usage
-#### In your source
+Attach `source-replacement/build/executors/source-replacement` on the script tag in `
` or mark it as `async type=module`
-```
-import 'source-replacement'
-```
+In your source import `source-replacement/build/executors/code-blocker` to prevent executing your source during the process of replacement
#### On your browser
Enter page with the following example
```
-https://example.com/#replacementTarget=https://your-target-js-source-url
+https://example.com/#targetReplacementSource=https://your-target-js-source-url
```
diff --git a/package.json b/package.json
index f8a8932..6166764 100644
--- a/package.json
+++ b/package.json
@@ -1,10 +1,9 @@
{
"name": "source-replacement",
- "version": "0.0.0",
- "main": "build/index.js",
- "entry": "src/index.ts",
+ "version": "2.0.0",
"types": "build",
"deploy": "build",
+ "main": "build/index.js",
"repository": {
"type": "git",
"url": "https://github.com/wongnai/source-replacement.git"
@@ -40,6 +39,7 @@
"semantic-release": "18.0.0",
"shake-detector": "0.3.7",
"ts-jest": "27.0.7",
- "typescript": "4.4.4"
+ "typescript": "4.4.4",
+ "vconsole": "3.14.3"
}
}
diff --git a/rollup.config.js b/rollup.config.js
index 75d584a..86457e7 100644
--- a/rollup.config.js
+++ b/rollup.config.js
@@ -1,34 +1,45 @@
import path from 'path'
-import commonjs from '@rollup/plugin-commonjs';
+import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
-import resolve from '@rollup/plugin-node-resolve';
+import resolve from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import visualizer from 'rollup-plugin-visualizer'
import config from './package.json'
-export default {
- input: config.entry,
- output: {
- dir: config.deploy,
- format: 'umd',
- sourcemap: true,
- name: 'SourceReplacement'
- },
- plugins: [
- resolve({
- browser: true,
- }),
- commonjs(),
- typescript({
- declaration: true,
- declarationDir: config.deploy,
- exclude: 'src/**/*.test.ts',
- }),
- json(),
- visualizer({
- filename: path.resolve(config.deploy, 'stat.html')
- }),
- ],
-};
+const sharedPlugins = [
+ resolve({
+ browser: true,
+ }),
+ commonjs(),
+ typescript({
+ declaration: true,
+ declarationDir: config.deploy,
+ exclude: 'src/**/*.test.ts',
+ }),
+ json(),
+ visualizer({
+ filename: path.resolve(config.deploy, 'stat.html'),
+ }),
+]
+
+export default [
+ {
+ input: 'src/executors/code-blocker/index.ts',
+ output: {
+ dir: config.deploy,
+ format: 'cjs',
+ },
+ plugins: sharedPlugins
+ },
+ {
+ input: 'src/executors/source-replacement/index.ts',
+ output: {
+ dir: config.deploy,
+ format: 'umd',
+ name: 'SourceReplacement',
+ },
+ plugins: sharedPlugins,
+ },
+]
diff --git a/src/core/preventPerformExistingScriptDuringInjection/index.test.ts b/src/core/preventPerformExistingScriptDuringInjection/index.test.ts
new file mode 100644
index 0000000..c6be402
--- /dev/null
+++ b/src/core/preventPerformExistingScriptDuringInjection/index.test.ts
@@ -0,0 +1,81 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+import preventPerformExistingScriptDuringInjection from '.'
+
+describe('preventPerformExistingScriptDuringInjection()', () => {
+ const MOCK_URL = 'http://example.com'
+
+ const promptSpy = jest.spyOn(global, 'prompt').mockReturnValue(MOCK_URL)
+
+ Object.defineProperty(window, 'location', {
+ value: {
+ reload: jest.fn(),
+ },
+ })
+
+ class VConsolePlugin {
+ events: Record = {}
+
+ on = jest.fn().mockImplementation((key: string, handler: Function) => {
+ const watcher = jest.fn()
+
+ handler(watcher)
+
+ this.events[key] = watcher.mock.calls[0][0]
+ })
+ }
+
+ class FakeVConsole {
+ static VConsolePlugin = VConsolePlugin
+
+ plugins: VConsolePlugin[] = []
+
+ addPlugin = jest.fn().mockImplementation(plugin => {
+ this.plugins.push(plugin)
+ })
+ }
+
+ window['VConsole'] = FakeVConsole
+
+ window['vConsole'] = new FakeVConsole()
+
+ afterEach(() => {
+ jest.clearAllMocks()
+ sessionStorage.clear()
+ })
+
+ it('should throw exception if app is marked from session that to be replaced', () => {
+ sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, MOCK_URL)
+
+ expect(() => preventPerformExistingScriptDuringInjection()).toThrowError(
+ new Error(
+ 'The under of code block stop performing causing by replacement process is running',
+ ),
+ )
+ })
+
+ it('should init vconsole plugin for set source from prompt to session', () => {
+ preventPerformExistingScriptDuringInjection()
+
+ const plugin = window['vConsole'].plugins[0]
+
+ expect(window['vConsole'].addPlugin).toBeCalledTimes(1)
+ expect(window['vConsole'].addPlugin).toBeCalledWith(plugin)
+ expect(plugin).toBeInstanceOf(VConsolePlugin)
+
+ expect(plugin.events.renderTab).toBe('Click to Replacement button below
')
+
+ expect(plugin.events.addTool).toHaveLength(1)
+
+ const addToolEvent = plugin.events.addTool[0]
+
+ expect(addToolEvent.name).toBe('Replacement')
+
+ addToolEvent.onClick()
+
+ expect(promptSpy).toBeCalledTimes(1)
+ expect(promptSpy).toBeCalledWith('Input target replacement')
+
+ expect(sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)).toBe(MOCK_URL)
+ expect(location.reload).toBeCalledTimes(1)
+ })
+})
diff --git a/src/core/preventPerformExistingScriptDuringInjection/index.ts b/src/core/preventPerformExistingScriptDuringInjection/index.ts
new file mode 100644
index 0000000..687789e
--- /dev/null
+++ b/src/core/preventPerformExistingScriptDuringInjection/index.ts
@@ -0,0 +1,46 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+
+import VConsole from 'vconsole'
+
+const PLUGIN_NAME = 'source_replacement'
+
+const TAB_NAME = 'Source Replacement'
+
+function preventPerformExistingScriptDuringInjection() {
+ if (sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)) {
+ throw new Error(
+ 'The under of code block stop performing causing by replacement process is running',
+ )
+ } else {
+ const Console: typeof VConsole = window['VConsole']
+
+ const consoleInstance: VConsole = window['vConsole']
+
+ if (Console && consoleInstance) {
+ const plugin = new Console.VConsolePlugin(PLUGIN_NAME, TAB_NAME)
+
+ plugin.on('renderTab', callback => {
+ callback('Click to Replacement button below
')
+ })
+
+ plugin.on('addTool', callback => {
+ callback([
+ {
+ name: 'Replacement',
+ onClick: () => {
+ const targetSource = prompt('Input target replacement')
+
+ sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, targetSource!)
+
+ location.reload()
+ },
+ },
+ ])
+ })
+
+ consoleInstance.addPlugin(plugin)
+ }
+ }
+}
+
+export default preventPerformExistingScriptDuringInjection
diff --git a/src/core/replaceSourceWithTargetSource/index.test.ts b/src/core/replaceSourceWithTargetSource/index.test.ts
deleted file mode 100644
index b2a93a3..0000000
--- a/src/core/replaceSourceWithTargetSource/index.test.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
-import * as replaceSourceWithTargetSourceType from '.'
-
-describe('replaceSourceWithTargetSource', () => {
- const getTargetSourceSpy = jest.fn()
- jest.doMock('utils/getTargetSource', () => getTargetSourceSpy)
-
- const loadTargetSourceSpy = jest.fn()
- jest.doMock('utils/loadTargetSource', () => loadTargetSourceSpy)
-
- const writeSourceToTargetPageSpy = jest.fn()
- jest.doMock('utils/writeSourceToTargetPage', () => writeSourceToTargetPageSpy)
-
- const { default: replaceSourceWithTargetSource } = require('.') as typeof replaceSourceWithTargetSourceType
-
- afterEach(() => {
- sessionStorage.clear()
- jest.clearAllMocks()
- })
-
- it('should not load and write target source if target source is not existed', async () => {
- getTargetSourceSpy.mockReturnValue(null)
-
- await replaceSourceWithTargetSource()
-
- expect(getTargetSourceSpy).toBeCalledTimes(1)
-
- expect(loadTargetSourceSpy).not.toBeCalled()
-
- expect(writeSourceToTargetPageSpy).not.toBeCalled()
- })
-})
diff --git a/src/core/replaceSourceWithTargetSource/index.ts b/src/core/replaceSourceWithTargetSource/index.ts
deleted file mode 100644
index c141be4..0000000
--- a/src/core/replaceSourceWithTargetSource/index.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
-import getTargetSource from 'utils/getTargetSource'
-import isReplacedPage from 'utils/isReplacedPage'
-import loadTargetSource from 'utils/loadTargetSource'
-import writeSourceToTargetPage from 'utils/writeSourceToTargetPage'
-
-async function replaceSourceWithTargetSource() {
- const targetSource = getTargetSource()
-
- if (targetSource && !isReplacedPage(targetSource)) {
- console.log('Start replacing with ', targetSource)
-
- sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, targetSource)
-
- const source = await loadTargetSource(targetSource)
-
- writeSourceToTargetPage(source)
- }
-}
-
-export default replaceSourceWithTargetSource
diff --git a/src/core/startDevTool/index.test.ts b/src/core/startDevTool/index.test.ts
deleted file mode 100644
index 3ae90d9..0000000
--- a/src/core/startDevTool/index.test.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-describe('startDevTool()', () => {
- let listenerSpy: Function
-
- const stopSpy = jest.fn()
-
- const startSpy = jest.fn()
-
- const subscribeSpy = jest.fn()
-
- class MockShakeDetector {
- subscribe = subscribeSpy.mockImplementation(listener => {
- listenerSpy = listener
-
- return this
- })
-
- stop = stopSpy
-
- start = startSpy
- }
-
- jest.doMock('shake-detector', () => MockShakeDetector)
-
- const replaceSourceWithTargetSourceSpy = jest.fn()
- jest.doMock('core/replaceSourceWithTargetSource', () => replaceSourceWithTargetSourceSpy)
-
- it('should perform start shake detector with bind listener to set target from prompt then perform source replacement and stop the shake detector', async () => {
- const { default: startDevTool } = await import('.')
-
- startDevTool()
-
- expect(subscribeSpy).toBeCalledTimes(1)
-
- expect(startSpy).toBeCalledTimes(1)
-
- expect(replaceSourceWithTargetSourceSpy).not.toBeCalled()
-
- expect(stopSpy).not.toBeCalled()
-
- listenerSpy()
-
- expect(stopSpy).toBeCalledTimes(1)
- })
-})
diff --git a/src/core/startDevTool/index.ts b/src/core/startDevTool/index.ts
deleted file mode 100644
index d6c61fb..0000000
--- a/src/core/startDevTool/index.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import replaceSourceWithTargetSource from 'core/replaceSourceWithTargetSource'
-import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
-import ShakeDetector from 'shake-detector'
-import isEmpty from 'lodash/isEmpty'
-
-function startDevTool() {
- const shakeDetector = new ShakeDetector({})
-
- shakeDetector.subscribe(() => {
- const target = prompt('Input your target replacement source url or leave it blank if no replacement wanted')
-
- sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, target!)
-
- location.reload()
-
- shakeDetector.stop()
- }).confirmPermissionGranted().start()
-
- shakeDetector.requestPermission().then(() => shakeDetector.start())
-}
-
-export default startDevTool
diff --git a/src/core/startReplacementProcess/index.test.ts b/src/core/startReplacementProcess/index.test.ts
new file mode 100644
index 0000000..c2f0611
--- /dev/null
+++ b/src/core/startReplacementProcess/index.test.ts
@@ -0,0 +1,99 @@
+describe('startReplacementProcess()', () => {
+ const MOCK_URL = 'http://example.com'
+
+ const MOCK_SOURCE = 'Test
'
+
+ const consumeSourceTargetSpy = jest.fn()
+ jest.doMock('utils/consumeSourceTarget', () => consumeSourceTargetSpy)
+
+ const getSourceTargetSpy = jest.fn()
+ jest.doMock('utils/getSourceTarget', () => getSourceTargetSpy)
+
+ const isReplacedPageSpy = jest.fn()
+ jest.doMock('utils/isReplacedPage', () => isReplacedPageSpy)
+
+ const loadTargetSourceSpy = jest.fn()
+ jest.doMock('utils/loadTargetSource', () => loadTargetSourceSpy)
+
+ const loggerSpy = jest.fn()
+ jest.doMock('utils/logger', () => loggerSpy)
+
+ const writeSourceToTargetPageSpy = jest.fn()
+ jest.doMock('utils/writeSourceToTargetPage', () => writeSourceToTargetPageSpy)
+
+ const { default: startReplacementProcess } = require('.') as typeof import('.')
+
+ afterEach(() => {
+ jest.clearAllMocks()
+ })
+
+ it('should consume source target and log no replacement happen if no source', async () => {
+ getSourceTargetSpy.mockReturnValue(null)
+
+ await startReplacementProcess()
+
+ expect(consumeSourceTargetSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledWith('No replacement happen')
+
+ expect(isReplacedPageSpy).not.toBeCalled()
+
+ expect(loadTargetSourceSpy).not.toBeCalled()
+
+ expect(writeSourceToTargetPageSpy).not.toBeCalled()
+ })
+
+ it('should consume source target and log page already replaced if source target already existed in dom', async () => {
+ getSourceTargetSpy.mockReturnValue(MOCK_URL)
+
+ isReplacedPageSpy.mockReturnValue(true)
+
+ await startReplacementProcess()
+
+ expect(consumeSourceTargetSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledWith('This page already replaced for http://example.com')
+
+ expect(isReplacedPageSpy).toBeCalledTimes(1)
+
+ expect(isReplacedPageSpy).toBeCalledWith(MOCK_URL)
+
+ expect(loadTargetSourceSpy).not.toBeCalled()
+
+ expect(writeSourceToTargetPageSpy).not.toBeCalled()
+ })
+
+ it('should consume source target and load source with write on page with notice user when process start and end', async () => {
+ getSourceTargetSpy.mockReturnValue(MOCK_URL)
+
+ loadTargetSourceSpy.mockReturnValue(MOCK_SOURCE)
+
+ isReplacedPageSpy.mockReturnValue(false)
+
+ await startReplacementProcess()
+
+ expect(consumeSourceTargetSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledTimes(2)
+
+ expect(loggerSpy).nthCalledWith(1, 'Start replacing process for http://example.com')
+
+ expect(loggerSpy).nthCalledWith(2, 'Page replaced with http://example.com')
+
+ expect(isReplacedPageSpy).toBeCalledTimes(1)
+
+ expect(isReplacedPageSpy).toBeCalledWith(MOCK_URL)
+
+ expect(loadTargetSourceSpy).toBeCalledTimes(1)
+
+ expect(loadTargetSourceSpy).toBeCalledWith(MOCK_URL)
+
+ expect(writeSourceToTargetPageSpy).toBeCalledTimes(1)
+
+ expect(writeSourceToTargetPageSpy).toBeCalledWith(MOCK_SOURCE)
+ })
+})
diff --git a/src/core/startReplacementProcess/index.ts b/src/core/startReplacementProcess/index.ts
new file mode 100644
index 0000000..208655a
--- /dev/null
+++ b/src/core/startReplacementProcess/index.ts
@@ -0,0 +1,34 @@
+import consumeSourceTarget from 'utils/consumeSourceTarget'
+import getSourceTarget from 'utils/getSourceTarget'
+import isReplacedPage from 'utils/isReplacedPage'
+import loadTargetSource from 'utils/loadTargetSource'
+import logger from 'utils/logger'
+import writeSourceToTargetPage from 'utils/writeSourceToTargetPage'
+
+async function startReplacementProcess() {
+ consumeSourceTarget()
+
+ const sourceTarget = getSourceTarget()
+
+ if (!sourceTarget) {
+ logger('No replacement happen')
+
+ return
+ }
+
+ if (isReplacedPage(sourceTarget)) {
+ logger(`This page already replaced for ${sourceTarget}`)
+
+ return
+ }
+
+ logger(`Start replacing process for ${sourceTarget}`)
+
+ const source = await loadTargetSource(sourceTarget)
+
+ writeSourceToTargetPage(source)
+
+ logger(`Page replaced with ${sourceTarget}`)
+}
+
+export default startReplacementProcess
diff --git a/src/executors/code-blocker/index.test.ts b/src/executors/code-blocker/index.test.ts
new file mode 100644
index 0000000..764f2c7
--- /dev/null
+++ b/src/executors/code-blocker/index.test.ts
@@ -0,0 +1,13 @@
+describe('code-blocker', () => {
+ const preventPerformExistingScriptDuringInjectionSpy = jest.fn()
+ jest.doMock(
+ 'core/preventPerformExistingScriptDuringInjection',
+ () => preventPerformExistingScriptDuringInjectionSpy,
+ )
+
+ it('should prevent perform existing script', async () => {
+ await import('.')
+
+ expect(preventPerformExistingScriptDuringInjectionSpy).toBeCalledTimes(1)
+ })
+})
diff --git a/src/executors/code-blocker/index.ts b/src/executors/code-blocker/index.ts
new file mode 100644
index 0000000..4c462f1
--- /dev/null
+++ b/src/executors/code-blocker/index.ts
@@ -0,0 +1,3 @@
+import preventPerformExistingScriptDuringInjection from 'core/preventPerformExistingScriptDuringInjection'
+
+preventPerformExistingScriptDuringInjection()
diff --git a/src/executors/source-replacement/index.test.ts b/src/executors/source-replacement/index.test.ts
new file mode 100644
index 0000000..ab5ec05
--- /dev/null
+++ b/src/executors/source-replacement/index.test.ts
@@ -0,0 +1,19 @@
+describe('source-replacement', () => {
+ const startReplacementProcessSpy = jest.fn()
+ jest.doMock('core/startReplacementProcess', () => startReplacementProcessSpy)
+
+ const loggerSpy = jest.fn()
+ jest.doMock('utils/logger', () => loggerSpy)
+
+ it('should perform start replacement process and warn log notify user not use this in production', async () => {
+ await import('.')
+
+ expect(startReplacementProcessSpy).toBeCalledTimes(1)
+
+ expect(loggerSpy).toBeCalledTimes(1)
+ expect(loggerSpy).toBeCalledWith(
+ '"Source Replacement" script is running, remove this if this is production environment.',
+ 'warn',
+ )
+ })
+})
diff --git a/src/executors/source-replacement/index.ts b/src/executors/source-replacement/index.ts
new file mode 100644
index 0000000..d18f291
--- /dev/null
+++ b/src/executors/source-replacement/index.ts
@@ -0,0 +1,10 @@
+import startReplacementProcess from 'core/startReplacementProcess'
+
+import logger from 'utils/logger'
+
+logger(
+ '"Source Replacement" script is running, remove this if this is production environment.',
+ 'warn',
+)
+
+startReplacementProcess()
diff --git a/src/index.test.ts b/src/index.test.ts
deleted file mode 100644
index 9d9875b..0000000
--- a/src/index.test.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-describe('script', () => {
- const replaceSourceWithTargetSourceSpy = jest.fn()
- jest.doMock('core/replaceSourceWithTargetSource', () => replaceSourceWithTargetSourceSpy)
-
- const startDevToolSpy = jest.fn()
- jest.doMock('core/startDevTool', () => startDevToolSpy)
-
- it('should perform replace source with target source and start dev tool', async () => {
- await import('.')
-
- expect(replaceSourceWithTargetSourceSpy).toBeCalledTimes(1)
- expect(startDevToolSpy).toBeCalledTimes(1)
- })
-})
diff --git a/src/index.ts b/src/index.ts
deleted file mode 100644
index 8610686..0000000
--- a/src/index.ts
+++ /dev/null
@@ -1,9 +0,0 @@
-import replaceSourceWithTargetSource from 'core/replaceSourceWithTargetSource'
-
-import startDevTool from 'core/startDevTool'
-
-console.log('Source Replacement is running')
-
-replaceSourceWithTargetSource()
-
-startDevTool()
diff --git a/src/utils/consumeSourceTarget/index.test.ts b/src/utils/consumeSourceTarget/index.test.ts
new file mode 100644
index 0000000..65eea14
--- /dev/null
+++ b/src/utils/consumeSourceTarget/index.test.ts
@@ -0,0 +1,38 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+
+describe('consumeSourceTarget()', () => {
+ const getReplacementTargetFromHashSpy = jest.fn()
+ jest.doMock('utils/getReplacementTargetFromHash', () => getReplacementTargetFromHashSpy)
+
+ const loggerSpy = jest.fn()
+ jest.doMock('utils/logger', () => loggerSpy)
+
+ const { default: consumeSourceTarget } = require('.') as typeof import('.')
+
+ afterEach(() => {
+ jest.clearAllMocks()
+ sessionStorage.clear()
+ })
+
+ it('should perform set session storage at targetReplacementSource as value from hash then log for notice user', () => {
+ const MOCK_URL = 'https://example.com'
+
+ getReplacementTargetFromHashSpy.mockReturnValue(MOCK_URL)
+
+ consumeSourceTarget()
+
+ expect(sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)).toBe(MOCK_URL)
+
+ expect(loggerSpy).toBeCalledWith('Consuming new replacement target as https://example.com')
+ })
+
+ it('should perform nothing that touch on session storage and logger', () => {
+ getReplacementTargetFromHashSpy.mockReturnValue(undefined)
+
+ consumeSourceTarget()
+
+ expect(sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)).toBeNull()
+
+ expect(loggerSpy).not.toBeCalled()
+ })
+})
diff --git a/src/utils/consumeSourceTarget/index.ts b/src/utils/consumeSourceTarget/index.ts
new file mode 100644
index 0000000..10023ae
--- /dev/null
+++ b/src/utils/consumeSourceTarget/index.ts
@@ -0,0 +1,15 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+import getReplacementTargetFromHash from 'utils/getReplacementTargetFromHash'
+import logger from 'utils/logger'
+
+function consumeSourceTarget() {
+ const sourceTargetFromHash = getReplacementTargetFromHash()
+
+ if (sourceTargetFromHash) {
+ sessionStorage.setItem(REPLACEMENT_SOURCE_KEY, sourceTargetFromHash)
+
+ logger(`Consuming new replacement target as ${sourceTargetFromHash}`)
+ }
+}
+
+export default consumeSourceTarget
diff --git a/src/utils/getReplacementTargetFromHash/index.test.ts b/src/utils/getReplacementTargetFromHash/index.test.ts
new file mode 100644
index 0000000..bcb2c41
--- /dev/null
+++ b/src/utils/getReplacementTargetFromHash/index.test.ts
@@ -0,0 +1,19 @@
+import getReplacementTargetFromHash from '.'
+
+describe('getReplacementTargetFromHash()', () => {
+ it('should return undefined if no hash', () => {
+ expect(getReplacementTargetFromHash()).toBeUndefined()
+ })
+
+ it('should return undefined if hash existed but no targetReplacementSource in hash', () => {
+ location.hash = '#otherHash'
+
+ expect(getReplacementTargetFromHash()).toBeUndefined()
+ })
+
+ it('should return url from targetReplacementSource in hash if existed', () => {
+ location.hash = '#targetReplacementSource=https://example.com'
+
+ expect(getReplacementTargetFromHash()).toBe('https://example.com')
+ })
+})
diff --git a/src/utils/getReplacementTargetFromHash/index.ts b/src/utils/getReplacementTargetFromHash/index.ts
new file mode 100644
index 0000000..7d284ba
--- /dev/null
+++ b/src/utils/getReplacementTargetFromHash/index.ts
@@ -0,0 +1,14 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+import qs from 'qs'
+
+function getReplacementTargetFromHash() {
+ const { hash } = location
+
+ if (!hash) {
+ return undefined
+ }
+
+ return qs.parse(hash.replace('#', ''))[REPLACEMENT_SOURCE_KEY] as string | undefined
+}
+
+export default getReplacementTargetFromHash
diff --git a/src/utils/getSourceTarget/index.test.ts b/src/utils/getSourceTarget/index.test.ts
new file mode 100644
index 0000000..606e425
--- /dev/null
+++ b/src/utils/getSourceTarget/index.test.ts
@@ -0,0 +1,13 @@
+import getSourceTarget from '.'
+
+describe('getSourceTarget()', () => {
+ it('should return null no replacementTarget in hash and sessionStorage', () => {
+ expect(getSourceTarget()).toBeNull()
+ })
+
+ it('should return value from sessionStorage if existed', () => {
+ sessionStorage.setItem('targetReplacementSource', 'https://example-storage.com')
+
+ expect(getSourceTarget()).toBe('https://example-storage.com')
+ })
+})
diff --git a/src/utils/getSourceTarget/index.ts b/src/utils/getSourceTarget/index.ts
new file mode 100644
index 0000000..214252b
--- /dev/null
+++ b/src/utils/getSourceTarget/index.ts
@@ -0,0 +1,7 @@
+import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
+
+function getSourceTarget() {
+ return sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)
+}
+
+export default getSourceTarget
diff --git a/src/utils/getTargetSource/index.test.ts b/src/utils/getTargetSource/index.test.ts
deleted file mode 100644
index 0e750ee..0000000
--- a/src/utils/getTargetSource/index.test.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-import getTargetSource from '.'
-
-describe('getTargetSource', () => {
- it('should return null no replacementTarget in hash and sessionStorage', () => {
- expect(getTargetSource()).toBeNull()
- })
-
- it('should return value from replacementTarget in hash if existed', () => {
- Object.defineProperty(window, 'location', {
- value: {
- hash: '#replacementTarget=https%3A%2F%2Fexample.com',
- },
- writable: true,
- })
- expect(getTargetSource()).toBe('https://example.com')
- })
-
- it('should return value from sessionStorage if existed', () => {
- Object.defineProperty(window, 'location', {
- value: {
- hash: '',
- },
- writable: true,
- })
-
- sessionStorage.setItem('targetReplacementSource', 'https://example-storage.com')
-
- expect(getTargetSource()).toBe('https://example-storage.com')
- })
-})
diff --git a/src/utils/getTargetSource/index.ts b/src/utils/getTargetSource/index.ts
deleted file mode 100644
index 5abde8d..0000000
--- a/src/utils/getTargetSource/index.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import qs from 'qs'
-
-import { REPLACEMENT_SOURCE_KEY } from 'debugConstants'
-
-function getTargetSource() {
- try {
- const targetSource = qs.parse(location.hash.replace('#', '')).replacementTarget || sessionStorage.getItem(REPLACEMENT_SOURCE_KEY)
-
- return targetSource as string
- } catch {
- return null
- }
-}
-
-export default getTargetSource
diff --git a/src/utils/isReplacedPage/index.test.ts b/src/utils/isReplacedPage/index.test.ts
index 85e451b..d8903de 100644
--- a/src/utils/isReplacedPage/index.test.ts
+++ b/src/utils/isReplacedPage/index.test.ts
@@ -1,25 +1,25 @@
-import isReplacedPage from "."
+import isReplacedPage from '.'
describe('isReplacedPage()', () => {
- const MOCK_URL = 'https://src'
+ const MOCK_URL = 'https://src'
- it('should return false if no script', () => {
- expect(isReplacedPage(MOCK_URL)).toBe(false)
- })
+ it('should return false if no script', () => {
+ expect(isReplacedPage(MOCK_URL)).toBe(false)
+ })
- it('should return false if non of script start with target src', () => {
- const script = document.createElement('script')
- script.src = 'https://not'
- document.body.append(script)
+ it('should return false if non of script start with target src', () => {
+ const script = document.createElement('script')
+ script.src = 'https://not'
+ document.body.append(script)
- expect(isReplacedPage(MOCK_URL)).toBe(false)
- })
-
- it('should return true if some of script start with target src', () => {
- const script = document.createElement('script')
- script.src = MOCK_URL
- document.body.append(script)
+ expect(isReplacedPage(MOCK_URL)).toBe(false)
+ })
- expect(isReplacedPage(MOCK_URL)).toBe(true)
- })
+ it('should return true if some of script start with target src', () => {
+ const script = document.createElement('script')
+ script.src = MOCK_URL
+ document.body.append(script)
+
+ expect(isReplacedPage(MOCK_URL)).toBe(true)
+ })
})
diff --git a/src/utils/isReplacedPage/index.ts b/src/utils/isReplacedPage/index.ts
index fafd0a9..45dbf3a 100644
--- a/src/utils/isReplacedPage/index.ts
+++ b/src/utils/isReplacedPage/index.ts
@@ -1,5 +1,7 @@
function isReplacedPage(targetSource: string) {
- return [...document.getElementsByTagName('script')].some(script => script.src.startsWith(targetSource))
+ return [...document.getElementsByTagName('script')].some(script =>
+ script.src.startsWith(targetSource),
+ )
}
export default isReplacedPage
diff --git a/src/utils/loadTargetSource/index.test.ts b/src/utils/loadTargetSource/index.test.ts
index 6877aa6..d110a96 100644
--- a/src/utils/loadTargetSource/index.test.ts
+++ b/src/utils/loadTargetSource/index.test.ts
@@ -1,22 +1,22 @@
-describe('loadTargetSource', () => {
- const placeLoadingPlaceholderSpy = jest.fn()
- jest.doMock('utils/placeLoadingPlaceholder', () => placeLoadingPlaceholderSpy)
+describe('loadTargetSource()', () => {
+ const placeLoadingPlaceholderSpy = jest.fn()
+ jest.doMock('utils/placeLoadingPlaceholder', () => placeLoadingPlaceholderSpy)
- it('should perform place loading and load source from target', async () => {
- const MOCK_SOURCE = ''
- const fetchSpy = jest.fn()
- Object.defineProperty(window, 'fetch', {
- value: fetchSpy.mockReturnValue({
- text: () => MOCK_SOURCE,
- })
- })
+ it('should perform place loading and load source from target', async () => {
+ const MOCK_SOURCE = 'LMWN Debugger Replacing In Progress...
Target Replacement is http://example.com
')
- })
+ expect(writeSourceToTargetPageSpy).toBeCalledWith(
+ 'LMWN Debugger Replacing In Progress...
Target Replacement is http://example.com
',
+ )
+ })
})
diff --git a/src/utils/placeLoadingPlaceholder/index.ts b/src/utils/placeLoadingPlaceholder/index.ts
index b8988e4..564987d 100644
--- a/src/utils/placeLoadingPlaceholder/index.ts
+++ b/src/utils/placeLoadingPlaceholder/index.ts
@@ -1,9 +1,9 @@
import writeSourceToTargetPage from 'utils/writeSourceToTargetPage'
function placeLoadingPlaceholder(target: string) {
- const LOADING_PLACEHOLDER = `LMWN Debugger Replacing In Progress...
Target Replacement is ${target}
`
+ const LOADING_PLACEHOLDER = `LMWN Debugger Replacing In Progress...
Target Replacement is ${target}
`
- writeSourceToTargetPage(LOADING_PLACEHOLDER)
+ writeSourceToTargetPage(LOADING_PLACEHOLDER)
}
export default placeLoadingPlaceholder
diff --git a/src/utils/writeSourceToTargetPage/index.test.ts b/src/utils/writeSourceToTargetPage/index.test.ts
index 7e1dc6a..dbac7c2 100644
--- a/src/utils/writeSourceToTargetPage/index.test.ts
+++ b/src/utils/writeSourceToTargetPage/index.test.ts
@@ -1,18 +1,18 @@
import writeSourceToTargetPage from '.'
-describe('writeSourceToTargetPage', () => {
- it('should open document for write source then close', () => {
- // @ts-ignore
- const openSpy = jest.spyOn(document, 'open').mockImplementation(() => {})
- const writeSpy = jest.spyOn(document, 'write').mockImplementation(() => {})
- const closeSpy = jest.spyOn(document, 'close').mockImplementation(() => {})
+describe('writeSourceToTargetPage()', () => {
+ it('should open document for write source then close', () => {
+ // @ts-ignore
+ const openSpy = jest.spyOn(document, 'open').mockImplementation(() => {})
+ const writeSpy = jest.spyOn(document, 'write').mockImplementation(() => {})
+ const closeSpy = jest.spyOn(document, 'close').mockImplementation(() => {})
- const MOCK_SOURCE = '