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(){
|
||||
|
||||
Reference in New Issue
Block a user