Skip to content

Commit

Permalink
feat: support latest devtools
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Aug 28, 2024
1 parent 747a5c4 commit 9d92ea3
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
11 changes: 6 additions & 5 deletions devtools/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ window.onload = function () {
function resetDevtools() {
const window = devtoolsIframe.contentWindow
setTimeout(() => {
window.runtime.loadLegacyModule('core/sdk/sdk-legacy.js').then(() => {
const SDK = window.SDK
for (const resourceTreeModel of SDK.TargetManager.instance().models(
SDK.ResourceTreeModel
window.runtime.loadLegacyModule('core/sdk/sdk.js').then(SDKModule => {
for (const resourceTreeModel of SDKModule.TargetManager.TargetManager.instance().models(
SDKModule.ResourceTreeModel.ResourceTreeModel
)) {
resourceTreeModel.dispatchEventToListeners(
SDK.ResourceTreeModel.Events.WillReloadPage,
SDKModule.ResourceTreeModel.Events.WillReloadPage,
resourceTreeModel
)
}
Expand All @@ -69,5 +68,7 @@ function resetDevtools() {
sendToChobitsu({ method: 'CSS.enable' })
sendToChobitsu({ method: 'Overlay.enable' })
sendToDevtools({ method: 'DOM.documentUpdated' })
sendToChobitsu({ method: 'Page.enable' })
sendToDevtools({ method: 'Page.loadEventFired' })
}, 0)
}
6 changes: 6 additions & 0 deletions src/domains/DOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ export function getTopLayerElements(): DOM.GetTopLayerElementsResponse {
}
}

export function getNodesForSubtreeByStyle(): DOM.GetNodesForSubtreeByStyleResponse {
return {
nodeIds: [],
}
}

function parseAttributes(str: string) {
str = `<div ${str}></div>`

Expand Down
49 changes: 42 additions & 7 deletions src/domains/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ export function emulateTouchFromMouseEvent(
case 'mouseReleased':
triggerTouchEvent('touchend', el, x, y)
if (isClick) {
el.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
})
)
triggerMouseEvent('click', el, x, y)
}
isClick = false
break
Expand All @@ -42,6 +36,47 @@ export function emulateTouchFromMouseEvent(
}
}

export function dispatchMouseEvent(params: Input.DispatchMouseEventRequest) {
const { type, x, y, deltaX, deltaY } = params

const el = document.elementFromPoint(x, y) || document.documentElement

switch (type) {
case 'mousePressed':
isClick = true
triggerMouseEvent('mousedown', el, x, y)
break
case 'mouseMoved':
isClick = false
triggerMouseEvent('mousemove', el, x, y)
break
case 'mouseReleased':
triggerMouseEvent('mouseup', el, x, y)
if (isClick) {
triggerMouseEvent('click', el, x, y)
}
isClick = false
break
case 'mouseWheel':
if (!isUndef(deltaX) && !isUndef(deltaY)) {
triggerScroll(el, deltaX, deltaY)
}
break
}
}

function triggerMouseEvent(type: string, el: Element, x: number, y: number) {
el.dispatchEvent(
new MouseEvent(type, {
bubbles: true,
cancelable: true,
view: window,
clientX: x,
clientY: y,
})
)
}

function triggerTouchEvent(type: string, el: Element, x: number, y: number) {
const touch = new Touch({
identifier: 0,
Expand Down
18 changes: 17 additions & 1 deletion src/domains/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import each from 'licia/each'
import rmCookie from 'licia/rmCookie'
import safeStorage from 'licia/safeStorage'
import connector from '../lib/connector'
import { getCookies } from './Network'
import Protocol from 'devtools-protocol'
import Storage = Protocol.Storage
Expand Down Expand Up @@ -39,7 +40,7 @@ export function getTrustTokens(): Storage.GetTrustTokensResponse {

export function getStorageKeyForFrame(): Storage.GetStorageKeyForFrameResponse {
return {
storageKey: 'chobitsu',
storageKey: location.origin,
}
}

Expand All @@ -53,3 +54,18 @@ export function getSharedStorageMetadata(): Storage.GetSharedStorageMetadataResp
},
}
}

export function setStorageBucketTracking() {
connector.trigger('Storage.storageBucketCreatedOrUpdated', {
bucketInfo: {
bucket: {
storageKey: location.origin,
},
durability: 'relaxed',
expiration: 0,
id: '0',
persistent: false,
quota: 0,
},
})
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ chobitsu.register('Storage', {
...Storage,
setInterestGroupTracking: noop,
setSharedStorageTracking: noop,
setStorageBucketTracking: noop,
trackIndexedDBForStorageKey: noop,
untrackCacheStorageForOrigin: noop,
untrackIndexedDBForOrigin: noop,
trackCacheStorageForOrigin: noop,
Expand Down

0 comments on commit 9d92ea3

Please sign in to comment.