コンテンツスクリプトにwindow.onpopstateイベントを追加して待機することができます。イベントが発生すると、コンテンツスクリプトを再度実行できます。
参照
A)extension.sendMessage()
B)extension.onMessage().addListener
C)tabs.executeScript()
D)history.pushState()
E)window.onpopstate
サンプルデモンストレーション:
manifest.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スクリプトの再実行のための背景ページに要求を送信
トラックコンテンツスクリプティングからのリクエスト用tと現在のページ
//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");
出力
は、あなたがより多くの情報が必要なら、私に教えてください。
、pushStateは発生しません。 "popstate"イベントなので、このコードは機能しません。 –