Javascript没有调用勇敢浏览器



我有一个功能,适用于所有浏览器,除了勇敢的浏览器。知道为什么吗?

有一个表单调用一个函数用用户的输入重定向到一个新的URL。在勇敢浏览器中调用时,它不做任何事情。它适用于所有其他浏览器。这是从Wordpress站点调用的。会不会跟这个有关?

<form id="url-10">
<input id="red-border-error-2" class="url10-form" type="text" name="urlName-10">
<input class="url10-button" type="submit" value="Build your app"></input>
</form> 
<div id="error-message-10"></div>
<script>
// JavaScript goes here


document.getElementById("url-10").addEventListener("submit", (event) => {
event.preventDefault()
let errorMessage = document.getElementById("red-border-error-2").placeholder = " Type your store URL here ";
let x = document.getElementById("red-border-error-2");
let myForm = document.getElementById("url-10");
let formData = new FormData(myForm);
if (formData.get("urlName-10") === "") {
return x.classList.add('placeholder-red-text');
errorMessage;
}
else{
EndOfUrl = sanitizeDomainInput(formData.get("urlName-10"));
newUrl = redirectLink(EndOfUrl);
markConversionAndRedirect(newUrl);
return false;
}
});
function markConversionAndRedirect(newUrl) {
gtag('event', 'conversion', {
'send_to': 'G-Q0HF9N3DYR/connect_square',
'event_callback': function () { window.location.href = newUrl }
})
};

function sanitizeDomainInput(input) {
input = input || 'unknown.com'
if (input.startsWith('http://')) {
input = input.substr(7)
}
if (input.startsWith('https://')) {
input = input.substr(8)
}
var regexp = new RegExp(/^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9])).([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$/)
return regexp.test(input) ? input : 'unknown.com';

};
function redirectLink(domain) {
const urlParams = new URLSearchParams(window.location.search);
const refValue = urlParams.get('ref'); 
return `https://dashboard.getorda.com/signup/?state=${domain}` + (refValue ? `_${ refValue }` : '');
};
</script>

我会说这是因为Brave阻止了您正在使用的gtag.js库。因为你的重定向发生在gtag回调函数内部,这意味着Brave永远不会执行此代码。

你可以修改你的markConversationAndRedirect功能,使其工作,即使gtag被阻塞。见下文.

function markConversionAndRedirect(newUrl) {
if (typeof gtag !== 'undefined') {
// gtag is loaded, we can safely use it
gtag('event', 'conversion', {
'send_to': 'G-Q0HF9N3DYR/connect_square',
'event_callback': function () { window.location.href = newUrl }
})
} else {
// gtag didnt load, redirect anyway
window.location.href = newUrl
}
};

你可以模拟一个自定义超时并返回一个承诺。在Brave中,超时将被超过,promise将被拒绝,然后你可以在catch块上运行自定义代码,或者在finally块中运行相同的行为。例子:

/* Sends Google Analytics event with custom timeout and returns a promise */
export function sendGAEventWithTimeout(eventName, threshold) {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(), threshold);
window.gtag('event', eventName, {
send_to: 'G-Q0HF9N3DYR/connect_square',
event_callback: (...args) => {
clearTimeout(timeout);
resolve(...args);
}
});
});
}
/* Run different code depending on if it fails (aka you are in Brave) or not */
sendGAEventWithTimeout('conversion', 1000)
.then((result) => {
window.location.href = newUrl;
})
.catch(() => {
// This will run on Brave or any other browser that blocks GA
});
/* Run same code on both cases */
sendGAEventWithTimeout('conversion', 1000)
.finally(() => {
window.location.href = newUrl;
});

最新更新