如何通过同步存储修改Google Chrome扩展的URL?

huangapple go评论54阅读模式
英文:

how to google chrome extension modify the URL from the sync storage?

问题

用例

当您使用多个Google帐户登录时,Google会在Google URL中添加一个查询参数authuser=X,如...<googleservice>.google.com/home/dashboard?authuser=1

我希望能够从扩展程序中设置authuser参数的默认值。

问题

作为一个Chrome扩展程序和前端的初学者,我不确定要使用哪个API来更新该值以平滑体验,而且无论用户是手动输入URL,还是我从另一个选项卡或程序中点击链接,URL都会被更新。

我目前的进展

目前,我有一个popup,其中有一个文本字段,它可以从storage.sync中读取。

如何通过同步存储修改Google Chrome扩展的URL?

问题

  1. 代码应该放在哪里? content_script 还是 service worker 或两者都是?
  2. 应该使用哪个API?
    1. window.location
    2. chrome.tabs
    3. declarativeNetRequest
    4. chrome.webRequest

感谢您的建议和帮助。

英文:

Use-case

When you log in with multiple Google accounts, google starts adding a query parameter authuser=X in Google URLs ...<googleservice>.google.com/home/dashboard?authuser=1

I want to be able to set the default value for the authuser parameter from the extension.

Problem

As a beginner with Chrome extensions and the frontend, I am unsure which API to use to update the value to smooth the experience and the URL is updated even if the user enters it manually or I click the link from another tab or program.

What I have so far

Right now I have popup with the text field it can read from the storage.sync.

如何通过同步存储修改Google Chrome扩展的URL?

Questions

  1. Where the code should live? content_script or `service worker or both?
  2. Which API to use?
    1. window.location
    2. chrome.tabs
    3. declarativeNetRequest
    4. chrome.webRequest

I appreciate your suggestions and help.

答案1

得分: 0

由于重定向规则是一个静态文本,只需要弹出窗口和declarativeNetRequest API,不需要内容脚本或后台脚本(服务工作者)。

manifest.json:

"permissions": ["declarativeNetRequestWithHostAccess"],
"host_permissions": ["*://*.google.com/"],
"action": {"default_popup": "popup.html"}

popup.html:

<input id=inp><button id=save>保存</button><div id=err></div>
<script src=popup.js></script>

popup.js:

document.getElementById('save').onclick = () => {
  chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [1],
    addRules: [{
      id: 1,
      condition: {
        resourceTypes: ['main_frame', 'sub_frame'],
        urlFilter: '||google.com/home/dashboard*',
      },
      action: {
        type: 'redirect',
        redirect: {
          transform: {
            queryTransform: {
              addOrReplaceParams: [{
                key: 'authuser',
                value: document.getElementById('inp').value,
              }],
            },
          },
        },
      },
    }],
  }).catch(e => {
    document.getElementById('err').textContent = e.message;
  });
};
英文:

Since the redirection rule is simple due to being a static text, you only need the popup and declarativeNetRequest API, no need for a content script or the background script (service worker).

manifest.json:

  &quot;permissions&quot;: [&quot;declarativeNetRequestWithHostAccess&quot;],
  &quot;host_permissions&quot;: [&quot;*://*.google.com/&quot;],
  &quot;action&quot;: {&quot;default_popup&quot;: &quot;popup.html&quot;}

popup.html:

&lt;input id=inp&gt;&lt;button id=save&gt;Save&lt;/button&gt;&lt;div id=err&gt;&lt;/div&gt;
&lt;script src=popup.js&gt;&lt;/script&gt;

popup.js:

document.getElementById(&#39;save&#39;).onclick = () =&gt; {
  chrome.declarativeNetRequest.updateDynamicRules({
    removeRuleIds: [1],
    addRules: [{
      id: 1,
      condition: {
        resourceTypes: [&#39;main_frame&#39;, &#39;sub_frame&#39;],
        urlFilter: &#39;||google.com/home/dashboard*&#39;,
      },
      action: {
        type: &#39;redirect&#39;,
        redirect: {
          transform: {
            queryTransform: {
              addOrReplaceParams: [{
                key: &#39;authuser&#39;,
                value: document.getElementById(&#39;inp&#39;).value,
              }],
            },
          },
        },
      },
    }],
  }).catch(e =&gt; {
    document.getElementById(&#39;err&#39;).textContent = e.message;
  });
};

huangapple
  • 本文由 发表于 2023年5月13日 15:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定