Skip to content

Instantly share code, notes, and snippets.

@yakkomajuri
Last active April 21, 2024 19:59
Show Gist options
  • Save yakkomajuri/cad311c02264d0a18c619f321f57d897 to your computer and use it in GitHub Desktop.
Save yakkomajuri/cad311c02264d0a18c619f321f57d897 to your computer and use it in GitHub Desktop.
/*
This event triggers when the browser has committed to loading a webpage.
As opposed to e.g. webNavigation.onCompleted, this will start to run early
so that we can begin to remove ads as soon as possible.
*/
chrome.webNavigation.onCommitted.addListener(function (tab) {
// Prevents script from running when other frames load
if (tab.frameId == 0) {
chrome.tabs.query({ active: true, lastFocusedWindow: true }, tabs => {
// Get the URL of the webpage
let url = tabs[0].url;
// Remove unnecessary protocol definitions and www subdomain from the URL
let parsedUrl = url.replace("https://", "")
.replace("http://", "")
.replace("www.", "")
// Remove path and queries e.g. linkedin.com/feed or linkedin.com?query=value
// We only want the base domain
let domain = parsedUrl.slice(0, parsedUrl.indexOf('/') == -1 ? parsedUrl.length : parsedUrl.indexOf('/'))
.slice(0, parsedUrl.indexOf('?') == -1 ? parsedUrl.length : parsedUrl.indexOf('?'));
try {
if (domain.length < 1 || domain === null || domain === undefined) {
return;
} else if (domain == "linkedin.com") {
runLinkedinScript();
return;
}
} catch (err) {
throw err;
}
});
}
});
function runLinkedinScript() {
// Inject script from file into the webpage
chrome.tabs.executeScript({
file: 'linkedin.js'
});
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment