2015-11-30 27 views
1

我是一名心理学学生,我经常阅读论文。大学图书馆提供对数据库的访问,但我需要使用图书馆搜索引擎并每次登录。非常烦人。我找到了避免跳页的方法。Chrome扩展程序:点击编辑当前网址,然后重定向到编辑的网址

这里是方法:

我加上“ezp.lib.unimelb.edu.au”后,我发现在谷歌学术纸目标数据库地址的末端,那么它会重定向到库登录页面。

例如,本文的地址是:

http://www.sciencedirect.com/science/article/pii/S0006899315008550

我修改它:

http://www.sciencedirect.com.ezp.lib.unimelb.edu.au /科学/条/ 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


当我打开这个文件夹到浏览器,这是行不通的。

这是我第一次使用JS,任何人有任何建议?

谢谢!

+0

我觉得我在如何让popup.js在点击上运行以及在编辑网址时遇到了一些问题。 – Saturn

回答

1

而不是让一个扩展的,这将是一个更容易让它可以在任何浏览器使用的书签......

  • 右键点击书签栏
  • 选择“添加页...“
  • 在 ”名称“ 中输入任何你想要的 ”中国重定向“ 或什么
  • 在 ”URL“,复制下面的代码(无空格)

    javascript:(function(){location.href=location.href.replace('sciencedirect.com/','sciencedirect.com/ezp.lib.unimelb.edu.au/');})(); 
    

粘贴现在,当你在页面上,点击书签,它会重定向你


更新:在URL试试这个代码对于其他域

javascript:(function(){var%20l=location;l.href=l.origin+l.href.replace(l.origin,'ezp.lib.unimelb.edu.au/');})(); 
+0

好酷。谢谢!但问题是,根据不同的数据库进行更改,因此我必须根据目标地址来更换它。 – Saturn

+0

'replace'方法可以使用正则表达式来定位域,那么所有域都以'.com'结尾?还有其他一些例子吗? – Mottie

+0

.net/.com.au/.org/.ac。英国/等... – Saturn

0
的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":["background.js"] 
 
    }, 
 

 
    "permissions": [ 
 
    "activeTab", 
 
    "tabs" 
 
    ] 
 
}


background.js

//Wait for click 
 

 
chrome.browserAction.onClicked.addListener(function(tab) { 
 
    chrome.tabs.executeScript(null, { 
 
    \t "file": "popup.js" 
 
    }, function(){ 
 
    \t "popup.js"; 
 
    \t console.log("Script Executed ..."); 
 
    }); 
 
})


popup.js

// Change the url to library when on click 
 

 
var l=location;l.href=l.origin+l.href.replace(l.origin, '.ezp.lib.unimelb.edu.au');

他们工作得很好。

完成第一个Chrome扩展非常酷。感谢Mottie的帮助。

2

即使没有点击,您也可以做到这一点。您可以使用此URL模式的content script,以便将脚本注入此页面。然后,您可以使用chrome.runtime.sendMessage()将消息发送到后台脚本,您的监听器将在此创建一个链接,然后使用chrome.tabs.update()和新URL重新加载标签页。

manifest.json的

{ 
"name": "My extension", 
... 

    "content_scripts": [{ 
     "matches": ["http://www.sciencedirect.com/science/article/pii/*"], 
     "js": ["content-script.js"] 
    }], 
    ... 
} 

内容的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社区的答案,我希望它有帮助。

+0

非常感谢!帮忙! – Saturn

相关问题