-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content-scripts are not injected correctly #7597
Comments
@rogerwang Could you help me with? Or can i help you with this issue? |
This is blocker for our project. Is anything, which i can help resolve with this issue? |
Still broken in v0.49.2 |
Try run it several time please, it is works to 50%. It is strange. |
I tried it for 10 times and it always works... |
It is very strange, now i tested several times. Recorded to gif. Btw, dev tools are narrower and narrower after every startup. |
Reproduction Attempt Process:
My results are the same as Roger's. Cannot reproduce. |
Maybe race condition and dependent on the hardware or system load? I usually get around these type of issues by finding right spot to split the task with a timeout. Hate doing it but seems unavoidable sometimes. |
I tested on 3 differents PC and all has same issue. In NWjs 0.47.3 and below I never seen this issue ( |
@panther7 Also could not reproduce, but I would suggest deferring the creation of the Connection object until the page has loaded; i.e. instead of: const connection = new Connection(); which occurs at initialization time, you may try: document.addEventListener('DOMContentLoaded', () => {
window.connection = new Connection();
}); Or any other event that is appropriate, e.g. |
@corwin-of-amber Thanks for suggest, but it still is not fixed. or sometimes I hate these random issues. |
Actually @TheJaredWilcurt, I have tested it a bit more (on macOS) and indeed when I reload the window (that is, Ctrl+R in the devtools window) rather than quit & relaunch, the When quitting & relaunching, it is always there. |
@TheJaredWilcurt Still cant-reproduce? Last test from @corwin-of-amber confirmed my issue. Btw, I tested today in Chromium v86.0.4240.198 and it works properly. |
Tested on latest nwjs0.50, still broken. |
@panther7 it does seem like the only missing thing is |
@corwin-of-amber hmm, it seem, that nwjs after v0.47 has more "async" loading content-scripts. js: {
code: `var params = { test: true, frameId: '${uuid}', extensionId: '${location.hostname}' };`,
files: ['contentscript.js']
}, // Edit: Works in this simple demo, but in our huge project not. :-/ |
Sometimes, i have this error:
"String extensionId is optional", but error say, that must be present... there is something broken in NWjs.
|
I created new example, it is more simple. It use only Test in NWjs:
Please, try it. Thank you very much. |
@panther7 I don't think you are using the chrome API correctly -- as error message reported from your example, it prompts that you are missing a parameter 'extension id'. Please check https://developer.chrome.com/extensions/runtime#method-sendMessage |
@rogerwang Thanks for response. What is wrong? API is same over years. Works correctly up to NWjs 0.48. Param // Edit: Here is example with extensionId. The |
According to the doc, the param is required when the API is called from web pages, that is what the sample is doing from a webview. So it's expected to fail. It may "work" for you in previous versions, but that was a bug and is fixed now. Please do not rely on it. |
@rogerwang Next example is without them. Only define content-script window.js: webview.addContentScripts([
{
js: {
code: `
var params = {};
params.extensionId = '${location.hostname}';
`,
},
name: 'params', matches: ['<all_urls>'], all_frames: true, run_at: 'document_start',
},
{
js: {
files: ['contentscript.js']
},
name: 'scripts', matches: ['<all_urls>'], all_frames: true, run_at: 'document_start',
},
]); contentscript.js: try {
console.log(params.extensionId);
} catch (err) {
console.error(err);
alert(`Broken, check devtools > console > error \n\n ${err}`);
} |
Another Example without contentscript.js, all is in {
name: 'scripts-1',
js: {
code: `
function sendMessage(data) {
let extensionId = '${location.hostname}';
return new Promise(resolve => {
chrome.runtime.sendMessage(extensionId, data, resolve);
});
}
(async () => {
if (window.top === window) {
try {
const response = await sendMessage({ greetings: 'hello' });
console.log(response.greetings);
} catch (err) {
console.error(err);
alert(\`Broken, check devtools > console > error \\n\\n $\{err\}\`);
}
}
})();
`,
},
matches: ['<all_urls>'],
all_frames: true,
run_at: 'document_start',
} |
Wow! Works! Thanks @rogerwang It was very difficult to come up with more and more examples. :) |
@rogerwang Thanks, when is scheduled release v0.50.2? |
It is already released |
I see this problem again, but on a smaller scale, from NWjs 0.52 (maybe 0.51 also). Please, could you check it? @rogerwang |
NWJS Version: 0.49.x, 0.50.x
Operating System: Win 10
Expected behavior
Insert content-scripts correctly, works fine in NWjs 0.47.3 and NWjs 0.48.4
Actual behavior
Sometimes, after init/load webview and injection content-scripts (via
webview.addContentScripts
).How to reproduce
hello-world
from webview to window.hello-word
message.The text was updated successfully, but these errors were encountered: