-
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.
Merge pull request #97 from yakisova41/dev-script-use-gm-xhr
✨ feature: It can disable to websocket in dev userscript.
- Loading branch information
Showing
5 changed files
with
63 additions
and
7 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
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
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
33 changes: 33 additions & 0 deletions
33
packages/crx-monkey/src/node/static/files/userScriptDevUseGmXhr.js
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,33 @@ | ||
/* eslint-disable no-undef */ | ||
async function getResponse() { | ||
return new Promise((resolve) => { | ||
GM.xmlHttpRequest({ | ||
url: 'http://${devServer.host}:${devServer.port}/userscript', | ||
onload: (e) => { | ||
resolve(e.responseText); | ||
}, | ||
}); | ||
}); | ||
} | ||
|
||
function watchScriptDiff(initialCode) { | ||
let scriptContentTmp = initialCode; | ||
|
||
setInterval(() => { | ||
getResponse().then((code) => { | ||
if (scriptContentTmp !== code) { | ||
location.reload(); | ||
} | ||
}); | ||
}, 1000); | ||
|
||
return scriptContentTmp; | ||
} | ||
|
||
getResponse().then((code) => { | ||
watchScriptDiff(code); | ||
|
||
const scriptElem = document.createElement('script'); | ||
scriptElem.textContent = code; | ||
unsafeWindow.document.body.appendChild(scriptElem); | ||
}); |
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