コンテンツスクリプトにwindow.onpopstateイベントを追加してリッスンできます。イベントが発生したら、コンテンツスクリプトを再実行できます。
参考文献
a) extension.sendMessage()
b) extension.onMessage()。addListener
c) tabs.executeScript()
d) history.pushState()
e) window.onpopstate
サンプルデモンストレーション:
マニフェスト.json
コンテンツスクリプトが挿入されたURLと、すべてのAPIのタブがマニフェストファイルで十分な権限を持っていることを確認します
{
"name": "History Push state Demo",
"version": "0.0.1",
"manifest_version": 2,
"description": "This demonstrates how push state works for chrome extension",
"background":{
"scripts":["background.js"]
},
"content_scripts": [{
"matches": ["http://www.google.co.in/"],
"js": ["content_scripts.js"]
}],
"permissions": ["tabs","http://www.google.co.in/"]
}
content_scripts.js
onpopstateイベントを追跡し、スクリプトの再実行のためにバックグラウンドページにリクエストを送信します
window.onpopstate = function (event) {
//Track for event changes here and
//send an intimation to background page to inject code again
chrome.extension.sendMessage("Rerun script");
};
//Change History state to Images Page
history.pushState({
page: 1
}, "title 1", "imghp?hl=en&tab=wi");
background.js
コンテンツスクリプトからのリクエストを追跡し、スクリプトを実行して現在のページに移動します
//Look for Intimation from Content Script for rerun of Injection
chrome.extension.onMessage.addListener(function (message, sender, callback) {
// Look for Exact message
if (message == "Rerun script") {
//Inject script again to the current active tab
chrome.tabs.executeScript({
file: "rerunInjection.js"
}, function () {
console.log("Injection is Completed");
});
}
});
rerunInjection.js
いくつかの些細なコード
console.log("Injected again");
出力

さらに情報が必要な場合はお知らせください。