我正在使用带有此扩展程序的Google Chrome - Open Link in Same Tab。
它很简单,基本上就是它所说的,强制所有链接在同一个窗口/标签中打开。我需要此功能的触摸屏信息亭左/右滑动导航。
该插件在链接方面效果很好。但是,当在“提交”中执行搜索时,它无法正常工作。网络搜索表单。大概是因为扩展程序强制_blank
中的任何内容在_top
中打开,但它忽略了forms
。
如何修改扩展程序,使其也考虑到表单,并强制它们也在同一个选项卡中打开?
我已下载扩展程序并查看了代码,主js似乎位于sametab.js
文件中。我在下面列出了这个,我确定我可以以某种方式修改它以满足我的需求。
感谢任何帮助。
"use strict";
// "_blank|_self|_parent|_top|framename"; "framename" should not start with "_"
// - list of iframe names only contains the names of iframes and not the names
// of windows in other tabs that could be targets
// - list of iframe names is not updated if an iframe's name changes
(function() {
var sameTab = {
converted: false,
observer: null,
iframeNameList: [],
index: -1,
mutationObserverFunction: function(mutations, observer) {
sameTab.observer.disconnect();
mutations.forEach(function(mutation) {
var i, node, target;
target = mutation.target;
if (!document.contains(target)) return;
switch (mutation.type) {
case "attributes":
if (mutation.attributeName !== "target" ||
target.tagName !== "A") return;
target.onclick = (target.target === "" ||
target.target[0] === '_') ? "" : sameTab.doNamedTarget;
if (target.target.toLowerCase() !== "_blank" ||
mutation.oldValue === "_top") return;
target.target = "_top";
break;
case "childList":
for (i = 0; i < mutation.addedNodes.length; i++) {
node = mutation.addedNodes[i];
if (node.parentNode !== target || node.nodeType !=
document.ELEMENT_NODE) continue;
sameTab.convertLinks(node);
}
break;
}
});
sameTab.observeDocument();
},
observeDocument: function() {
sameTab.observer.observe(document, {
childList: true,
subtree: true,
characterData: false,
attributes: true,
attributeOldValue: true,
attributeFilter: ["target"]
});
},
convertDocument: function(eventObject) {
sameTab.convertLinks(document);
sameTab.observer = new MutationObserver(sameTab.mutationObserverFunction);
sameTab.observeDocument();
sameTab.converted = true;
},
// When a link with a named target is clicked, change the target to "_top"
// unless the name is in the list of iframe names.
doNamedTarget: function(eventObject) {
// First make sure the iframe's own name is correct.
if (sameTab.index !== -1) {
sameTab.iframeNameList[sameTab.index] === window.name;
}
// Do nothing if the target name is in the list of window names.
if (sameTab.iframeNameList.indexOf(eventObject.target.target) !== -1) return;
eventObject.target.target = "_top";
},
// If the link target is "_blank" change it to "_top". If it is a name
// which does not begin with "_" set the link's click event handler so
// the list of iframe names can be checked for the target when the link is
// clicked.
convertLinks: function(node) {
var i, linkElements;
linkElements = node.querySelectorAll("a[target]");
for (i = 0; i < linkElements.length; i++) {
if (linkElements[i].target === "") continue;
if (linkElements[i].target[0] !== "_") {
linkElements[i].onclick = sameTab.doNamedTarget;
} else if (linkElements[i].target.toLowerCase() === "_blank") {
linkElements[i].target = "_top";
}
}
if (node.tagName !== "A" || node.target === "") return;
if (node.target[0] !== "_") {
node.onclick = sameTab.doNamedTarget;
} else if (node.target.toLowerCase() === "_blank") {
node.target = "_top";
}
}
};
var frame = null;
if (window === top) {
// Top frame
frame = {
iframeList: [],
convertAllLinks: false,
hostname: null,
// Delete an item from the list of iframes.
removeDeletedIframes: function(source) {
var i;
for (i = frame.iframeList.length - 1; i >= 0; i--) {
if (frame.iframeList[i].source && (!source ||
frame.iframeList[i].source !== source)) continue;
frame.iframeList.splice(i, 1);
sameTab.iframeNameList.splice(i, 1);
}
},
sendIframeList: function(source) {
var i, len, origin;
// First remove any deleted iframes from the lists.
frame.removeDeletedIframes(source);
len = frame.iframeList.length;
for (i = 0; i < len; i++) {
origin = (frame.iframeList[i].origin === "null") ?
"*" : frame.iframeList[i].origin;
frame.iframeList[i].source.postMessage({
senderId: "sameTabExtensionTop",
message: "nameList",
iframeNameList: sameTab.iframeNameList,
index: i,
}, origin);
}
},
checkLists: function(items) {
var i;
if (!items.settingsInitialized) {
console.warn("Stored data missing.");
window.removeEventListener("message", frame.windowMessages, false);
frame.iframeList.length = 0;
sameTab.iframeNameList.length = 0;
} else if (!items.convertLinks ||
(items.useWhitelist &&
items.whitelist.indexOf(frame.hostname) == -1) ||
items.blacklist.indexOf(frame.hostname) != -1) {
window.removeEventListener("message", frame.windowMessages, false);
frame.iframeList.length = 0;
sameTab.iframeNameList.length = 0;
} else {
frame.convertAllLinks = true;
frame.sendIframeList();
if (document.readyState === "interactive" ||
document.readyState === "complete") {
sameTab.convertDocument(null);
} else {
document.addEventListener("DOMContentLoaded",
sameTab.convertDocument, false);
}
}
},
getHostname: function() {
switch (location.protocol) {
case "file:":
return "file://" + location.hostname + location.pathname;
break;
case "http:":
case "https:":
return location.hostname;
break;
default:
return null;
break;
}
},
windowMessages: function(eventObject) {
var i, len;
if (!eventObject.data ||
eventObject.data.senderId !== "sameTabExtensionIframe") return;
switch (eventObject.data.message) {
case "windowUnloaded":
frame.sendIframeList(eventObject.source);
break;
case "contentLoaded":
if (!eventObject.source ||
eventObject.source.top !== window) return;
len = frame.iframeList.length;
for (i = 0; i < len; i++) {
if (frame.iframeList[i].source === eventObject.source) break;
}
frame.iframeList[i] = eventObject;
sameTab.iframeNameList[i] = eventObject.data.frameName;
if (!frame.convertAllLinks) return;
frame.sendIframeList(null);
break;
}
}
};
frame.hostname = frame.getHostname();
if (frame.hostname) {
window.addEventListener("message", frame.windowMessages, false);
chrome.storage.local.get(null, frame.checkLists);
}
} else {
// Iframes
frame = {
// Accept messages from the top window only.
windowMessages: function(eventObject) {
if (eventObject.source !== top) return;
if (!eventObject.data ||
eventObject.data.senderId !== "sameTabExtensionTop" ||
eventObject.data.message !== "nameList" ||
!eventObject.data.iframeNameList) return;
sameTab.iframeNameList = eventObject.data.iframeNameList;
sameTab.index = eventObject.data.index;
if (sameTab.converted) return;
sameTab.convertDocument(null);
},
// Tell top window that the window has unloaded
windowUnload: function(eventObject) {
var origin;
try {
origin = top.location.origin;
} catch(err) {
origin = "*";
}
top.postMessage({
senderId: "sameTabExtensionIframe",
message: "windowUnloaded",
}, origin);
},
// Post the window's name to the top window.
contentLoaded: function(eventObject) {
var origin;
try {
origin = top.location.origin;
} catch(err) {
origin = "*";
}
top.postMessage({
senderId: "sameTabExtensionIframe",
message: "contentLoaded",
frameName: window.name
}, origin);
}
};
window.onunload = frame.windowUnload;
window.addEventListener("message", frame.windowMessages, false);
document.addEventListener("DOMContentLoaded", frame.contentLoaded, false);
}
}());
答案 0 :(得分:1)
只需在(function() {
var submitButtons = document.getElementsByTagName("button");
for (var i = 0; i < submitButtons .length; i++) {
submitButtons[i].setAttribute("target", "_top");
}
答案 1 :(得分:0)
您可以尝试设置querySelectorAll以查找表单。我非常确定表单也支持target
。