如何在Chrome扩展中编写一些JavaScript代码,以便如果运行javascript代码,并且用户按下后退按钮,它会返回两次? [英] How to write some javascript in a Chrome extension, so that if the javascript code is run, and the user presses the back button, it goes back twice?

查看:101
本文介绍了如何在Chrome扩展中编写一些JavaScript代码,以便如果运行javascript代码,并且用户按下后退按钮,它会返回两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,在我的Chrome扩展中,它会在域中的某些页面上创建一个重定向。这确实是我希望它做的行为。我不希望这改变 - 它是必需的。



我想要做的是,让用户按下后退按钮时,它会返回两次,当脚本运行 - 因此绕过这个问题。



见下面的例子:

1)用户访问 http://domain.com/page1.html

2)用户点击页面 http://domain.com/page2 .html

3)浏览器扩展脚本会导致它重定向到 http://domain.com/page2.html&aaa 马上。

4a)当用户按下一次按钮后,它会返回到 http://domain.com/page2.html ,然后将它们重定向回 http://domain.com/page2.html&aaa ,令他们感到沮丧。

4b)当用户连续按两次按钮,它可以回到 http://domain.com/page1.html ,就像他们打算这样做。

我想要做的就是添加一些能够导致重定向的页面,这样,当用户点击时返回按钮一次,返回两次。




更新:


以下是我正在处理的内容:

  function backTwo()
{
window.history.go(-2)
}

if {
[某些代码表明网址中的字符串和aaa存在]
}

然后{
[有些代码表示运行backTwo()函数]
}

else {
[有些代码说SA没有]
}

我不确定要做什么XT。我并不是一个真正的JavaScript编码器,我只是想把这个功能添加到扩展中。



如果你能给我一个例子,它会帮助很多。

解决方案

不用使用背景页面来修改历史导航行为,首先创建历史记录。



例如,通过使用 location.replace 内容脚本

  //在http://domain.com/page2.html上运行
location.replace('http://domain.com/page2.html&aaa');

清单文件:

 content_scripts:[{
matches:[*://domain.com/page2.html],
js:[redirect.js ],
run_at:document_start,
all_frames:true
}],

编辑

您声称 location.replace 无法从中获取资源服务器。这不是真的。您遇到的情况是您的网页由浏览器缓存 。为了确保页面从服务器重新加载,请使用 cache busting 方法,例如将随机查询字符串参数附加到您的网址:

  //在http://domain.com/page2.html上运行
location.replace('http://domain.com/page2.html?aaa&_t='+ new 。日期()的getTime());

在前面的例子中,您可以导航到 http:// domain。 ?COM / page2.html AAA&安培; _t = 1234 ... 1234 ... 的值是相当独特的,所以这个页面永远不会被你的浏览器的本地缓存所取代。



你是否控制服务器?如果是的话,你也可以通过标题禁用缓存。有关此主题的更多信息,请参见确保一个网页不会在所有浏览器中被缓存


Basically, in my chrome extension, it creates a redirect on certain pages within a domain. This is indeed the behavior I want it to be doing. I do not want this changed - it is required.

What I want to do instead, is to have it so that when the user presses the back button, it goes back twice when the script is run - hence circumventing the issue.

See below for an example:

1) User visits http://domain.com/page1.html
2) User clicks on the page http://domain.com/page2.html
3) Browser extension script causes it to redirect to http://domain.com/page2.html&aaa immediately.
4a) When user presses back button once, it goes back to http://domain.com/page2.html which then redirects them back to http://domain.com/page2.html&aaa, making them frustrated.
4b) When user presses back button twice in a row, it goes back to http://domain.com/page1.html like they had intended to do.

What I want to do, is add something that causes it so that whatever pages the javascript which causes the redirect is run on, also makes it so that when a user clicks the back button once, it goes back twice.


Update:

Here is what I am working on:

function backTwo()
  {
  window.history.go(-2)
  }

if {
  [SOME CODE TO SAY THAT THE STRING &aaa EXISTS IN THE URL]
}

then {
  [SOME CODE TO SAY RUN backTwo() FUNCTION]
}

else {
  [SOME CODE TO SAY DO NOTHING]
}

I'm not really sure what to do next. I'm not really a javascript coder, I'm just trying to add this feature to the extension.

If you could give me an example, it would help a lot.

解决方案

Instead of using background pages to modify the history navigation behaviour, avoid the problem by not creating the history entry in the first place.

For example, by using location.replace in a content script:

// Running on http://domain.com/page2.html
location.replace('http://domain.com/page2.html&aaa');

Manifest file:

  "content_scripts": [{
      "matches": ["*://domain.com/page2.html"],
      "js": ["redirect.js"],
      "run_at": "document_start",
      "all_frames": true
  }],

EDIT:
You alleged that location.replace does not fetch the resource from the server. That's not true. What you're experiencing is that your page was cached by the browser. To make sure that the page is reloaded from the server, use a cache busting method, such as appending a random query string parameter to your URL:

// Running on http://domain.com/page2.html
location.replace('http://domain.com/page2.html?aaa&_t=' + new Date().getTime());

In the previous case, you would navigate to http://domain.com/page2.html?aaa&_t=1234.... The value of 1234... is reasonably unique, so the page is never fetched your browser's local cache.

Do you control the server? If yes, you can also disable the cache through headers. For more information on this subject, see Making sure a web page is not cached, across all browsers.

这篇关于如何在Chrome扩展中编写一些JavaScript代码,以便如果运行javascript代码,并且用户按下后退按钮,它会返回两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆