How to disable chrome from opening up "new window" and "tabs"?
Possible approach:
One approach could be building an extension that injects a content script in every page. This content script would parse the DOM and remove all and set all target
attributes off the anchor elementstarget
attributes of anchor elements to _self
.
Caveats:
- There are dynamically inserted elements (including anchor elements) on many pages.
- There are dynamically changing elements (including anchor elements) on some pages.
- Not all pages/tabs are opened through links (e.g. some page could use
window.open()
).
Solution:
You could use a MutationObserver that watches for anchor elements being inserted or having their target
attribute modified and make the appropriate adjustments.
You still need to take care of tabs opened by other means (e.g. window.open()
) if it is extremely important to you (but those cases should be very very few, so it might not be worth the trouble).
Sample code:
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"content_scripts": [{
"matches": ["*://*/*"],
"js": ["content.js"],
"run_at": "document_start",
"all_frames": true
}]
}
content.js:
/* Define helper functions */
var processAnchor = function(a) {
//if (a.hasAttribute('target')) {
// a.removeAttribute('target');
//}
a.setAttribute('target', '_self');
};
/* Define the observer for watching over inserted elements */
var insertedObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(m) {
var inserted = [].slice.call(m.addedNodes);
while (inserted.length > 0) {
var elem = inserted.shift();
[].slice.call(elem.children || []).forEach(function(el) {
inserted.push(el);
});
if (elem.nodeName === 'A') {
processAnchor(elem);
}
}
});
});
/* Define the observer for watching over
* modified attributes of anchor elements */
var modifiedObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(m) {
if ((m.type === 'attributes') && (m.target.nodeName === 'A')) {
processAnchor(m.target);
}
});
});
/* Start observing */
insertedObserver.observe(document.documentElement, {
childList: true,
subtree: true
});
modifiedObserver.observe(document.documentElement, {
attributes: true,
substree: true
});
Oneezy
BIO I'm a self-taught Interactive Designer with a deep understanding of the web. Growing up I was influenced by comic books, street art, skateboarding and video games- which led me to design, development and digital effects. My knowledge of web techniques and attention to detail give me the skills to quickly create intuitive/ pixel perfect interfaces that respond fluidly to any device- Wearables, Mobile, Tablet, Desktop and whatever is to come next. I have a keen eye for usability and passionate about user experience- Design Flow, Performance, and Optimization. FINAL THOUGHTS I'm a forward thinker with big ideas and strive for the best in every project! In the future I’ll stay on top of the latest web trends while keeping a high focus on hybrid app development (mobile + web). Basically, I love the combination of design and cutting-edge technology!
Updated on July 09, 2022Comments
-
Oneezy almost 2 years
Is their a way to keep all pages on the internet in a single window through Chromes browser settings? or a addon/plugin that I can do this with?
I don't want web pages to open up in new tabs and/or new windows when I click on some links.
Let me know if anyone has any suggestions!, Thanks!!
<a href="http://www.bing.com" target="_blank">Opens a New Tab!</a> <i>Thtats not what i want..., I want this link to stay in same url bar.</i> <p>Like this!</p> <a class="click" href="http://www.bing.com">Click Here</a>
-
Oneezy over 10 yearsThankyou for your great answer. Your talking about removing the targe=_blank's from external websites with javascript? I dont think that's possible due to security purposes. To better explain my problem have a look at my first question I asked earlier: stackoverflow.com/questions/20601259/…
-
Oneezy over 10 yearsAnd this is my 2nd question I asked earlier: stackoverflow.com/questions/20604534/… ,,,,, as you can see this problem is being really troublesome for me ;)
-
Oneezy over 10 yearsmy first approach was to use iframes and have a bottom navigation for my web app but because of the external target _blanks inside other web pages i didnt have any control over stopping the new window/tab from being opened. And its a must have requirement for this app that im creating because it will be for touch screen kiosk.. so web browsing has 2b limited
-
gkalpak over 10 yearsYou aksed if you can achive this using an extension. The answer is that you can and I also gave you some sample code that achieves that. (I slightly modified the code to make sure links are opened in their iframe (if any).) I don't really understand your comments. Please, try the proposed solution above and report back if you encounter any problems. (For me at least, it works fine, both for links in iframes and outside.)
-
Oneezy over 10 yearsahh, cool. Ok, Let me dig into this a bit and I'll let you know my findings. sorry.. its a bit late here if im not making much sense lol.. I'll have a look at this tomorrow. thanks a bunch
-
gkalpak over 10 years@MrRioku: If you feel this answer adequately address the question, consider marking it as accepted. In any case, feel free to upvote it if you liked it :)
-
Cris about 10 yearsahahaha. WHy not just use the chrome.tabs API from a background page, and hook into the event when a new tab is opened and close it?
-
gkalpak about 10 yearsahahaha. BEcause (among other reasons) the background-script would not know what tab/iframe opened the tab in order to reopen the URL inside that tab/iframe and because the user would see a tab being opened and then closed and it is a bad UX.