[英]Chrome Extension:redirect the current url with specific rules
I am a researcher and I want to access academic papers via my university.我是一名研究员,我想通过我的大学访问学术论文。 I discovered the I can modify the URL to achieve this.
我发现我可以修改 URL 来实现这一点。 The rules are replacing "."
规则正在替换“。” with "-" and adding ".proxy.findit.dtu.dk" to the domain.
使用“-”并将“.proxy.findit.dtu.dk”添加到域中。
For example: From: https://www.sciencedirect.com/science/article/pii/S0925838811021414例如:来自: https://www.sciencedirect.com/science/article/pii/S0925838811021414
to: https://www-sciencedirect-com.proxy.findit.dtu.dk/science/article/pii/S0925838811021414至: https://www-sciencedirect-com.proxy.findit.dtu.dk/science/article/pii/S0925838811021414
Inspired by this post , I modified the code to:受这篇文章的启发,我将代码修改为:
First file: manifest.json第一个文件:manifest.json
{ "manifest_version": 2, "name": "Redirect via DTU", "description": "This extension automatically replace '.' with '-' and adds the '.findit.dtu.dk' to the browser's address, allowing you to visit the databases bought by the library quickly", "version": "1.0", "browser_action": { "default_icon": "DTU.png", "default_title": "Redirect via DTU," }: "background":{ "scripts".["background,js"] }: "permissions", [ "activeTab", "tabs" ] }
Second file: popup.js第二个文件:popup.js
// Change the url to library when on click var l=location; l.href=l.href.replace(/\./g, "-") l.href=l.origin+l.href.replace(l.origin, '.proxy.findit.dtu.dk');
Third file: background.js第三个文件:background.js
//Wait for click chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript(null, { "file": "popup.js" }, function(){ "popup.js"; console.log("Script Executed..."); }); })
The last file I included is a figure: DTU.png.我包含的最后一个文件是一个图形:DTU.png。
However, it does not work.但是,它不起作用。 There is some problem with popup.js.
popup.js 有一些问题。 I cannot replace "."
我不能替换“。” with "-" and adding ".proxy.findit.dtu.dk" to the domain at the same time .
使用“-”并同时将“.proxy.findit.dtu.dk”添加到域中。 Only the adding is working.
只有添加有效。 What I got after running the example was: https://www.sciencedirect.com.proxy.findit.dtu.dk/science/article/abs/pii/S0925838811021414
运行示例后得到的是: https://www.sciencedirect.com.proxy.findit.dtu.dk/science/article/abs/pii/S0925838811021414
I am totally new at JavaScript.我是 JavaScript 的新手。 Any suggestions to solve this?
有什么建议可以解决这个问题吗?
Cheers!干杯!
Assigning to location.href
is tricky because it leads to navigation so you should do it in one operation:分配给
location.href
很棘手,因为它会导致导航,因此您应该在一个操作中完成:
location.href = 'https://' +
location.hostname.replace(/\./g, '-') +
'.proxy.findit.dtu.dk' +
location.href.slice(location.origin.length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.