使用chrome扩展名更改变量

时间:2014-08-20 01:14:21

标签: javascript google-chrome variables google-chrome-extension content-script

我经常使用的页面有一个轻微无用的超时功能,这样一小时后它会自动将用户注销。我试图为chrome编写一个扩展,将这个超时时间延长到十个小时,但是因为我在遇到问题之前从未写过扩展。我知道内容脚本不能直接影响页面变量,但我一直在尝试使用Method 2 on this page,但我无法开始工作。

网站上的超时代码是基本的:

var x = 3600
var y = 1;
var z = 300
    x = x-y
    setTimeout("startClock()", 1000)
function startClock(){
    x = x-y
    setTimeout("startClock()", 1000)
if(x==z){
alert("Your Session is about to expire. After a period of inactivity (60 minutes), all current session information is removed and you will be required to log in again.");
} else if (x==0) {
document.location.href = "content?module=home&page=homepg&logoutFinal=1";
}

我为使用content_scripts来调用contentscript.js的扩展程序编写了一个基本清单文件,这与Method 2 example.

中提供的内容几乎相同
var actualCode = ['var x = 36000'].join('\n');

var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);

我目前的愿望是拥有一个扩展程序,可以自动将x的值重置为36000秒,或者在页面刷新后10小时。

1 个答案:

答案 0 :(得分:0)

经过一段时间的故障排除后,我发现整个问题都是manifest.json中的问题,特别是匹配部分。上面给出的代码是有效的,我链接到的文档中给出的方法也是如此。

感谢devnull69的快速响应,我尝试了这种方法,在解决manifest.json中的问题后,它完全消除了超时。