英文:
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
中读取。
问题
- 代码应该放在哪里?
content_script
还是service worker
或两者都是? - 应该使用哪个API?
- window.location
- chrome.tabs
- declarativeNetRequest
- 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
.
Questions
- Where the code should live?
content_script
or `service worker or both? - Which API to use?
- window.location
- chrome.tabs
- declarativeNetRequest
- 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:
"permissions": ["declarativeNetRequestWithHostAccess"],
"host_permissions": ["*://*.google.com/"],
"action": {"default_popup": "popup.html"}
popup.html:
<input id=inp><button id=save>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;
});
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论