【问题标题】:Change a variable with a chrome extension使用 chrome 扩展名更改变量
【发布时间】:2014-08-20 01:14:20
【问题描述】:

我经常使用的页面有一个有点无用的超时功能,这样一小时后它会自动将用户注销。我正在尝试为 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 小时。

【问题讨论】:

  • 查看@SeanDowney 在stackoverflow.com/questions/3847121/… 中的回答。解决方案是清除所有超时,因为您没有处理页面超时并且更改 x 可能不起作用,因为一旦您的脚本运行,超时很可能已经开始

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 2022-07-11
    • 2014-10-19
    • 2021-08-07
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多