我是一名心理学专业的学生,我经常阅读论文。大学图书馆提供对数据库的访问,但我每次都需要使用图书馆搜索引擎并登录。很烦人。我找到了一种避免在页面上跳来跳去的方法。
方法如下:
我在谷歌学术找到一篇论文后在目标数据库地址末尾添加“ezp.lib.unimelb.edu.au”,然后它会重定向到图书馆登录页面。
例如论文的地址是:
http://www.sciencedirect.com/science/article/pii/S0006899315008550
我修改为:
http://www.sciencedirect.com .ezp.lib.unimelb.edu.au/science/article/pii/S000689315008550
我想创建一个 Chrome 扩展程序来在点击时完成这项工作(太懒了)。我尝试了几个小时,但没有用。
这是我所做的:
我在一个文件夹中有三个文件:
第一个文件:manifest.json
{
"manifest_version": 2,
"name": "Damn! Take me to the library!",
"description": "This extension automatically adds the 'ezp.lib.unimelb.edu.au' to the browser's address, allowing you to visit the databases bought by the library quickly",
"version": "1.0",
"browser_action": {
"default_icon": "unimelb.png",
"default_title": "Damn! Take me to the library!"
},
"background":{
"scripts":["popup.js"]
},
"permissions": [
"activeTab",
"tabs"
]
}
第二个文件:popup.js
function getCurrentTabUrlthenChangeIt(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var url = tab.url;
callback(url);
var newurl = url.replace('/',"ezp.lib.unimelb.edu.au/");
window.location.replace(newurl);
});
}
第三个文件:unimelb.png
当我将此文件夹加载到 Chrome 时,它不起作用。
第一次使用JS,请问大家有什么建议吗?
谢谢!
最佳答案
即使不点击也可以做到这一点。您可以使用 content script对于此 URL 模式,以便您的脚本被注入(inject)此页面。然后您可以使用 chrome.runtime.sendMessage()
向后台脚本发送消息,您的监听器将在此处创建您想要的链接,然后使用 chrome.tabs.update 重新加载选项卡()
与新的 URL。
list .json
{
"name": "My extension",
...
"content_scripts": [{
"matches": ["http://www.sciencedirect.com/science/article/pii/*"],
"js": ["content-script.js"]
}],
...
}
content-script.js
chrome.runtime.sendMessage({loadURL: true});
background.js
chrome.runtime.onMessage.addListener(function(message, sender, response) {
if (message.loadURL) {
var newurl = sender.tab.url.replace("/", "ezp.lib.unimelb.edu.au/");
chrome.tabs.update(sender.tab.id, {url: newURL})
}
);
这是我在 StackOverflow 社区的第一个回答,希望对您有所帮助。
关于javascript - Chrome 扩展 : Edit the current url on click and then redirect to the edited one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33990967/