Userscript bookmarklet
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
var includes = {{includes|tojson}};
|
||||
var excludes = {{excludes|tojson}};
|
||||
|
||||
var specialChars = '\\{}+.():-|^$';
|
||||
var makeRegex = function(pattern) {
|
||||
pattern = pattern.split('');
|
||||
var i, len = pattern.length;
|
||||
for( i = 0; i < len; i++) {
|
||||
var character = pattern[i];
|
||||
if(specialChars.indexOf(character) > -1) {
|
||||
pattern[i] = '\\' + character;
|
||||
} else if(character === '?') {
|
||||
pattern[i] = '.';
|
||||
} else if(character === '*') {
|
||||
pattern[i] = '.*';
|
||||
}
|
||||
}
|
||||
return new RegExp('^' + pattern.join('') + '$');
|
||||
};
|
||||
|
||||
var isCorrectUrl = function() {
|
||||
for(i in includes) {
|
||||
var reg = includes[i]
|
||||
if (makeRegex(reg).test(document.location.href))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
var addUserscript = function() {
|
||||
// Add window param
|
||||
document.body.setAttribute('cp_auto_open', true)
|
||||
|
||||
// Load userscript
|
||||
var e = document.createElement('script');
|
||||
e.setAttribute('type', 'text/javascript');
|
||||
e.setAttribute('charset', 'UTF-8');
|
||||
e.setAttribute('src', '{{host}}/userscript.get/couchpotato.js?r=' + Math.random() * 99999999);
|
||||
document.body.appendChild(e)
|
||||
}
|
||||
if(isCorrectUrl())
|
||||
addUserscript()
|
||||
else
|
||||
alert('Can\'t find a proper movie on this page..')
|
||||
@@ -22,9 +22,20 @@ class Userscript(Plugin):
|
||||
addApiView('userscript.get/<path:filename>', self.getUserScript, static = True)
|
||||
addApiView('userscript', self.iFrame)
|
||||
addApiView('userscript.add_via_url', self.getViaUrl)
|
||||
addApiView('userscript.bookmark', self.bookmark)
|
||||
|
||||
addEvent('userscript.get_version', self.getVersion)
|
||||
|
||||
def bookmark(self):
|
||||
|
||||
params = {
|
||||
'includes': fireEvent('userscript.get_includes', merge = True),
|
||||
'excludes': fireEvent('userscript.get_excludes', merge = True),
|
||||
'host': fireEvent('app.api_url', single = True)
|
||||
}
|
||||
|
||||
return self.renderTemplate(__file__, 'bookmark.js', **params)
|
||||
|
||||
def getUserScript(self, filename = ''):
|
||||
|
||||
params = {
|
||||
|
||||
@@ -63,15 +63,48 @@ var UserscriptSettingTab = new Class({
|
||||
self.settings = App.getPage('Settings')
|
||||
self.settings.addEvent('create', function(){
|
||||
|
||||
// See if userscript can be installed
|
||||
var userscript = false;
|
||||
try {
|
||||
if(Components.interfaces.gmIGreasemonkeyService)
|
||||
userscript = true
|
||||
}
|
||||
catch(e){
|
||||
userscript = Browser.chrome === true;
|
||||
}
|
||||
|
||||
self.settings.createGroup({
|
||||
'label': 'Install the Userscript'
|
||||
'name': 'userscript',
|
||||
'label': 'Install the bookmarklet' + (userscript ? ' or userscript' : ''),
|
||||
'description': 'Easily add movies via imdb.com, appletrailers and more'
|
||||
}).inject(self.settings.tabs.automation.content, 'top').adopt(
|
||||
new Element('a', {
|
||||
(userscript ? [new Element('a.userscript.button', {
|
||||
'text': 'Install userscript',
|
||||
'href': Api.createUrl('userscript.get')+'couchpotato.user.js',
|
||||
'target': '_self'
|
||||
})
|
||||
);
|
||||
}), new Element('span.or[text=or]')] : null),
|
||||
new Element('span.bookmarklet').adopt(
|
||||
new Element('a.button.green', {
|
||||
'text': '+CouchPotato',
|
||||
'href': "javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','" +
|
||||
Api.getOption('host') + '/userscript.bookmark/' +
|
||||
"?r='+Math.random()*99999999);document.body.appendChild(e)})());",
|
||||
'target': '',
|
||||
'events': {
|
||||
'click': function(e){
|
||||
(e).stop()
|
||||
alert('Drag it to your bookmark ;)')
|
||||
}
|
||||
}
|
||||
}),
|
||||
new Element('span', {
|
||||
'text': '⇽ Drag this to your bookmarks'
|
||||
})
|
||||
)
|
||||
).setStyles({
|
||||
'background-image': "url('"+Api.createUrl('static/userscript/userscript.png')+"')"
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 99 KiB |
@@ -85,35 +85,42 @@ var osd = function(){
|
||||
var popup = create('div', {
|
||||
'id': 'cp_popup'
|
||||
});
|
||||
|
||||
var onclick = function(){
|
||||
|
||||
// Try and get imdb url
|
||||
try {
|
||||
var regex = new RegExp(/tt(\d+)/);
|
||||
var imdb_id = document.body.innerHTML.match(regex)[0];
|
||||
if (imdb_id)
|
||||
iframe.setAttribute('src', createApiUrl('http://imdb.com/title/'+imdb_id+'/'))
|
||||
}
|
||||
catch(e){}
|
||||
|
||||
popup.innerHTML = '';
|
||||
popup.appendChild(create('a', {
|
||||
'innerHTML': '<img src="' + close_img + '" />',
|
||||
'id': 'close_button',
|
||||
'onclick': function(){
|
||||
popup.innerHTML = '';
|
||||
popup.appendChild(add_button);
|
||||
}
|
||||
}));
|
||||
popup.appendChild(iframe)
|
||||
}
|
||||
|
||||
var add_button = create('a', {
|
||||
'innerHTML': '<img src="' + cp_icon + '" />',
|
||||
'id': 'add_to',
|
||||
'onclick': function(){
|
||||
|
||||
// Try and get imdb url
|
||||
try {
|
||||
var regex = new RegExp(/tt(\d+)/);
|
||||
var imdb_id = document.body.innerHTML.match(regex)[0];
|
||||
if (imdb_id)
|
||||
iframe.setAttribute('src', createApiUrl('http://imdb.com/title/'+imdb_id+'/'))
|
||||
}
|
||||
catch(e){}
|
||||
|
||||
popup.innerHTML = '';
|
||||
popup.appendChild(create('a', {
|
||||
'innerHTML': '<img src="' + close_img + '" />',
|
||||
'id': 'close_button',
|
||||
'onclick': function(){
|
||||
popup.innerHTML = '';
|
||||
popup.appendChild(add_button);
|
||||
}
|
||||
}));
|
||||
popup.appendChild(iframe)
|
||||
}
|
||||
'onclick': onclick
|
||||
});
|
||||
popup.appendChild(add_button);
|
||||
|
||||
document.body.parentNode.insertBefore(popup, document.body);
|
||||
|
||||
// Auto fold open
|
||||
if(document.body.getAttribute('cp_auto_open'))
|
||||
onclick()
|
||||
};
|
||||
|
||||
var setVersion = function(){
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
/* @override
|
||||
http://127.0.0.1:5000/_api_/static/style/page/settings.css */
|
||||
|
||||
.page.settings:after {
|
||||
content: ".";
|
||||
display: block;
|
||||
@@ -550,4 +553,41 @@
|
||||
.page .tab_about .group_actions a {
|
||||
margin: 0 10px;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.group_userscript {
|
||||
background: center bottom no-repeat;
|
||||
min-height: 360px;
|
||||
font-size: 20px;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
.group_userscript h2 .hint {
|
||||
display: block;
|
||||
margin: 0 !important;
|
||||
}
|
||||
|
||||
.group_userscript .userscript {
|
||||
float: left;
|
||||
margin: 14px 0 0 25px;
|
||||
height: 36px;
|
||||
line-height: 25px;
|
||||
}
|
||||
|
||||
.group_userscript .or {
|
||||
float: left;
|
||||
margin: 20px 10px;
|
||||
}
|
||||
|
||||
.group_userscript .bookmarklet {
|
||||
display: block;
|
||||
display: block;
|
||||
float: left;
|
||||
padding: 20px 15px 0 0 ;
|
||||
border-radius: 5px;
|
||||
}
|
||||
|
||||
.group_userscript .bookmarklet span {
|
||||
margin-left: 10px;
|
||||
display: inline-block;
|
||||
}
|
||||
@@ -51,6 +51,7 @@
|
||||
new Uniform();
|
||||
|
||||
Api.setup({
|
||||
'host': {{ fireEvent('app.api_url', single = True)|tojson|safe }},
|
||||
'url': {{ url_for('api.index')|tojson|safe }},
|
||||
'path_sep': {{ sep|tojson|safe }},
|
||||
'is_remote': false
|
||||
|
||||
Reference in New Issue
Block a user