Skip to content

Instantly share code, notes, and snippets.

@songouyang
Created July 31, 2025 02:43
Show Gist options
  • Select an option

  • Save songouyang/05e6922172cf4cc2353bbaab7b089bd9 to your computer and use it in GitHub Desktop.

Select an option

Save songouyang/05e6922172cf4cc2353bbaab7b089bd9 to your computer and use it in GitHub Desktop.
Google & Youtube NCR for Google Chrome
// .com and .sa to US
var google = "http://www.google.com",
googleus = encodeURI("?gl=us&hl=en&pws=0&gws_rd=cr");
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: google + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1] + googleus};
},
{
urls: [
"*://www.google.com/",
"*://www.google.com.sa/",
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
// youtube.com to US
var youtube = "http://www.youtube.com",
youtubeus = encodeURI("?gl=US");
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {redirectUrl: youtube + details.url.match(/^https?:\/\/[^\/]+([\S\s]*)/)[1] + youtubeus};
},
{
urls: [
"*://www.youtube.com/",
],
types: ["main_frame"]
},
["blocking"]
);
/* google SA omni to US
var googleomni = encodeURI("&gl=us&hl=en&pws=0");
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var requestURL = details.url,
re = new RegExp('^(http(?:s)?):\/\/www\.google\.(?:(?!com\/)[a-z]+\.?[a-z]+)', 'i'),
finalurl = requestURL.replace(re, '$1://www.google.com');
if (details.url.indexOf(googleomni) > -1) {
return {
redirectUrl: finalurl
};
} else {
return {
redirectUrl: finalurl + googleomni
};
}
},
{
urls: [
"*://www.google.com.sa/",
],
types: ["main_frame"]
},
['blocking']
); */
// google.com omni to US
var googleomni = encodeURI("&gl=us&hl=en&pws=0");
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var requestURL = details.url;
if (details.url.indexOf(googleomni) > -1) {
return {
redirectUrl: requestURL
};
} else {
return {
redirectUrl: requestURL + googleomni
};
}
},
{
urls: [
"*://www.google.com/search?*",
],
types: ["main_frame"]
},
['blocking']
);
// google.com hp to US
var googlesahp = encodeURI("&sa=X" );
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var requestURL = details.url;
if (details.url.indexOf(googlesahp) > -1) {
return {
redirectUrl: details.url.replace(googlesahp, encodeURI("&gl=us") )
};
}
},
{
urls: [
"*://www.google.com/webhp*",
],
types: ["main_frame"]
},
['blocking']
);
// google images hp to US
var googleimages = encodeURI("?gl=us&hl=en");
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var requestURL = details.url;
if (details.url.indexOf(encodeURI("?gl=us&hl=en")) > -1) {
return {
redirectUrl: requestURL
};
} else {
return {
redirectUrl: details.url.replace(encodeURI("?hl=en"), googleimages)
};
}
},
{
urls: [
"*://www.google.com/imghp*",
],
types: ["main_frame"]
},
['blocking']
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment