在我的chrome扩展中,将contenscript注入页面不起作用? [英] In my chrome extension, Injection of contenscript into a page not working?
问题描述
我将contentcript注入页面:
I injected contentscript into a page by :
//this is in background.js
chrome.tabs.executeScript(null, {"file": "contentscript.js"});
然后检查它是否插入良好我在页面上修改了一个元素,但没有做任何更改。
我正确地注射了吗?
这是我的档案:
** background.js :**
Then to check if it inserted well or not I modified an element on the page, but no changes made.
Did I inject it properly?
Here are my files :
**background.js:**
//injecting contentscript here
chrome.tabs.executeScript(null, {"file": "contentscript.js"});
//receiving end
var title;
chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
if(message.method == 'setTitle')
title = message.title;
else if(message.method == 'getTitle')
sendResponse(title);
});
** contentscript.js:**
**contentscript.js:**
<script>
var table = document.getElementsByClassName("collapse")[0];
var marks = new Array();
for(var i=0;i<8;i++)
marks[i] = table.children[0].children[i+1].children[5].innerHTML;
var total=0;
for(var i=0;i<8;i++)
total += Number(marks[i]);
//code to send the data to background.js
var fromDOM = total;
chrome.runtime.sendMessage({method:'setTitle',title:fromDOM});
</script>
** popup.js:**
**popup.js:**
var total= 0,percentage = 0;
chrome.runtime.sendMessage({method:'getTitle'}, function(response){
$('.total').text(response);
percentage = total/7.25;
$('.percentage').text(percentage);
});
为什么这不起作用?有任何变化或错误吗?
**这就是我的popup.html的样子:**
http://i.stack.imgur.com/lrKBZ.png
Why is this not working? Any changes or errors?
**This is how my popup.html is looking :**
http://i.stack.imgur.com/lrKBZ.png
推荐答案
(' .total')。text(response);
百分比=总数/ 7. 25 ;
('.total').text(response); percentage = total/7.25;
(' 。percentage')。text(percentage);
});
('.percentage').text(percentage); });
为什么这不起作用?有任何变化或错误吗?
**这就是我的popup.html的样子:**
http://i.stack.imgur.com/lrKBZ.png
Why is this not working? Any changes or errors?
**This is how my popup.html is looking :**
http://i.stack.imgur.com/lrKBZ.png
I got the problem solved.
I've just moved the code to insert contenscript from background.js to popup.js. Actually, when it is in background.js , it is getting inserted only once, when I moved it to popup.js, it is getting inserted everytime the popup is clicked and is active.
Thanks
这篇关于在我的chrome扩展中,将contenscript注入页面不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!