I’m creating a small google chrome extension for website, and I want to change some html on particular pages.
The problem is that website load his content via ajax, and heavy use history.pushState API.
So, I added this thing to manifest:
"content_scripts": [
{
"matches": ["http://vk.com/friends"],
"js": ["js/lib/jquery.min.js", "js/friends.js"],
},
]
Everything works fine when I’m opening page first time or reloading it.
But when I’m navigating between website pages, chrome don’t insert my scripts on “/friends” page. I think this happening because the URL actually don’t changing. They use history.pushState() and so, chrome can’t insert/rerun my script again.
Is there any solution for this?
You can add a window.onpopstate event in content script and listen for it, when an event fires you can re-run content script again.
References
a) extension.sendMessage()
b) extension.onMessage().addListener
c) tabs.executeScript()
d) history.pushState()
e) window.onpopstate
Sample Demonstration:
manifest.json
Ensure content script injected URL and all API’s tabs have enough permission in manifest file
content_scripts.js
Track for onpopstate event and send a request to background page for rerun of script
background.js
Track for request from content script and execute script to the current page
rerunInjection.js
Some Trivial code
Output
Let me know if you need more information.