繁体   English   中英

Chrome 扩展:使用特定规则重定向当前 url

[英]Chrome Extension:redirect the current url with specific rules

我是一名研究员,我想通过我的大学访问学术论文。 我发现我可以修改 URL 来实现这一点。 规则正在替换“。” 使用“-”并将“.proxy.findit.dtu.dk”添加到域中。

例如:来自: https://www.sciencedirect.com/science/article/pii/S0925838811021414
至: https://www-sciencedirect-com.proxy.findit.dtu.dk/science/article/pii/S0925838811021414

这篇文章的启发,我将代码修改为:
第一个文件: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" ] }

第二个文件: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');

第三个文件: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..."); }); })

我包含的最后一个文件是一个图形:DTU.png。
但是,它不起作用。 popup.js 有一些问题。 我不能替换“。” 使用“-”并同时将“.proxy.findit.dtu.dk”添加到域中 只有添加有效。 运行示例后得到的是: https://www.sciencedirect.com.proxy.findit.dtu.dk/science/article/abs/pii/S0925838811021414

我是 JavaScript 的新手。 有什么建议可以解决这个问题吗?
干杯!

分配给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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM