![](/img/trans.png)
[英]Can I get the address bar url from the javascript console when the page has failed to load?
[英]How can I get the url of a page that didn't load with javascript?
我正在嘗試為Firefox編寫一個插件,如果URL包含任何給定的字符串,該插件會將您重定向到另一個網站。 這適用於正確加載的頁面,但會說如果頁面未加載(可能被阻止等),我的代碼將無法獲取URL。
我嘗試了window.location.href
但這在卸載的頁面上不起作用。
對於加載的頁面, window.location.href
起作用。 我希望即使在無法加載的頁面上也能獲取URL。
長話短說,我想知道如何使用javascript獲取此頁面的URL? http://www.randomrandomrandomrandom.com/
我認為您可能想研究Mozilla文檔中的tabs.getCurrent函數。 如您所說, window.location.href
僅在頁面加載后才起作用,所以您要在頁面加載之前檢查URL,因為您不確定是否會加載。
function getURL(tabInfo) {
console.log(tabInfo.url);
}
function logError(error) {
console.log(`Error: ${error}`);
}
var gettingCurrent = browser.tabs.getCurrent();
gettingCurrent.then(getURL, logError);
僅在擴展名具有“選項卡”權限時,URL選項才存在。 ( 來源 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.