2016-09-21 16 views
2

我有一個Chrome擴展,我想操縱GitHub上的一些DOM。當我刷新任何給定的頁面時,所有工作都按預期工作,但如果我正常導航到該頁面,則通常不會執行腳本。Chrome擴展只能在刷新時運行,而不能在頁面導航上運行

manifest.json的

{ 
    "manifest_version": 2, 

    "name": "Name", 
    "description": "Detailed description", 
    "version": "1.3.5", 
    "content_scripts": [{ 
     "matches": ["https://github.com/*/*/issues/*"], 
     "js": ["jquery-2.1.0.min.js", "index.js"] 
    }], 
    "browser_action": { 
     "default_icon": "icon.png" 
    }, 
    "permissions": [ 
     "activeTab", 
     "https://ajax.googleapis.com/" 
    ] 
} 

index.js

$(document).ready(function() { 
    // DOM manipulating code 
    // removed for brevity 
}); 
+0

Github使用歷史API的網站導航,你將不得不掛鉤歷史pushState函數,[如何通過history.pushState得到關於歷史變化的通知?](http://stackoverflow.com/questions/4570093/how-to-get-notification-about-changes-of-the-history-via-history-pushstate) –

+0

@ZigMandel該帖子中的建議解決方案不適用於我的情況所以我開了一個新的問題。 – James

+0

@詹姆斯,如果你已經嘗試了一些東西,但它不起作用,那麼你應該在你的問題中提及它。不這樣做只會浪費你要求幫助你的人的時間。 – Makyen

回答

1

當你從另一個導航到GitHub的頁面,在該頁面的容器被更新,而不是整個頁面。

因此,從我的經驗來看......如果您從回購的主頁開始並切換到問題頁面,則擴展並不總是注意到這一變化。因此,我建議改變清單matches所有github上的,而不是僅僅的問題:

{ 
    "manifest_version": 2, 
    "name": "Name", 
    "description": "Detailed description", 
    "version": "1.3.5", 
    "content_scripts": [{ 
     "matches": ["https://github.com/*"], 
     "js": ["jquery-2.1.0.min.js", "index.js"] 
    }], 
    "browser_action": { 
     "default_icon": "icon.png" 
    }, 
    "permissions": [ 
     "activeTab", 
     "https://ajax.googleapis.com/" 
    ] 
} 

如果不工作,那麼你的代碼中,監測一個「pjax:結束」的文檔事件:

document.addEventListener("pjax:end", function() { 
    // run code/call function 
}); 
+0

什麼時候發生「pjax:end」事件?我試着實現你的兩個建議,問題仍然存在 – James

+0

這個解決方案應該可以工作,我使用同樣的方法。 @詹姆斯,確保你已經重新加載了整個擴展和站點(用pjax:end重新輸入新的內容腳本)。 – wOxxOm

+0

GitHub完成將新內容加載到目標容器後觸發「pjax:end」。 – Mottie

相關問題