-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
116 lines (100 loc) · 3.68 KB
/
background.js
File metadata and controls
116 lines (100 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var $isEmpty = function ( h ) {
var k;
for ( k in h ) {
return false;
}
return true;
}
var $isTrue = function ( b ){ // Fuck JS, who would fucking think `[],{}` as true ????
if( b instanceof Object ){
return !$isEmpty( b );
}
return b !== "" && !!b;
};
var Spiffo = {};
Spiffo.tabControl = {
thisTab: {},
setTabTitle: function ( title, appended ) {
if( appended ) {
if( document.title.indexOf( title ) < 0 ) {
document.title = title + document.title;
}
}
else {
document.title = title;
}
},
};
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if ( changeInfo.status === 'complete' || changeInfo[ "title" ] ) {
console.log( tab, changeInfo );
chrome.storage.sync.get(tab.url, function(data) {
if ( data[ tab.url ] && data[ tab.url ][ "lock" ] ) {
chrome.scripting.executeScript({
target: {tabId: tabId},
func: Spiffo.tabControl.setTabTitle,
args: [ data[ tab.url ][ "title" ], data[ tab.url ][ "appendMode" ] ]
});
}
});
}
});
Spiffo.tombstone = {
spawnNewWin: function ( win, tabs ) {
let winFram = win[ "info" ];
delete winFram[ "id" ]; // Legacy ID is useless.
//alert( JSON.stringify( winFram ) )
chrome.windows.create( winFram, function ( neoWin ) {
for ( let j = 0; j < tabs.length; ++j ) {
let tab = tabs[ j ];
tab[ "windowId" ] = neoWin[ "id" ];
delete tab[ "id" ];
delete tab[ "index" ];
delete tab[ "openerTabId" ];
//alert( JSON.stringify( tab ) )
chrome.tabs.create( tab );
}
setTimeout( function () {
chrome.tabs.remove( [neoWin.tabs[0].id] );
}, 200 );
} );
}
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if ( message.action === "WinsSpawnEvent" ) {
if( $isTrue( message ) && $isTrue( message.data.winMapped ) ) {
let winMapped = message.data.winMapped;
let winThis = message.winThis;
try {
let winAt = 0;
for ( let k in winMapped ) {
if ( winMapped.hasOwnProperty( k ) ) {
let win = winMapped[ k ];
let tabs = win [ "tabs" ];
//console.log( win );
if( winAt++ === 0 ) {
try{
for ( let j = 0; j < tabs.length; ++j ) {
let tab = tabs[ j ];
tab[ "windowId" ] = winThis[ "id" ];
chrome.tabs.create( tab );
}
}
catch ( e ) {
console.warn( e );
Spiffo.tombstone.spawnNewWin( win, tabs );
}
}
else {
Spiffo.tombstone.spawnNewWin( win, tabs );
}
}
}
}
catch ( e ) {
sendResponse( { result : "error", what: "Jesus Fuck !!! What->" + e.toString() } );
}
sendResponse( { result : "success" } );
}
}
});