Possible approach:
One approach could be to create an extension that injects script content on each page. This content script will parse the DOM and remove all target attributes from the anchor elements and set _self to all target attributes of the anchor _self .
Warning:
- Many pages dynamically insert elements (including anchor elements).
- Some pages dynamically change elements (including anchor elements).
- Not all pages / tabs are opened by links (for example,
window.open() can be used on one page).
Decision:
You can use MutationObserver , which monitors the insertion of anchor elements or has the target attribute to change and make appropriate adjustments.
You still need to take care of the tabs opened by other means (for example, window.open() ) if this is very important for you (but these cases should be very few, so this may not be worth the trouble).
Code example:
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 });
source share