他们是否可以通过 Chrome 浏览器设置将互联网上的所有页面保留在一个窗口中?或者我可以使用的插件/插件?
当我点击某些链接时,我不希望在新选项卡和/或新窗口中打开网页。
如果有人有任何建议,请告诉我!谢谢!!
<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>
最佳答案
可能的方法:
一种方法可能是构建一个注入(inject) content script 的扩展程序。 在每个页面中。此内容脚本将解析 DOM 并删除 anchor 元素上的所有 并将 anchor 元素的所有 target
属性target
属性设置为 _self
.
注意事项:
- 许多页面上都有动态插入的元素(包括 anchor 元素)。
- 某些页面上存在动态变化的元素(包括 anchor 元素)。
- 并非所有页面/选项卡都通过链接打开(例如,某些页面可以使用
window.open()
)。
解决方案:
您可以使用 MutationObserver 监视 anchor 元素的插入或 target
属性的修改并进行适当的调整。
如果对您来说非常重要,您仍然需要处理通过其他方式打开的选项卡(例如 window.open()
)(但这些情况应该非常少,因此可能会不值得这么麻烦)。
示例代码:
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
});
关于google-chrome - 如何禁止chrome打开 "new window"和 "tabs"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20606225/