userscripts
userscripts copied to clipboard
Script Injecting twice on particular website
// ==UserScript==
// @name Github Alert
// @version 1.0
// @description Github Alert
// @author Dennis
// @match https://*.tankionline.com/*
// @grant none
// @require https://jeremyckahn.github.io/keydrown/dist/keydrown.js
// @icon https://www.google.com/s2/favicons?domain=tankionline.com
// @noframes
// ==/UserScript==
window.onload = () => {
alert("ran")
}
kd.T.up = () => {
alert("Alert: activated from keydrown")
}
console.log(`This is a test script - ${window.location.href}`)
System Information:
macOS or iOS version: iPadOS 15.4.1 Userscripts version: v1.1 (19) Safari version: Latest (bundled with iPadOS 15.4.1) Is this issue related to script injection? Yes Did the test script (pasted above) successfully run on your machine? Yes
Hello, for some reason this script gets injected twice on this particular website. I thought it could be because of the require but I’ve removed it and it still has the same issue. What could be the reason and is there a workaround for it? Website
I thought it could be because of the require but I’ve removed it
Considering that, i am testing this (slight edit to what you pasted above):
// ==UserScript==
// @name Github Alert
// @version 1.0
// @description Github Alert
// @author Dennis
// @match https://*.tankionline.com/*
// @grant none
// @icon https://www.google.com/s2/favicons?domain=tankionline.com
// @noframes
// ==/UserScript==
window.onload = () => {
alert("ran")
}
console.log(`This is a test script - ${window.location.href}`)
When you say it gets injected twice, how are determining that? When I visit the website you linked, I only see 1 alert are you seeing it twice?
In the video below I refresh the page to demonstrate the page load event and the single alert I see:
https://user-images.githubusercontent.com/7660254/169591062-4f60afae-b725-446a-a05d-224e4703438e.MP4
I’m not sure when it started, but recently all scripts are being injected at least twice. Here’s the console log:

(Let me know if I should open another issue for this)
System Information:
macOS or iOS version: macOS 12.1 Userscripts version: v4.1.0 (47) Safari version: v15.2 (17612.3.6.1.6)
@HazemAM could you link me to one of those userscripts and give me the page url where you saw the multiple injection happening (from the screenshot you provided)? Thank you
@quoid Sure! Here’s the Remove Google Redirection script.
I used a Google search to test that, here’s an example URL.
Also something I noticed: the last 2 injects (for PiP on keypress) happen async after the page fully loads, which is different from the rest.
When you say it gets injected twice, how are determining that? When I visit the website you linked, I only see 1 alert are you seeing it twice?
Yes precisely! I'm seeing the alert twice that's very strange. Here's my screen recording
https://user-images.githubusercontent.com/25965937/169876449-c842cffc-7bea-4bc1-942b-4b976161ebce.mov
@HazemAM Thank you for supplying the script and url
@dennis777 thanks for providing the image, that certainly seems like double injection is happening
Would either of you be willing to sign up for beta releases and see if the problem persists? It's available on Testflight for iOS, ipadOS and macOS.
You could use the links below or directly email me; my email address is on my profile page.
Great! I’ll get that setup asap and report back to you
Best regards, Dennis
On May 23, 2022, at 16:19, Justin Wasack @.***> wrote:
@HazemAM Thank you for supplying the script and url
@dennis777 thanks for providing the image, that certainly seems like double injection is happening
Would either of you be willing to sign up for beta releases and see if the problem persists? It's available on Testflight for iOS, ipadOS and macOS.
You could use the links below or directly email me; my email address is on my profile page.
iOS Beta Sign Up Form
macOS Beta Sign Up Form
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.
Alright, so I just tried it and sadly still seems to be double injection going on even on the latest-beta v1.2 (24)
Dang, I'm really lost here. I do not know why this would happen with some users but not others.
I would have suggested perhaps a screen share if you were on a computer but since you're on an iPad and there's limited debugging one can easily do on that, I'm not sure what to do next. I'm curious to see how @HazemAM made out with the beta build.
I have a Mac! I use it too same issue persists there so we can definitely get that setup sometime if you’d like!
Best regards, Dennis
On May 26, 2022, at 19:01, Justin Wasack @.***> wrote:
Dang, I'm really lost here. I do not know why this would happen with some users but not others.
I would have suggested perhaps a screen share if you were on a computer but since you're on an iPad and there's limited debugging one can easily do on that, I'm not sure what to do next. I'm curious to see how @HazemAM made out with the beta build.
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.
@dennis777
That would be super helpful, you can shoot me an email (on my profile page) to set something up if you'd be willing, thanks again.
@quoid I tried the 4.2.0 beta and I still see the issue. Let me know if I can help somehow
@HazemAM
At this point I think the best thing to try would be to do a screen share and have me guide you through some checks/debug steps. If that's something you'd be comfortable with, shoot me an email and we can set something up.
It's an old bug: https://github.com/quoid/userscripts/issues/197
I don't know why this occurs, but to fix it on my side, I did this at the top of all my scripts:
/* Double Injection Protection
-------------------------*/
if (document.querySelector('#uniqueIDForScript')) {
throw Error('This script was already injected...');
}
document.body.append(CreateElement('div', async divTag => {
divTag.setAttribute('id', 'uniqueIDForScript');
divTag.style.setProperty('display', 'none');
await Sleep(10000);
divTag.remove();
}))
function CreateElement(type, func) {
const tag = document.createElement(type);
func(tag);
return tag;
}
function Sleep(ms) {
return new Promise(a => setTimeout(() => a(true), ms));
}
Okay, I’ve signed up with the form looking forward to resolving this issue!
Best regards, Dennis
On May 23, 2022, at 16:27, Dennis Litvinenko @.***> wrote:
Great! I’ll get that setup asap and report back to you
Best regards, Dennis
On May 23, 2022, at 16:19, Justin Wasack @.***> wrote:
@HazemAM Thank you for supplying the script and url
@dennis777 thanks for providing the image, that certainly seems like double injection is happening
Would either of you be willing to sign up for beta releases and see if the problem persists? It's available on Testflight for iOS, ipadOS and macOS.
You could use the links below or directly email me; my email address is on my profile page.
iOS Beta Sign Up Form
macOS Beta Sign Up Form
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.
This PR #433 should help check for such issues.
The most likely cause of double injection is the presence of subframes, and the improved console output included above should be a great help in detecting this.
Since this question is very stale, I'll close it. Thanks for your feedback. If you still encounter this issue, please add a new comment and relevant console logs so we can check for possible errors.