有没有办法让我的OWN浏览器(Chrome)无法返回/转发/刷新?
这种情况经常发生,当我在devtools中开发和玩游戏时(改变HTML和CSS只是为了尝试一下)我有时会不小心刷回或者不习惯刷新。我希望能够通过某种扩展来禁用后退或前进按钮吗?
我不是要在任何实时网站上禁用该按钮,只是为了我本地。有什么想法吗?
答案 0 :(得分:7)
如果您想防止意外导航,则无需安装任何扩展程序。只需打开控制台,然后运行以下代码:
window.onbeforeunload = function() {
return 'Want to unload?';
};
使用此代码,您将收到确认提示。
如果您确实希望阻止页面通过扩展程序卸载,请使用How to cancel webRequest silently in chrome extension的答案中描述的技术。
这是一个最小的演示扩展,可以为您的浏览器添加一个按钮。点击后,您无法再导航到其他页面。您仍然可以在没有任何警告的情况下关闭选项卡:
// background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.webRequest.onBeforeRequest.addListener(function(details) {
var scheme = /^https/.test(details.url) ? 'https' : 'http';
return { redirectUrl: scheme + '://robwu.nl/204' };
// Or (seems to work now, but future support not guaranteed):
// return { redirectUrl: 'javascript:' };
}, {
urls: ['*://*/*'],
types: ['main_frame'],
tabId: tab.id
}, ['blocking']);
});
manifest.json用于此扩展名:
{
"name": "Never unload the current page any more!",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": true
},
"browser_action": {
"default_title": ""
},
"permissions": [
"<all_urls>",
"webRequest",
"webRequestBlocking"
]
}