Chrome扩展程序重定向到带有参数的网址 [英] Chrome Extension to Redirect to URL with Parameter

查看:392
本文介绍了Chrome扩展程序重定向到带有参数的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个Chrome扩展程序,该扩展程序会在URL的末尾匹配给定模式( *://*.mydomain.com/s/* )。下面是我有的清单文件和后台脚本,但我无法使它工作。我做错了什么?

I'm attempting to create a Chrome extension which will add a parameter to the end of a URL if the URL matches a given pattern (*://*.mydomain.com/s/*). Below is the manifest file and background script I have, but I cannot get it working. What am I doing wrong?

manifest.json:

manifest.json:

{
  "manifest_version": 2,
  "name": "Search Grid View",
  "version": "0.1",
  "description": "Changes MyDomain.com search to grid view by default",

  "background": {
     "scripts": ["background.js"]
  },

  "permissions": [
    "tabs",
    "webRequest",
    "*://*.mydomain.com/s/*",
    "webRequestBlocking"
  ]

}

background.js:

background.js:

chrome.webRequest.onBeforeRequest.addListener(
  function(details) {         
    var currentUrl = tabs[0].url;
    var newUrl = currentUrl + "&style=gridview"
    return { redirectUrl: newUrl};
  },
  {
    urls: [
      '*://*.mydomain.com/s/*'
    ],
    types: ['main_frame']
  },
  ['blocking']);

在此先感谢您的任何建议!

Thanks in advance for any advice!

推荐答案


  1. 使用调试器 - 在chrome:// extensions页面上点击你的扩展的后台页面,并切换到来源面板。

  2. 要使用 onBeforeRequest 的回调参数

  3. 检查网址是否已被修改

  1. Use debugger - click your extension's background page on chrome://extensions page and switch to the Sources panel.
  2. To obtain the url use onBeforeRequest's callback parameter
  3. Check if the url is already modified.

>





chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        return {
            redirectUrl: details.url + 
                (details.url.indexOf("?") == -1 ? "?" : "") +
                (details.url.indexOf("&style=gridview") == -1 ? "&style=gridview" : "")
        };
    },
    {urls: ['*://*.mydomain.com/s/*'], types: ['main_frame']},
    ['blocking']
);

这篇关于Chrome扩展程序重定向到带有参数的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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