Chrome扩展程序 – 内容脚本 – 我如何编写一个简单的内容脚本,将在每个页面上执行一个类似
javascript的警报(“hello”)加载..我的意思是当我去一些页面,如google.com的消息应该出现..或者如果我重新加载任何页面,消息应该显示为Im newvbiew.请帮忙
我到目前为止有这个json文件
- {
- "name": "Highlight some phrases","description": "Hightlight some pre defined text from websql database after page loads","version": "0.1","permissions": [
- "tabs","<all_urls>"
- ],"browser_action": {
- "default_icon": "icon.png","default_popup": "popup.html"
- },"content_scripts": [
- {
- "matches": [
- "http://*/*","https://*/*"
- ],"js": ["content.js"]
- }
- ],"background": {
- "page": "background.html"
- },"manifest_version": 2
- }
解决方法
如果您需要的是在每个加载的页面上提醒你,下面是一个简单的演示:
manifest.json的:
- {
- "name": "Highlight some phrases","browser_action": {
- "default_icon": "icon.png"
- },"content_scripts": [
- {
- "matches": [
- "http://*/*","https://*/*"
- ],"js": ["content.js"],"run_at": "document_end" // pay attention to this line
- }
- ],"manifest_version":2
- }
这里是content.js中的内容:
- // alert("hello");
- document.body.style.background = 'yellow';