Chrome阻止跳转
插件是可以阻止页面跳转的
正确的做法
chrome.webNavigation.onBeforeNavigate.addListener((details) => {
//尝试阻止tab跳转url.
if(0<luoism.downtabid) {
log('navigation阻止下载'+JSON.stringify(details));
chrome.tabs.executeScript(luoism.downtabid,{code: 'window.stop()'});
}
});
错误的做法
chrome.tabs.onUpdated.addListener(function(tabid,changeinfo,tab) {
//尝试阻止tab跳转url. 并不能真正阻止跳转.
if("loading"==changeinfo.status&&0<luoism.downtabid) {
log('onUpdated阻止下载'+decodeURIComponent(JSON.stringify(changeinfo)));
chrome.tabs.executeScript(luoism.downtabid,{code: 'window.stop()'});
return;
}
}
page变化的api
//content端
//1. MutationObserver (docs) to literally detect DOM changes
//2. Event listener for sites that signal content change by sending a DOM event
//3. Periodic checking of DOM via setInterval
//Cloaking History API inside an injected DOM script:
document.head.appendChild(document.createElement('script')).text = '(' +
function() {
// injected DOM script is not a content script anymore,
// it can modify objects and functions of the page
var _pushState = history.pushState;
history.pushState = function(state, title, url) {
_pushState.call(this, state, title, url);
window.dispatchEvent(new CustomEvent('state-changed', {detail: state}));
};
// repeat the above for replaceState too
} + ')(); this.remove();'; // remove the DOM script element
// And here content script listens to our DOM script custom events
window.addEventListener('state-changed', function(e) {
console.log('History state changed', e.detail, location.hash);
doSomething();
});
//Listening to hashchange, popstate events:
window.addEventListener('hashchange', function(e) {
console.log('URL hash changed', e);
doSomething();
});
window.addEventListener('popstate', function(e) {
console.log('State changed', e);
doSomething();
});
//back端:
There are advanced API to work with navigation: webNavigation, webRequest
var rxLookfor = /^https?:\/\/(www\.)?google\.(com|\w\w(\.\w\w)?)\/.*?[?#&]q=/;
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (rxLookfor.test(changeInfo.url)) {
chrome.tabs.sendMessage(tabId, 'url-update');
}
});
变化url, 但是不做reload
//实话说, 这么骚的操作, 真不知道用到哪里好, 先记录一下吧:
window.history.pushState(“object or string”, “Title”, “/new-url”);
//监控这种变化:
chrome.webNavigation.onHistoryStateUpdated.addListener(function(e) {
console.log(e);
}, {url: [{hostEquals: 'yourwebsite.com'});
- https://stackoverflow.com/questions/38311437/detect-this-event-on-chrome-change-of-url-change-without-reloading
页面变化参考:
- https://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener/39508954#39508954
- https://stackoverflow.com/questions/40423835/chrome-extension-webnavigation-listener-for-hash-change
阻止跳转参考:
- https://stackoverflow.com/questions/7387152/modify-url-location-in-chrome-extensions-stop-the-initial-request
- https://developer.chrome.com/extensions/webNavigation
- https://support.google.com/chrome/a/answer/7532419?hl=en
- https://github.com/gorhill/uBlock
- https://stackoverflow.com/questions/38619308/how-to-block-a-tab-from-opening-a-page-on-webnavigation-onbeforenavigate-event
- 特别要注明一下这个问题, 这个题主很无耻.